r5u870

Ricoh R5U870 Linux Driver
git clone https://logand.com/git/r5u870.git/
Log | Files | Refs | README | LICENSE

usbcam.h (25260B)


      1 /*
      2  * USBCAM abstraction library for USB webcam drivers
      3  * Version 0.11.1
      4  * 
      5  * Copyright (c) 2007 Sam Revitch <samr7 cs washington edu>
      6  *
      7  * This program is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 2, or (at your option)
     10  * any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
     20  */
     21 
     22 /*
     23  * This library is intended to ease the process of creating drivers
     24  * for simpler USB webcam devices.  It handles most V4L interactions, and
     25  * all USB driver entry points.  It provides a minidriver callback API
     26  * for handling most common tasks.
     27  */
     28 
     29 #ifndef __USBCAM_H__
     30 #define	__USBCAM_H__
     31 
     32 #ifdef __KERNEL__
     33 
     34 #include <linux/usb.h>
     35 #include <linux/mutex.h>
     36 #include <linux/module.h>
     37 #include <linux/version.h>
     38 #include <linux/videodev.h>
     39 #include <media/v4l2-common.h>
     40 
     41 /* The actual per-minidriver structure is opaque */
     42 typedef struct usbcam_minidrv usbcam_minidrv_t;
     43 struct usbcam_dev;
     44 struct usbcam_curframe;
     45 
     46 
     47 /*
     48  * Log file and debug infrastructure
     49  */
     50 #define usbcam_info(UDP, FMT, ARG...) do {				\
     51 	printk(KERN_INFO "%s: " FMT "\n",				\
     52 	       (UDP)->ud_dev_name, ## ARG);				\
     53 } while (0)
     54 #define usbcam_warn(UDP, FMT, ARG...) do {				\
     55 	printk(KERN_WARNING "%s: " FMT "\n",				\
     56 	       (UDP)->ud_dev_name, ## ARG);				\
     57 } while (0)
     58 #define usbcam_err(UDP, FMT, ARG...) do {				\
     59 	printk(KERN_ERR "%s: " FMT "\n",				\
     60 	       (UDP)->ud_dev_name, ## ARG);				\
     61 } while (0)
     62 
     63 
     64 #if defined(CONFIG_USB_USBCAM_DEBUG)
     65 #define usbcam_dbg(UDP, SUBSYS, FMT, ARG...) do {			\
     66 	if ((UDP)->ud_debug)                            \
     67 		printk(KERN_DEBUG "%s: " FMT "\n",			\
     68 		       (UDP)->ud_dev_name, ## ARG);			\
     69 } while (0)
     70 #define usbcam_assert(expr)						\
     71 do {									\
     72 	if (!(expr)) {							\
     73 		printk(KERN_ERR "%s:%d: assertion \"" # expr "\" "	\
     74 		       "failed", __FILE__, __LINE__);			\
     75 		dump_stack();						\
     76 	}								\
     77 } while (0)
     78 extern void usbcam_hexdump(struct usbcam_dev *udp, const u8 *buf, size_t len);
     79 
     80 #else
     81 #define usbcam_dbg(UDP, SUBSYS, FMT, ARG...)
     82 #define usbcam_assert(expr)
     83 #define usbcam_hexdump(udp, buf, len)
     84 #endif
     85 
     86 /*
     87  * Debug subsystem bit values
     88  *
     89  * The usbcam_dev.ud_debug debug subsystem mask is a pointer to a
     90  * per-minidriver integer, which is usually a module parameter and can
     91  * be manipulated via sysfs.
     92  */
     93 enum {
     94 	USBCAM_DBG_VIDEOBUF = 0,
     95 	USBCAM_DBG_CAPTURE,
     96 	USBCAM_DBG_IOCTL_BUF,
     97 	USBCAM_DBG_IOCTL_FMT,
     98 	USBCAM_DBG_IOCTL_MISC,
     99 	USBCAM_DBG_DEV_STATE,
    100 	USBCAM_DBG_ALTSETTING,
    101 	USBCAM_DBG_URBSTREAM,
    102 
    103 	/* First bit available to minidrivers */
    104 	USBCAM_DBGBIT_MD_START = 8,
    105 };
    106 
    107 
    108 
    109 /*
    110  * The usbcam_pix_fmt structure is used to describe a pixel format natively
    111  * supported by the driver to V4L clients.  A minidriver may set the
    112  * ud_fmt_array member of usbcam_dev to point to an array of these
    113  * structures, and the array will be used to service the ENUMFMT ioctl,
    114  * as long as the minidriver does not intercept that ioctl.
    115  */
    116 struct usbcam_pix_fmt {
    117 	char description[32];
    118 	unsigned int pixelformat;
    119 	unsigned int flags;
    120 };
    121 
    122 
    123 /*
    124  * usbcam_dev: The per-device structure representing:
    125  *	(1) A USB device/interface
    126  *	(2) A registered V4L device
    127  *
    128  * Commented fields are of interest to minidrivers.
    129  * Uncommented fields should be considered opaque.
    130  */
    131 
    132 struct usbcam_dev {
    133 	/*
    134 	 * ud_vdev is the video4linux device structure.
    135 	 * The minidriver may be interested in a few fields:
    136 	 *	ud_vdev.name: The device name displayed by applications
    137 	 */
    138 	struct video_device		ud_vdev;
    139 
    140 	/*
    141 	 * ud_dev, ud_intf: The associated USB device/primary interface
    142 	 * These members are read-only to all except when set during
    143 	 * usbcam_dev structure initialization.
    144 	 */
    145 	struct usb_device		*ud_dev;
    146 	struct usb_interface		*ud_intf;
    147 
    148 	usbcam_minidrv_t		*ud_minidrv;
    149 
    150 	/*
    151 	 * ud_minidrv_data: Minidriver private data
    152 	 * During structure initialization, if a minidriver structure
    153 	 * size was specified to usbcam_register(), that many bytes of
    154 	 * memory are allocated, the allocation is assigned here, and
    155 	 * the original allocation is automatically freed with the
    156 	 * usbcam_dev structure.  Otherwise this member is initialized
    157 	 * to NULL.
    158 	 * The minidriver may set whatever policies it wants with how
    159 	 * this field is managed.
    160 	 */
    161 	void				*ud_minidrv_data;
    162 
    163 	struct list_head		ud_drv_links;
    164 
    165 	/*
    166 	 * ud_minidrv_id: The device's unique number among all devices
    167 	 * belonging to the minidriver.  Set prior to the minidriver's
    168 	 * init handler being called.  Read-only at all other times.
    169 	 */
    170 	int				ud_minidrv_id;
    171 
    172 	/*
    173 	 * ud_lock: the mutex protecting most of this structure and all
    174 	 * minidriver entry points.
    175 	 */
    176 	struct mutex			ud_lock;
    177 
    178 	/*
    179 	 * ud_format: Currently configured size/format
    180 	 *	Protected by ud_lock
    181 	 *	Modified only by minidriver
    182 	 *	Examined by ioctl handler and framebuffer allocator
    183 	 */
    184 	struct v4l2_pix_format		ud_format;
    185 
    186 	/*
    187 	 * ud_fmt_array, ud_fmt_array_elem_size, ud_fmt_array_len:
    188 	 * Supported pixel formats for enumeration
    189 	 *	Protected by ud_lock
    190 	 *	Modified only by minidriver, usually set by init callout
    191 	 *	Examined by default ioctl handler for ENUMFMT
    192 	 */
    193 	const struct usbcam_pix_fmt	*ud_fmt_array;
    194 	int				ud_fmt_array_elem_size;
    195 	int				ud_fmt_array_len;
    196 
    197 	struct list_head		ud_ctrl_list;
    198 
    199 	/*
    200 	 * ud_capturing: Minidriver capture-in-progress flag
    201 	 *	Protected by ud_lock
    202 	 *	Modified only by minidriver, e.g. cap_start, cap_stop
    203 	 *	Examined by framebuffer interface and S_FMT ioctl handler
    204 	 */
    205 	unsigned int			ud_capturing : 1,
    206 
    207 	/*
    208 	 * ud_disconnected: Set if the underlying USB device has been
    209 	 * disconnected, just prior to invoking the minidriver disconnect
    210 	 * callout, if one is provided.  This field should be considered
    211 	 * read-only to minidrivers.
    212 	 */
    213 					ud_disconnected : 1,
    214 
    215 					ud_initializing : 1,
    216 					ud_suspended : 1,
    217 					ud_disconnected_primary : 1,
    218 					ud_videodev_released : 1;
    219 
    220 	struct kref			ud_kref;
    221 	struct list_head		ud_interface_list;
    222 
    223 	struct list_head		ud_frame_cap_queue;
    224 
    225 	int				*ud_debug;
    226 
    227 	int				ud_work_refs;
    228 	spinlock_t			ud_work_lock;
    229 	int				ud_work_lockwait;
    230 	struct task_struct		*ud_work_thread;
    231 	struct list_head		ud_work_queue;
    232 
    233 	struct mutex			ud_open_lock;
    234 	int				ud_user_refs;
    235 	unsigned int			ud_autopm_ref : 1;
    236 	struct file			*ud_excl_owner;
    237 
    238 	/*
    239 	 * ud_dev_name: Name of device as used for worker thread names and
    240 	 * debug messages.  The minidriver may set this field in its init
    241 	 * callout, but should consider it read-only after that point.
    242 	 */
    243 	char				ud_dev_name[32];
    244 };
    245 
    246 
    247 /*
    248  * Per-device reference counting helpers
    249  *
    250  * When the last reference is released, the minidriver's release
    251  * callout will be invoked.
    252  *
    253  * usbcam_get() may be called from any context.
    254  * usbcam_put() can sleep, and may not be called while holding the device
    255  * lock (see below).
    256  */
    257 #define usbcam_get(UDP) kref_get(&(UDP)->ud_kref)
    258 extern void usbcam_put(struct usbcam_dev *udp);
    259 
    260 
    261 /*
    262  * Per-Device locking helpers
    263  *
    264  * The minidriver callouts, which are described below in usbcam_dev_ops
    265  * and usbcam_urbstream_ops, are all invoked with the device lock held.
    266  * Also, all usbcam work queue callouts are invoked with the device lock
    267  * held.
    268  *
    269  * Minidrivers that must be entered through paths other than those
    270  * described above may need to examine or update data structures
    271  * protected by the device lock, and may do so using the lock
    272  * acquire/release macros.  For example, minidrivers that use
    273  * procfs/sysfs file operations, or completion callouts unsuitable for
    274  * the usbcam work queue will need this.
    275  *
    276  * Minidrivers may release and reacquire the lock inside of certain
    277  * usbcam minidriver callouts (see 
    278  */
    279 #define usbcam_lock(UDP) mutex_lock(&(UDP)->ud_lock)
    280 #define usbcam_unlock(UDP) mutex_unlock(&(UDP)->ud_lock)
    281 #define usbcam_chklock(UDP) usbcam_assert(mutex_is_locked(&(UDP)->ud_lock))
    282 
    283 
    284 /*
    285  * MINIDRIVER CALLOUTS
    286  *
    287  * Callouts invoked at various stages of the lifetime of a usbcam_dev
    288  * device.
    289  *
    290  * REQUIRED: init, cap_start, cap_stop.
    291  *
    292  * All other callouts are optional.
    293  *
    294  * By default, all callouts are invoked with the device lock held.
    295  *
    296  * The device lock is not intended to be held for long periods of time.
    297  * Some operations may run for a long time, and may need to sleep
    298  * waiting for an operation to complete on the device.  If these
    299  * operations need to be able to run at the same time as capture, the
    300  * minidriver must ensure that it does not hold the device lock while
    301  * waiting for such long operations to complete.
    302  *
    303  * To support operating without the device lock, there are flags
    304  * in the ops structure to selectively disable this behavior for the
    305  * ioctl callout, control callouts, and power management callouts.
    306  * The minidriver will be responsible for acquiring and releasing the
    307  * device lock, and re-verifying the device state upon reacquisition.
    308  *
    309  * Certain callouts are explicitly restricted from releasing and
    310  * reacquiring the device lock.  These include:
    311  *
    312  * disconnect, open, close, try_format, set_format, cap_start, cap_stop,
    313  * testpattern
    314  * 
    315  */
    316 struct usbcam_dev_ops {
    317 	int	unlocked_ioctl : 1,
    318 		unlocked_ctrl : 1,
    319 		unlocked_pm : 1,
    320 		supports_autosuspend : 1,
    321 		no_workref_on_open : 1;
    322 
    323 	/* init: invoked when a matching USB device is discovered */
    324 	int (*init)(struct usbcam_dev *udp, const struct usb_device_id *);
    325 
    326 	/* suspend/resume: invoked from power management paths */
    327 	int (*suspend)(struct usbcam_dev *udp, pm_message_t message);
    328 	int (*resume)(struct usbcam_dev *udp);
    329 
    330 	/* disconnect: invoked when a device has been disconnected */
    331 	void (*disconnect)(struct usbcam_dev *udp);
    332 
    333 	/* release: invoked when a usbcam_dev is about to be freed */
    334 	void (*release)(struct usbcam_dev *udp);
    335 
    336 	/* open: invoked on first user open of the device */
    337 	int (*open)(struct usbcam_dev *udp);
    338 
    339 	/* close: invoked on last user close of the device */
    340 	void (*close)(struct usbcam_dev *udp);
    341 
    342 	/* try_format: invoked to negotiate capture format */
    343 	int (*try_format)(struct usbcam_dev *udp,
    344 			  struct v4l2_pix_format *fmt_in_out);
    345 
    346 	/* set_format: invoked when the capture format is being set */
    347 	int (*set_format)(struct usbcam_dev *udp,
    348 			  struct v4l2_pix_format *fmt_in_out);
    349 
    350 	/* ioctl: ioctl call interception for most commands */
    351 	int (*ioctl)(struct usbcam_dev *udp, int cmd, void *arg);
    352 
    353 	/* cap_start: invoked when capture should be initiated */
    354 	int (*cap_start)(struct usbcam_dev *udp);
    355 
    356 	/* cap_stop: invoked when capture should be halted */
    357 	void (*cap_stop)(struct usbcam_dev *udp);
    358 };
    359 
    360 
    361 /*
    362  * Minidrivers may register themselves using usbcam_register_mod(),
    363  * although the recommended method is to use the usbcam_register()
    364  * macro instead.
    365  *
    366  * The driver should keep a usbcam_minidrv structure pointer as a
    367  * handle to the usbcam registration, as it will need it later when it
    368  * needs to unregister itself.  usbcam_register_mod() accepts a pointer
    369  * to this structure, and fills it in on success.
    370  *
    371  * The minidriver must report a primary version number in KERNEL_VERSION
    372  * format, which is used by the default VIDIOC_QUERYCAP ioctl handler.
    373  * The minidriver may optionally report an extra version string.
    374  *
    375  * usbcam_register_mod() will cause usbcam to register a new USB driver
    376  * with the given device ID table, so that it may be called when
    377  * matching devices are discovered.
    378  *
    379  * When a matching device is detected, usbcam will:
    380  *	-> Allocate a usbcam_dev structure, including a minidriver-specific
    381  *	   portion of a given size, attached to ud_minidrv_data
    382  *	-> Invoke the ->init() callout for the minidriver
    383  *	-> If successful, register a new video4linux device and
    384  *	   start accepting V4L API requests on it
    385  *
    386  * usbcam_register() depends on variables defined by the
    387  * DEFINE_USBCAM_MODPARAMS macro to function correctly.  This defines
    388  * a set of static variables and marks them as module parameters.  These
    389  * include:
    390  *	-> video_nr: Favored video4linux minor numbers
    391  *	-> debug: A pointer to the debug level variable
    392  */
    393 
    394 extern int usbcam_register_mod(usbcam_minidrv_t **driverpp,
    395 			       int mdrv_version, const char *mdrv_verx,
    396 			       const struct usbcam_dev_ops *ops,
    397 			       const int dev_priv_size,
    398 			       const struct usb_device_id *id_table,
    399 			       const int *video_nrs, int video_nrs_len,
    400 			       int *debug, struct module *md,
    401 			       const char *modname);
    402 
    403 /*
    404  * usbcam_unregister() will remove a minidriver registration with the
    405  * USB subsystem, and will prepare the minidriver structure to be
    406  * freed.  It is safe to call this API in paths other than minidriver
    407  * module unload, as long as the minidriver is registered.
    408  */
    409 extern void usbcam_unregister(usbcam_minidrv_t *driverp);
    410 
    411 #define DEFINE_USBCAM_MODPARAMS_BASE					\
    412 	static int video_nr_len = 0;					\
    413 	static int video_nr[8];						\
    414 	module_param_array(video_nr, int, &video_nr_len, S_IRUGO|S_IWUSR);    \
    415 	MODULE_PARM_DESC(video_nr, "=n[,n...] Force /dev/videoX IDs");
    416 
    417 #if defined(CONFIG_USB_USBCAM_DEBUG)
    418 #ifndef USBCAM_DEBUG_DEFAULT
    419 #define USBCAM_DEBUG_DEFAULT 0x0020
    420 #endif
    421 #define DEFINE_USBCAM_MODPARAMS 					\
    422 	DEFINE_USBCAM_MODPARAMS_BASE					\
    423 	static int debug = USBCAM_DEBUG_DEFAULT;			\
    424 	module_param(debug, int, S_IRUGO|S_IWUSR);		      	\
    425 	MODULE_PARM_DESC(debug, "Enable debug trace messages");
    426 #define usbcam_register(DRVPP, MDV, VERX, CB, STRUCTSIZE, IDTBL)	\
    427 	usbcam_register_mod((DRVPP), (MDV), (VERX), (CB), (STRUCTSIZE),	\
    428 			    (IDTBL), video_nr, video_nr_len, &debug,	\
    429 			    THIS_MODULE, KBUILD_MODNAME)
    430 #else
    431 #define DEFINE_USBCAM_MODPARAMS 					\
    432 	DEFINE_USBCAM_MODPARAMS_BASE
    433 #define usbcam_register(DRVPP, MDV, VERX, CB, STRUCTSIZE, IDTBL)	\
    434 	usbcam_register_mod((DRVPP), (MDV), (VERX), (CB), (STRUCTSIZE),	\
    435 			    (IDTBL), video_nr, video_nr_len, NULL,	\
    436 			    THIS_MODULE, KBUILD_MODNAME)
    437 #endif
    438 
    439 
    440 /*
    441  * For minidrivers that do not need to do anything other than register
    442  * and unregister themselves as usbcam minidrivers when their modules
    443  * are loaded and unloaded, the DEFINE_USBCAM_MINIDRV_MODULE macro
    444  * is offered.
    445  */
    446 #define DEFINE_USBCAM_MINIDRV_MODULE(VER, VERX, OPS, STRUCTSIZE, IDTBL)	\
    447 	DEFINE_USBCAM_MODPARAMS						\
    448 	static usbcam_minidrv_t *usbcam_minidrv_driver;			\
    449 	static int __init usbcam_minidrv_init(void)			\
    450 	{								\
    451 		return usbcam_register(&usbcam_minidrv_driver,		\
    452 				       (VER), (VERX), (OPS),		\
    453 				       (STRUCTSIZE), (IDTBL));		\
    454 	}								\
    455 	static void __exit usbcam_minidrv_exit(void)			\
    456 	{								\
    457 		usbcam_unregister(usbcam_minidrv_driver);		\
    458 	}								\
    459 	module_init(usbcam_minidrv_init);				\
    460 	module_exit(usbcam_minidrv_exit);
    461 
    462 
    463 /*
    464  * Function for claiming additional interfaces
    465  *
    466  * This may only be called from the minidriver init callout
    467  *
    468  * For devices with multiple interfaces that pertain to a single driver,
    469  * a separate usbcam_dev would ordinarily be created for each interface,
    470  * and the init callout would be invoked.  With this function,
    471  * additional pertinent interfaces can be bound to the base usbcam_dev.
    472  *
    473  * Additional claimed interfaces will be automatically released at
    474  * disconnect time, or in the event of a failure from the init callout.
    475  *
    476  * If the subject interface is already claimed by another USB driver,
    477  * this function will fail with -EBUSY.
    478  */
    479 extern int usbcam_claim_interface(struct usbcam_dev *udp, int ifnum);
    480 
    481 
    482 /*
    483  * Alternate setting choosing helper function
    484  *
    485  * This function assists in choosing an alternate setting based on
    486  * overall bandwidth and packet size requirements for a given pipe.
    487  */
    488 extern int usbcam_choose_altsetting(struct usbcam_dev *udp, int ifnum,
    489 				    int pipe, int bytes_per_sec_min,
    490 				    int pkt_min, int pkt_max,
    491 				    int *altsetting_nr);
    492 
    493 
    494 /*
    495  * CONTROL MANAGEMENT
    496  *
    497  * The control structure is interpreted by the usbcam ioctl handler to
    498  * answer QUERYCTRL/G_CTRL/S_CTRL/QUERYMENU requests.  A minidriver may
    499  * define template controls, and add instances of them per device using
    500  * usbcam_ctrl_add().  Template controls may include additional fields,
    501  * as long as struct usbcam_ctrl is the first member.
    502  *
    503  * This mode of handling control-related ioctls is only used if the
    504  * minidriver does not intercept and handle the appropriate ioctl
    505  * commands in its ioctl callout.
    506  */
    507 struct usbcam_ctrl {
    508 	struct list_head	uc_links;
    509 	struct v4l2_queryctrl	uc_v4l;
    510 
    511 	const char 		**uc_menu_names;
    512 
    513 	/* Retrieves details about the control dynamically, may be NULL */
    514 	int (*query_fn)(struct usbcam_dev *, const struct usbcam_ctrl *,
    515 			struct v4l2_queryctrl *);
    516 
    517 	/* Retrieves the current value of the control */
    518 	int (*get_fn)(struct usbcam_dev *, const struct usbcam_ctrl *,
    519 		      struct v4l2_ext_control *);
    520 
    521 	/* If set_fn is not set, the control is considered read-only. */
    522 	int (*set_fn)(struct usbcam_dev *, const struct usbcam_ctrl *,
    523 		      const struct v4l2_ext_control *);
    524 
    525 	/* Called when a control is being freed, not normally needed */
    526 	void (*release_fn)(struct usbcam_dev *, struct usbcam_ctrl *);
    527 };
    528 
    529 extern struct usbcam_ctrl *usbcam_ctrl_find(struct usbcam_dev *udp, u32 ctlid);
    530 
    531 /* Recommended API: Add a control based on a template */
    532 extern struct usbcam_ctrl *usbcam_ctrl_add_tmpl(struct usbcam_dev *udp,
    533 						const struct usbcam_ctrl *tplp,
    534 						size_t real_size);
    535 
    536 /* Less recommended APIs: allocate, configure, try to add, free on failure */
    537 extern struct usbcam_ctrl *usbcam_ctrl_alloc(size_t real_size);
    538 #define usbcam_ctrl_free(CTRLP) kfree(CTRLP)
    539 extern int usbcam_ctrl_add(struct usbcam_dev *udp,
    540 			   struct usbcam_ctrl *ctrlp);
    541 
    542 
    543 /*
    544  * CURRENT FRAME BUFFER ACCESS
    545  *
    546  * Minidrivers need to get the address to which the current frame
    547  * buffer is mapped, and the allocated size of the frame buffer in
    548  * order to do their job.  The below functions facilitate that.
    549  *
    550  * usbcam_curframe_complete() will cause the current frame buffer to
    551  * be returned to the client application with a successful status.
    552  * The next queued frame buffer will become the current frame buffer.
    553  *
    554  * usbcam_curframe_abortall() will cause all pending frame buffers
    555  * to be aborted and returned to the client application with -EIO.
    556  */
    557 struct usbcam_curframe {
    558 	u8			*uf_base;
    559 	size_t			uf_size;
    560 	enum v4l2_field		uf_field;
    561 	struct timeval		uf_timestamp;
    562 };
    563 
    564 extern int usbcam_curframe_get(struct usbcam_dev *udp,
    565 			       struct usbcam_curframe *cf);
    566 extern void usbcam_curframe_complete_detail(struct usbcam_dev *udp,
    567 					    struct usbcam_curframe *cf);
    568 #define usbcam_curframe_complete(UDP) \
    569 	usbcam_curframe_complete_detail(UDP, NULL)
    570 
    571 extern void usbcam_curframe_abortall(struct usbcam_dev *udp);
    572 
    573 /*
    574  * FRAME FILLERS AND TEST PATTERNS
    575  *
    576  * usbcam_curframe_fill() is a glorified memset: it fills the current
    577  * frame buffer, starting at offs, with nrecs instances of the repeating
    578  * byte sequence pattern/patlen.
    579  *
    580  * It does range checking and will issue printk warnings if the frame
    581  * buffer or the ud_format.sizeimage is overshot.
    582  */
    583 extern void usbcam_curframe_fill(struct usbcam_dev *udp, size_t offset,
    584 				 const void *pattern, int patlen, int nrecs);
    585 
    586 /*
    587  * usbcam_curframe_testpattern() attempts to fill the current frame
    588  * buffer with a blue test pattern.  It paves over any partial frame
    589  * data.  If you wish to display part of an incomplete or interrupted
    590  * frame, don't use this function.  It is used by default to fill in
    591  * frames that are completed with is_error = 1.
    592  *
    593  * This function understands many formats, but there will always be
    594  * exceptions.  To keep the testpattern mechanism consistent in this
    595  * situation, this function will always defer to the 'testpattern'
    596  * function in the minidriver's usbcam_dev_ops, if one is given,
    597  * before attempting to fill the frame itself.
    598  *
    599  * This function is aware of bytesperline and padded line formats.
    600  */
    601 extern int usbcam_curframe_testpattern(struct usbcam_dev *udp);
    602 
    603 
    604 /*
    605  * WORK ITEMS AND THE PER-DEVICE WORKER THREAD
    606  *
    607  * So long as a usbcam video4linux device is open, there is a worker
    608  * thread available to execute deferred minidriver tasks.  The worker
    609  * thread becomes available prior to the open callout issued to the
    610  * minidriver, and the last close of the device will block after the
    611  * minidriver close callout finishes, waiting for the work queue to
    612  * drain.
    613  *
    614  * A work item may be queued from any context, including interrupt
    615  * contexts and URB completion callouts.
    616  *
    617  * Work item callouts are invoked in the context of the worker thread
    618  * with the device mutex held.  When a work item is queued, when the
    619  * worker thread is next idle, it will wait for the device mutex, and
    620  * will start the work item once it acquires the device mutex.  Until
    621  * that point, the work item may be canceled and dequeued using the
    622  * usbcam_work_cancel() API, which may only be called while holding
    623  * the device mutex.
    624  *
    625  * It was decided not to use the core kernel work queue
    626  * implementation for the usbcam work queue because:
    627  * 1. Work item cancelation is seen as a useful feature
    628  * 2. Multithreading is not seen as a useful feature
    629  */
    630 
    631 struct usbcam_workitem;
    632 typedef void (*usbcam_workfunc_t)(struct usbcam_workitem *work);
    633 
    634 struct usbcam_workitem {
    635 	struct list_head	uw_links;
    636 	struct usbcam_dev	*uw_dev;
    637 	usbcam_workfunc_t	uw_func;
    638 };
    639 
    640 extern void usbcam_work_init(struct usbcam_dev *udp,
    641 			     struct usbcam_workitem *wip,
    642 			     usbcam_workfunc_t func);
    643 extern int usbcam_work_queue(struct usbcam_workitem *wip);
    644 extern int usbcam_work_cancel(struct usbcam_workitem *wip);
    645 
    646 struct usbcam_delayedwork {
    647 	struct usbcam_workitem	dw_work;
    648 	struct timer_list	dw_timer;
    649 };
    650 
    651 extern void usbcam_delayedwork_init(struct usbcam_dev *udp,
    652 				    struct usbcam_delayedwork *dwp,
    653 				    usbcam_workfunc_t func);
    654 extern void usbcam_delayedwork_queue(struct usbcam_delayedwork *dwp,
    655 				     unsigned int timeout_ms);
    656 extern int usbcam_delayedwork_cancel(struct usbcam_delayedwork *dwp);
    657 
    658 
    659 /*
    660  * usbcam_work_ref() and usbcam_work_unref() are for managing the start
    661  * and stop of the worker thread.
    662  */
    663 extern int usbcam_work_ref(struct usbcam_dev *udp);
    664 extern void usbcam_work_unref(struct usbcam_dev *udp);
    665 
    666 /*
    667  * usbcam_work_runqueue() will run work queue items in the current context
    668  * until the work queue becomes empty.
    669  */
    670 extern void usbcam_work_runqueue(struct usbcam_dev *udp);
    671 
    672 
    673 /*
    674  * Streaming URB helper structure
    675  *
    676  * Minidrivers create any number of these, assign appropriate endpoints,
    677  * and invoke start.  Completed packets are automatically processed in the
    678  * worker thread.
    679  *
    680  * This infrastructure may only be used when a device is opened, as the
    681  * worker thread is shut down at other times.
    682  */
    683 
    684 extern struct urb *usbcam_urb_alloc(struct usbcam_dev *udp, int pipe,
    685 				    int pktsize, int npkts, int alloc_buf);
    686 extern void usbcam_urb_free(struct urb *urbp, int free_buf);
    687 
    688 
    689 struct usbcam_urbstream;
    690 
    691 struct usbcam_urbstream_ops {
    692 	void (*packet_done)(struct usbcam_dev *, struct usbcam_urbstream *,
    693 			    const u8 *pktdata, int pktlen, int pktstatus);
    694 	void (*submit_error)(struct usbcam_dev *, struct usbcam_urbstream *,
    695 			     int status);
    696 };
    697 
    698 struct usbcam_urbstream {
    699 	struct usbcam_dev			*us_dev;
    700 	char					us_endpoint;
    701 	char					us_streaming;
    702 	char					us_active_goal;
    703 	char					us_active_count;
    704 	const struct usbcam_urbstream_ops	*us_ops;
    705 	spinlock_t				us_lock;
    706 	int					us_timeout_ticks;
    707 	int					us_resubmit_err;
    708 	struct list_head			us_unused_list;
    709 	struct list_head			us_active_list;
    710 	struct list_head			us_complete_list;
    711 	struct completion			us_active_empty;
    712 	struct usbcam_workitem			us_error_workitem;
    713 };
    714 
    715 extern void usbcam_urbstream_init(struct usbcam_urbstream *usp,
    716 				  struct usbcam_dev *udp, int ep);
    717 
    718 extern int usbcam_urbstream_start(struct usbcam_urbstream *);
    719 extern void usbcam_urbstream_stop(struct usbcam_urbstream *, int wait);
    720 extern void usbcam_urbstream_cleanup(struct usbcam_urbstream *usp);
    721 extern int usbcam_urbstream_submit_one(struct usbcam_urbstream *usp);
    722 
    723 extern int usbcam_urbstream_config_iso(struct usbcam_urbstream *usp,
    724 				       const struct usbcam_urbstream_ops *ops,
    725 				       int pktcount, int nreqs, int interval,
    726 				       int pktlen);
    727 
    728 extern int usbcam_urbstream_config_bulk(struct usbcam_urbstream *usp,
    729 					const struct usbcam_urbstream_ops *ops,
    730 					int nreqs, int reqlen, int maxpkt,
    731 					int timeout_ms);
    732 
    733 
    734 /*
    735  * Backward compatibility crap for slightly older 2.6 series kernels
    736  */
    737 
    738 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
    739 #if !defined(V4L2_CTRL_FLAG_NEXT_CTRL)
    740 #define V4L2_CTRL_FLAG_NEXT_CTRL 0
    741 #endif
    742 #if !defined(V4L2_CTRL_FLAG_SLIDER)
    743 #define V4L2_CTRL_FLAG_SLIDER 0
    744 #endif
    745 #if !defined(V4L2_CTRL_FLAG_INACTIVE)
    746 #define V4L2_CTRL_FLAG_INACTIVE 0
    747 #endif
    748 #if !defined(V4L2_CTRL_FLAG_UPDATE)
    749 #define V4L2_CTRL_FLAG_UPDATE 0
    750 #endif
    751 #endif  /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) */
    752 #endif /* __KERNEL__ */
    753 
    754 
    755 #endif /* __USBCAM_H__ */