v4l2-dev.h 16 KB

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