v4l2-dev.h 15 KB

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