v4l2-dev.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. *
  3. * V 4 L 2 D R I V E R H E L P E R A P I
  4. *
  5. * Moved from videodev2.h
  6. *
  7. * Some commonly needed functions for drivers (v4l2-common.o module)
  8. */
  9. #ifndef _V4L2_DEV_H
  10. #define _V4L2_DEV_H
  11. #include <linux/poll.h>
  12. #include <linux/fs.h>
  13. #include <linux/device.h>
  14. #include <linux/cdev.h>
  15. #include <linux/mutex.h>
  16. #include <linux/videodev2.h>
  17. #include <media/media-entity.h>
  18. #define VIDEO_MAJOR 81
  19. #define VFL_TYPE_GRABBER 0
  20. #define VFL_TYPE_VBI 1
  21. #define VFL_TYPE_RADIO 2
  22. #define VFL_TYPE_SUBDEV 3
  23. #define VFL_TYPE_SDR 4
  24. #define VFL_TYPE_MAX 5
  25. /* Is this a receiver, transmitter or mem-to-mem? */
  26. /* Ignored for VFL_TYPE_SUBDEV. */
  27. #define VFL_DIR_RX 0
  28. #define VFL_DIR_TX 1
  29. #define VFL_DIR_M2M 2
  30. struct v4l2_ioctl_callbacks;
  31. struct video_device;
  32. struct v4l2_device;
  33. struct v4l2_ctrl_handler;
  34. /* Flag to mark the video_device struct as registered.
  35. Drivers can clear this flag if they want to block all future
  36. device access. It is cleared by video_unregister_device. */
  37. #define V4L2_FL_REGISTERED (0)
  38. /* file->private_data points to struct v4l2_fh */
  39. #define V4L2_FL_USES_V4L2_FH (1)
  40. /* Priority helper functions */
  41. /**
  42. * struct v4l2_prio_state - stores the priority states
  43. *
  44. * @prios: array with elements to store the array priorities
  45. *
  46. *
  47. * .. note::
  48. * The size of @prios array matches the number of priority types defined
  49. * by :ref:`enum v4l2_priority <v4l2-priority>`.
  50. */
  51. struct v4l2_prio_state {
  52. atomic_t prios[4];
  53. };
  54. /**
  55. * v4l2_prio_init - initializes a struct v4l2_prio_state
  56. *
  57. * @global: pointer to &struct v4l2_prio_state
  58. */
  59. void v4l2_prio_init(struct v4l2_prio_state *global);
  60. /**
  61. * v4l2_prio_change - changes the v4l2 file handler priority
  62. *
  63. * @global: pointer to the &struct v4l2_prio_state of the device node.
  64. * @local: pointer to the desired priority, as defined by :ref:`enum v4l2_priority <v4l2-priority>`
  65. * @new: Priority type requested, as defined by :ref:`enum v4l2_priority <v4l2-priority>`.
  66. *
  67. * .. note::
  68. * This function should be used only by the V4L2 core.
  69. */
  70. int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  71. enum v4l2_priority new);
  72. /**
  73. * v4l2_prio_open - Implements the priority logic for a file handler open
  74. *
  75. * @global: pointer to the &struct v4l2_prio_state of the device node.
  76. * @local: pointer to the desired priority, as defined by :ref:`enum v4l2_priority <v4l2-priority>`
  77. *
  78. * .. note::
  79. * This function should be used only by the V4L2 core.
  80. */
  81. void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
  82. /**
  83. * v4l2_prio_close - Implements the priority logic for a file handler close
  84. *
  85. * @global: pointer to the &struct v4l2_prio_state of the device node.
  86. * @local: priority to be released, as defined by :ref:`enum v4l2_priority <v4l2-priority>`
  87. *
  88. * .. note::
  89. * This function should be used only by the V4L2 core.
  90. */
  91. void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
  92. /**
  93. * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
  94. *
  95. * @global: pointer to the &struct v4l2_prio_state of the device node.
  96. *
  97. * .. note::
  98. * This function should be used only by the V4L2 core.
  99. */
  100. enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
  101. /**
  102. * v4l2_prio_close - Implements the priority logic for a file handler close
  103. *
  104. * @global: pointer to the &struct v4l2_prio_state of the device node.
  105. * @local: desired priority, as defined by :ref:`enum v4l2_priority <v4l2-priority>` local
  106. *
  107. * .. note::
  108. * This function should be used only by the V4L2 core.
  109. */
  110. int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
  111. /**
  112. * struct v4l2_file_operations - fs operations used by a V4L2 device
  113. *
  114. * @owner: pointer to struct module
  115. * @read: operations needed to implement the read() syscall
  116. * @write: operations needed to implement the write() syscall
  117. * @poll: operations needed to implement the poll() syscall
  118. * @unlocked_ioctl: operations needed to implement the ioctl() syscall
  119. * @compat_ioctl32: operations needed to implement the ioctl() syscall for
  120. * the special case where the Kernel uses 64 bits instructions, but
  121. * the userspace uses 32 bits.
  122. * @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU
  123. * @mmap: operations needed to implement the mmap() syscall
  124. * @open: operations needed to implement the open() syscall
  125. * @release: operations needed to implement the release() syscall
  126. *
  127. * .. note::
  128. *
  129. * Those operations are used to implemente the fs struct file_operations
  130. * at the V4L2 drivers. The V4L2 core overrides the fs ops with some
  131. * extra logic needed by the subsystem.
  132. */
  133. struct v4l2_file_operations {
  134. struct module *owner;
  135. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  136. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  137. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  138. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  139. #ifdef CONFIG_COMPAT
  140. long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
  141. #endif
  142. unsigned long (*get_unmapped_area) (struct file *, unsigned long,
  143. unsigned long, unsigned long, unsigned long);
  144. int (*mmap) (struct file *, struct vm_area_struct *);
  145. int (*open) (struct file *);
  146. int (*release) (struct file *);
  147. };
  148. /*
  149. * Newer version of video_device, handled by videodev2.c
  150. * This version moves redundant code from video device code to
  151. * the common handler
  152. */
  153. /**
  154. * struct video_device - Structure used to create and manage the V4L2 device
  155. * nodes.
  156. *
  157. * @entity: &struct media_entity
  158. * @intf_devnode: pointer to &struct media_intf_devnode
  159. * @pipe: &struct media_pipeline
  160. * @fops: pointer to &struct v4l2_file_operations for the video device
  161. * @device_caps: device capabilities as used in v4l2_capabilities
  162. * @dev: &struct device for the video device
  163. * @cdev: character device
  164. * @v4l2_dev: pointer to &struct v4l2_device parent
  165. * @dev_parent: pointer to &struct device parent
  166. * @ctrl_handler: Control handler associated with this device node.
  167. * May be NULL.
  168. * @queue: &struct vb2_queue associated with this device node. May be NULL.
  169. * @prio: pointer to &struct v4l2_prio_state with device's Priority state.
  170. * If NULL, then v4l2_dev->prio will be used.
  171. * @name: video device name
  172. * @vfl_type: V4L device type
  173. * @vfl_dir: V4L receiver, transmitter or m2m
  174. * @minor: device node 'minor'. It is set to -1 if the registration failed
  175. * @num: number of the video device node
  176. * @flags: video device flags. Use bitops to set/clear/test flags
  177. * @index: attribute to differentiate multiple indices on one physical device
  178. * @fh_lock: Lock for all v4l2_fhs
  179. * @fh_list: List of &struct v4l2_fh
  180. * @dev_debug: Internal device debug flags, not for use by drivers
  181. * @tvnorms: Supported tv norms
  182. *
  183. * @release: video device release() callback
  184. * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
  185. *
  186. * @valid_ioctls: bitmap with the valid ioctls for this device
  187. * @disable_locking: bitmap with the ioctls that don't require locking
  188. * @lock: pointer to &struct mutex serialization lock
  189. *
  190. * .. note::
  191. * Only set @dev_parent if that can't be deduced from @v4l2_dev.
  192. */
  193. struct video_device
  194. {
  195. #if defined(CONFIG_MEDIA_CONTROLLER)
  196. struct media_entity entity;
  197. struct media_intf_devnode *intf_devnode;
  198. struct media_pipeline pipe;
  199. #endif
  200. const struct v4l2_file_operations *fops;
  201. u32 device_caps;
  202. /* sysfs */
  203. struct device dev;
  204. struct cdev *cdev;
  205. struct v4l2_device *v4l2_dev;
  206. struct device *dev_parent;
  207. struct v4l2_ctrl_handler *ctrl_handler;
  208. struct vb2_queue *queue;
  209. struct v4l2_prio_state *prio;
  210. /* device info */
  211. char name[32];
  212. int vfl_type;
  213. int vfl_dir;
  214. int minor;
  215. u16 num;
  216. unsigned long flags;
  217. int index;
  218. /* V4L2 file handles */
  219. spinlock_t fh_lock;
  220. struct list_head fh_list;
  221. int dev_debug;
  222. v4l2_std_id tvnorms;
  223. /* callbacks */
  224. void (*release)(struct video_device *vdev);
  225. const struct v4l2_ioctl_ops *ioctl_ops;
  226. DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
  227. DECLARE_BITMAP(disable_locking, BASE_VIDIOC_PRIVATE);
  228. struct mutex *lock;
  229. };
  230. #define media_entity_to_video_device(__e) \
  231. container_of(__e, struct video_device, entity)
  232. /* dev to video-device */
  233. #define to_video_device(cd) container_of(cd, struct video_device, dev)
  234. /**
  235. * __video_register_device - register video4linux devices
  236. *
  237. * @vdev: struct video_device to register
  238. * @type: type of device to register
  239. * @nr: which device node number is desired:
  240. * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
  241. * @warn_if_nr_in_use: warn if the desired device node number
  242. * was already in use and another number was chosen instead.
  243. * @owner: module that owns the video device node
  244. *
  245. * The registration code assigns minor numbers and device node numbers
  246. * based on the requested type and registers the new device node with
  247. * the kernel.
  248. *
  249. * This function assumes that struct video_device was zeroed when it
  250. * was allocated and does not contain any stale date.
  251. *
  252. * An error is returned if no free minor or device node number could be
  253. * found, or if the registration of the device node failed.
  254. *
  255. * Returns 0 on success.
  256. *
  257. * Valid values for @type are:
  258. *
  259. * - %VFL_TYPE_GRABBER - A frame grabber
  260. * - %VFL_TYPE_VBI - Vertical blank data (undecoded)
  261. * - %VFL_TYPE_RADIO - A radio card
  262. * - %VFL_TYPE_SUBDEV - A subdevice
  263. * - %VFL_TYPE_SDR - Software Defined Radio
  264. *
  265. * .. note::
  266. *
  267. * This function is meant to be used only inside the V4L2 core.
  268. * Drivers should use video_register_device() or
  269. * video_register_device_no_warn().
  270. */
  271. int __must_check __video_register_device(struct video_device *vdev, int type,
  272. int nr, int warn_if_nr_in_use, struct module *owner);
  273. /**
  274. * video_register_device - register video4linux devices
  275. *
  276. * @vdev: struct video_device to register
  277. * @type: type of device to register
  278. * @nr: which device node number is desired:
  279. * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
  280. *
  281. * Internally, it calls __video_register_device(). Please see its
  282. * documentation for more details.
  283. *
  284. * .. note::
  285. * if video_register_device fails, the release() callback of
  286. * &struct video_device structure is *not* called, so the caller
  287. * is responsible for freeing any data. Usually that means that
  288. * you video_device_release() should be called on failure.
  289. */
  290. static inline int __must_check video_register_device(struct video_device *vdev,
  291. int type, int nr)
  292. {
  293. return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
  294. }
  295. /**
  296. * video_register_device_no_warn - register video4linux devices
  297. *
  298. * @vdev: struct video_device to register
  299. * @type: type of device to register
  300. * @nr: which device node number is desired:
  301. * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
  302. *
  303. * This function is identical to video_register_device() except that no
  304. * warning is issued if the desired device node number was already in use.
  305. *
  306. * Internally, it calls __video_register_device(). Please see its
  307. * documentation for more details.
  308. *
  309. * .. note::
  310. * if video_register_device fails, the release() callback of
  311. * &struct video_device structure is *not* called, so the caller
  312. * is responsible for freeing any data. Usually that means that
  313. * you video_device_release() should be called on failure.
  314. */
  315. static inline int __must_check video_register_device_no_warn(
  316. struct video_device *vdev, int type, int nr)
  317. {
  318. return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
  319. }
  320. /**
  321. * video_unregister_device - Unregister video devices.
  322. *
  323. * @vdev: &struct video_device to register
  324. *
  325. * Does nothing if vdev == NULL or if video_is_registered() returns false.
  326. */
  327. void video_unregister_device(struct video_device *vdev);
  328. /**
  329. * video_device_alloc - helper function to alloc &struct video_device
  330. *
  331. * Returns NULL if %-ENOMEM or a &struct video_device on success.
  332. */
  333. struct video_device * __must_check video_device_alloc(void);
  334. /**
  335. * video_device_release - helper function to release &struct video_device
  336. *
  337. * @vdev: pointer to &struct video_device
  338. *
  339. * Can also be used for video_device->release\(\).
  340. */
  341. void video_device_release(struct video_device *vdev);
  342. /**
  343. * video_device_release_empty - helper function to implement the
  344. * video_device->release\(\) callback.
  345. *
  346. * @vdev: pointer to &struct video_device
  347. *
  348. * This release function does nothing.
  349. *
  350. * It should be used when the video_device is a static global struct.
  351. *
  352. * .. note::
  353. * Having a static video_device is a dubious construction at best.
  354. */
  355. void video_device_release_empty(struct video_device *vdev);
  356. /**
  357. * v4l2_is_known_ioctl - Checks if a given cmd is a known V4L ioctl
  358. *
  359. * @cmd: ioctl command
  360. *
  361. * returns true if cmd is a known V4L2 ioctl
  362. */
  363. bool v4l2_is_known_ioctl(unsigned int cmd);
  364. /** v4l2_disable_ioctl_locking - mark that a given command
  365. * shouldn't use core locking
  366. *
  367. * @vdev: pointer to &struct video_device
  368. * @cmd: ioctl command
  369. */
  370. static inline void v4l2_disable_ioctl_locking(struct video_device *vdev,
  371. unsigned int cmd)
  372. {
  373. if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  374. set_bit(_IOC_NR(cmd), vdev->disable_locking);
  375. }
  376. /**
  377. * v4l2_disable_ioctl- mark that a given command isn't implemented.
  378. * shouldn't use core locking
  379. *
  380. * @vdev: pointer to &struct video_device
  381. * @cmd: ioctl command
  382. *
  383. * This function allows drivers to provide just one v4l2_ioctl_ops struct, but
  384. * disable ioctls based on the specific card that is actually found.
  385. *
  386. * .. note::
  387. *
  388. * This must be called before video_register_device.
  389. * See also the comments for determine_valid_ioctls().
  390. */
  391. static inline void v4l2_disable_ioctl(struct video_device *vdev,
  392. unsigned int cmd)
  393. {
  394. if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  395. set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
  396. }
  397. /**
  398. * video_get_drvdata - gets private data from &struct video_device.
  399. *
  400. * @vdev: pointer to &struct video_device
  401. *
  402. * returns a pointer to the private data
  403. */
  404. static inline void *video_get_drvdata(struct video_device *vdev)
  405. {
  406. return dev_get_drvdata(&vdev->dev);
  407. }
  408. /**
  409. * video_set_drvdata - sets private data from &struct video_device.
  410. *
  411. * @vdev: pointer to &struct video_device
  412. * @data: private data pointer
  413. */
  414. static inline void video_set_drvdata(struct video_device *vdev, void *data)
  415. {
  416. dev_set_drvdata(&vdev->dev, data);
  417. }
  418. /**
  419. * video_devdata - gets &struct video_device from struct file.
  420. *
  421. * @file: pointer to struct file
  422. */
  423. struct video_device *video_devdata(struct file *file);
  424. /**
  425. * video_drvdata - gets private data from &struct video_device using the
  426. * struct file.
  427. *
  428. * @file: pointer to struct file
  429. *
  430. * This is function combines both video_get_drvdata() and video_devdata()
  431. * as this is used very often.
  432. */
  433. static inline void *video_drvdata(struct file *file)
  434. {
  435. return video_get_drvdata(video_devdata(file));
  436. }
  437. /**
  438. * video_device_node_name - returns the video device name
  439. *
  440. * @vdev: pointer to &struct video_device
  441. *
  442. * Returns the device name string
  443. */
  444. static inline const char *video_device_node_name(struct video_device *vdev)
  445. {
  446. return dev_name(&vdev->dev);
  447. }
  448. /**
  449. * video_is_registered - returns true if the &struct video_device is registered.
  450. *
  451. *
  452. * @vdev: pointer to &struct video_device
  453. */
  454. static inline int video_is_registered(struct video_device *vdev)
  455. {
  456. return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
  457. }
  458. #endif /* _V4L2_DEV_H */