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 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 19 #define VIDEO_MAJOR 81 20 21 #define VFL_TYPE_GRABBER 0 22 #define VFL_TYPE_VBI 1 23 #define VFL_TYPE_RADIO 2 24 #define VFL_TYPE_VTX 3 25 #define VFL_TYPE_MAX 4 26 27 struct v4l2_ioctl_callbacks; 28 struct video_device; 29 struct v4l2_device; 30 31 /* Flag to mark the video_device struct as registered. 32 Drivers can clear this flag if they want to block all future 33 device access. It is cleared by video_unregister_device. */ 34 #define V4L2_FL_REGISTERED (0) 35 #define V4L2_FL_USES_V4L2_FH (1) 36 37 struct v4l2_file_operations { 38 struct module *owner; 39 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); 40 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); 41 unsigned int (*poll) (struct file *, struct poll_table_struct *); 42 long (*ioctl) (struct file *, unsigned int, unsigned long); 43 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); 44 unsigned long (*get_unmapped_area) (struct file *, unsigned long, 45 unsigned long, unsigned long, unsigned long); 46 int (*mmap) (struct file *, struct vm_area_struct *); 47 int (*open) (struct file *); 48 int (*release) (struct file *); 49 }; 50 51 /* 52 * Newer version of video_device, handled by videodev2.c 53 * This version moves redundant code from video device code to 54 * the common handler 55 */ 56 57 struct video_device 58 { 59 /* device ops */ 60 const struct v4l2_file_operations *fops; 61 62 /* sysfs */ 63 struct device dev; /* v4l device */ 64 struct cdev *cdev; /* character device */ 65 66 /* Set either parent or v4l2_dev if your driver uses v4l2_device */ 67 struct device *parent; /* device parent */ 68 struct v4l2_device *v4l2_dev; /* v4l2_device parent */ 69 70 /* device info */ 71 char name[32]; 72 int vfl_type; 73 /* 'minor' is set to -1 if the registration failed */ 74 int minor; 75 u16 num; 76 /* use bitops to set/clear/test flags */ 77 unsigned long flags; 78 /* attribute to differentiate multiple indices on one physical device */ 79 int index; 80 81 /* V4L2 file handles */ 82 spinlock_t fh_lock; /* Lock for all v4l2_fhs */ 83 struct list_head fh_list; /* List of struct v4l2_fh */ 84 85 int debug; /* Activates debug level*/ 86 87 /* Video standard vars */ 88 v4l2_std_id tvnorms; /* Supported tv norms */ 89 v4l2_std_id current_norm; /* Current tvnorm */ 90 91 /* callbacks */ 92 void (*release)(struct video_device *vdev); 93 94 /* ioctl callbacks */ 95 const struct v4l2_ioctl_ops *ioctl_ops; 96 }; 97 98 /* dev to video-device */ 99 #define to_video_device(cd) container_of(cd, struct video_device, dev) 100 101 /* Register video devices. Note that if video_register_device fails, 102 the release() callback of the video_device structure is *not* called, so 103 the caller is responsible for freeing any data. Usually that means that 104 you call video_device_release() on failure. */ 105 int __must_check video_register_device(struct video_device *vdev, int type, int nr); 106 107 /* Same as video_register_device, but no warning is issued if the desired 108 device node number was already in use. */ 109 int __must_check video_register_device_no_warn(struct video_device *vdev, int type, int nr); 110 111 /* Unregister video devices. Will do nothing if vdev == NULL or 112 video_is_registered() returns false. */ 113 void video_unregister_device(struct video_device *vdev); 114 115 /* helper functions to alloc/release struct video_device, the 116 latter can also be used for video_device->release(). */ 117 struct video_device * __must_check video_device_alloc(void); 118 119 /* this release function frees the vdev pointer */ 120 void video_device_release(struct video_device *vdev); 121 122 /* this release function does nothing, use when the video_device is a 123 static global struct. Note that having a static video_device is 124 a dubious construction at best. */ 125 void video_device_release_empty(struct video_device *vdev); 126 127 /* helper functions to access driver private data. */ 128 static inline void *video_get_drvdata(struct video_device *vdev) 129 { 130 return dev_get_drvdata(&vdev->dev); 131 } 132 133 static inline void video_set_drvdata(struct video_device *vdev, void *data) 134 { 135 dev_set_drvdata(&vdev->dev, data); 136 } 137 138 struct video_device *video_devdata(struct file *file); 139 140 /* Combine video_get_drvdata and video_devdata as this is 141 used very often. */ 142 static inline void *video_drvdata(struct file *file) 143 { 144 return video_get_drvdata(video_devdata(file)); 145 } 146 147 static inline const char *video_device_node_name(struct video_device *vdev) 148 { 149 return dev_name(&vdev->dev); 150 } 151 152 static inline int video_is_registered(struct video_device *vdev) 153 { 154 return test_bit(V4L2_FL_REGISTERED, &vdev->flags); 155 } 156 157 #endif /* _V4L2_DEV_H */ 158