pwc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* (C) 1999-2003 Nemosoft Unv.
  2. (C) 2004-2006 Luc Saillard (luc@saillard.org)
  3. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  4. driver and thus may have bugs that are not present in the original version.
  5. Please send bug reports and support requests to <luc@saillard.org>.
  6. The decompression routines have been implemented by reverse-engineering the
  7. Nemosoft binary pwcx module. Caveat emptor.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef PWC_H
  21. #define PWC_H
  22. #include <linux/module.h>
  23. #include <linux/usb.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/wait.h>
  26. #include <linux/mutex.h>
  27. #include <linux/mm.h>
  28. #include <linux/slab.h>
  29. #include <asm/errno.h>
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/videobuf2-vmalloc.h>
  35. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  36. #include <linux/input.h>
  37. #endif
  38. #include <media/pwc-ioctl.h>
  39. /* Version block */
  40. #define PWC_VERSION "10.0.15"
  41. #define PWC_NAME "pwc"
  42. #define PFX PWC_NAME ": "
  43. /* Trace certain actions in the driver */
  44. #define PWC_DEBUG_LEVEL_MODULE (1<<0)
  45. #define PWC_DEBUG_LEVEL_PROBE (1<<1)
  46. #define PWC_DEBUG_LEVEL_OPEN (1<<2)
  47. #define PWC_DEBUG_LEVEL_READ (1<<3)
  48. #define PWC_DEBUG_LEVEL_MEMORY (1<<4)
  49. #define PWC_DEBUG_LEVEL_FLOW (1<<5)
  50. #define PWC_DEBUG_LEVEL_SIZE (1<<6)
  51. #define PWC_DEBUG_LEVEL_IOCTL (1<<7)
  52. #define PWC_DEBUG_LEVEL_TRACE (1<<8)
  53. #define PWC_DEBUG_MODULE(fmt, args...) PWC_DEBUG(MODULE, fmt, ##args)
  54. #define PWC_DEBUG_PROBE(fmt, args...) PWC_DEBUG(PROBE, fmt, ##args)
  55. #define PWC_DEBUG_OPEN(fmt, args...) PWC_DEBUG(OPEN, fmt, ##args)
  56. #define PWC_DEBUG_READ(fmt, args...) PWC_DEBUG(READ, fmt, ##args)
  57. #define PWC_DEBUG_MEMORY(fmt, args...) PWC_DEBUG(MEMORY, fmt, ##args)
  58. #define PWC_DEBUG_FLOW(fmt, args...) PWC_DEBUG(FLOW, fmt, ##args)
  59. #define PWC_DEBUG_SIZE(fmt, args...) PWC_DEBUG(SIZE, fmt, ##args)
  60. #define PWC_DEBUG_IOCTL(fmt, args...) PWC_DEBUG(IOCTL, fmt, ##args)
  61. #define PWC_DEBUG_TRACE(fmt, args...) PWC_DEBUG(TRACE, fmt, ##args)
  62. #ifdef CONFIG_USB_PWC_DEBUG
  63. #define PWC_DEBUG_LEVEL (PWC_DEBUG_LEVEL_MODULE)
  64. #define PWC_DEBUG(level, fmt, args...) do {\
  65. if ((PWC_DEBUG_LEVEL_ ##level) & pwc_trace) \
  66. printk(KERN_DEBUG PFX fmt, ##args); \
  67. } while (0)
  68. #define PWC_ERROR(fmt, args...) printk(KERN_ERR PFX fmt, ##args)
  69. #define PWC_WARNING(fmt, args...) printk(KERN_WARNING PFX fmt, ##args)
  70. #define PWC_INFO(fmt, args...) printk(KERN_INFO PFX fmt, ##args)
  71. #define PWC_TRACE(fmt, args...) PWC_DEBUG(TRACE, fmt, ##args)
  72. #else /* if ! CONFIG_USB_PWC_DEBUG */
  73. #define PWC_ERROR(fmt, args...) printk(KERN_ERR PFX fmt, ##args)
  74. #define PWC_WARNING(fmt, args...) printk(KERN_WARNING PFX fmt, ##args)
  75. #define PWC_INFO(fmt, args...) printk(KERN_INFO PFX fmt, ##args)
  76. #define PWC_TRACE(fmt, args...) do { } while(0)
  77. #define PWC_DEBUG(level, fmt, args...) do { } while(0)
  78. #define pwc_trace 0
  79. #endif
  80. /* Defines for ToUCam cameras */
  81. #define TOUCAM_HEADER_SIZE 8
  82. #define TOUCAM_TRAILER_SIZE 4
  83. #define FEATURE_MOTOR_PANTILT 0x0001
  84. #define FEATURE_CODEC1 0x0002
  85. #define FEATURE_CODEC2 0x0004
  86. /* Ignore errors in the first N frames, to allow for startup delays */
  87. #define FRAME_LOWMARK 5
  88. /* Size and number of buffers for the ISO pipe. */
  89. #define MAX_ISO_BUFS 3
  90. #define ISO_FRAMES_PER_DESC 10
  91. #define ISO_MAX_FRAME_SIZE 960
  92. #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
  93. /* Maximum size after decompression is 640x480 YUV data, 1.5 * 640 * 480 */
  94. #define PWC_FRAME_SIZE (460800 + TOUCAM_HEADER_SIZE + TOUCAM_TRAILER_SIZE)
  95. /* Absolute minimum and maximum number of buffers available for mmap() */
  96. #define MIN_FRAMES 2
  97. #define MAX_FRAMES 16
  98. /* Some macros to quickly find the type of a webcam */
  99. #define DEVICE_USE_CODEC1(x) ((x)<675)
  100. #define DEVICE_USE_CODEC2(x) ((x)>=675 && (x)<700)
  101. #define DEVICE_USE_CODEC3(x) ((x)>=700)
  102. #define DEVICE_USE_CODEC23(x) ((x)>=675)
  103. /* from pwc-dec.h */
  104. #define PWCX_FLAG_PLANAR 0x0001
  105. /* Request types: video */
  106. #define SET_LUM_CTL 0x01
  107. #define GET_LUM_CTL 0x02
  108. #define SET_CHROM_CTL 0x03
  109. #define GET_CHROM_CTL 0x04
  110. #define SET_STATUS_CTL 0x05
  111. #define GET_STATUS_CTL 0x06
  112. #define SET_EP_STREAM_CTL 0x07
  113. #define GET_EP_STREAM_CTL 0x08
  114. #define GET_XX_CTL 0x09
  115. #define SET_XX_CTL 0x0A
  116. #define GET_XY_CTL 0x0B
  117. #define SET_XY_CTL 0x0C
  118. #define SET_MPT_CTL 0x0D
  119. #define GET_MPT_CTL 0x0E
  120. /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
  121. #define AGC_MODE_FORMATTER 0x2000
  122. #define PRESET_AGC_FORMATTER 0x2100
  123. #define SHUTTER_MODE_FORMATTER 0x2200
  124. #define PRESET_SHUTTER_FORMATTER 0x2300
  125. #define PRESET_CONTOUR_FORMATTER 0x2400
  126. #define AUTO_CONTOUR_FORMATTER 0x2500
  127. #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
  128. #define CONTRAST_FORMATTER 0x2700
  129. #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
  130. #define FLICKERLESS_MODE_FORMATTER 0x2900
  131. #define AE_CONTROL_SPEED 0x2A00
  132. #define BRIGHTNESS_FORMATTER 0x2B00
  133. #define GAMMA_FORMATTER 0x2C00
  134. /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
  135. #define WB_MODE_FORMATTER 0x1000
  136. #define AWB_CONTROL_SPEED_FORMATTER 0x1100
  137. #define AWB_CONTROL_DELAY_FORMATTER 0x1200
  138. #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
  139. #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
  140. #define COLOUR_MODE_FORMATTER 0x1500
  141. #define SATURATION_MODE_FORMATTER1 0x1600
  142. #define SATURATION_MODE_FORMATTER2 0x1700
  143. /* Selectors for the Status controls [GS]ET_STATUS_CTL */
  144. #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
  145. #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
  146. #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
  147. #define READ_AGC_FORMATTER 0x0500
  148. #define READ_SHUTTER_FORMATTER 0x0600
  149. #define READ_RED_GAIN_FORMATTER 0x0700
  150. #define READ_BLUE_GAIN_FORMATTER 0x0800
  151. /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
  152. #define PT_RELATIVE_CONTROL_FORMATTER 0x01
  153. #define PT_RESET_CONTROL_FORMATTER 0x02
  154. #define PT_STATUS_FORMATTER 0x03
  155. /* intermediate buffers with raw data from the USB cam */
  156. struct pwc_frame_buf
  157. {
  158. struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
  159. struct list_head list;
  160. void *data;
  161. int filled; /* number of bytes filled */
  162. };
  163. struct pwc_device
  164. {
  165. struct video_device vdev;
  166. struct mutex modlock;
  167. /* Pointer to our usb_device, may be NULL after unplug */
  168. struct usb_device *udev;
  169. /* Protects the setting of udev to NULL by our disconnect handler */
  170. struct mutex udevlock;
  171. /* type of cam (645, 646, 675, 680, 690, 720, 730, 740, 750) */
  172. int type;
  173. int release; /* release number */
  174. int features; /* feature bits */
  175. char serial[30]; /* serial number (string) */
  176. /*** Video data ***/
  177. struct file *capt_file; /* file doing video capture */
  178. int vendpoint; /* video isoc endpoint */
  179. int vcinterface; /* video control interface */
  180. int valternate; /* alternate interface needed */
  181. int vframes, vsize; /* frames-per-second & size (see PSZ_*) */
  182. int pixfmt; /* pixelformat: V4L2_PIX_FMT_YUV420 or _PWCX */
  183. int vframe_count; /* received frames */
  184. int vmax_packet_size; /* USB maxpacket size */
  185. int vlast_packet_size; /* for frame synchronisation */
  186. int visoc_errors; /* number of contiguous ISOC errors */
  187. int vcompression; /* desired compression factor */
  188. int vbandlength; /* compressed band length; 0 is uncompressed */
  189. char vsnapshot; /* snapshot mode */
  190. char vsync; /* used by isoc handler */
  191. char vmirror; /* for ToUCaM series */
  192. char power_save; /* Do powersaving for this cam */
  193. int cmd_len;
  194. unsigned char cmd_buf[13];
  195. struct urb *urbs[MAX_ISO_BUFS];
  196. char iso_init;
  197. /* videobuf2 queue and queued buffers list */
  198. struct vb2_queue vb_queue;
  199. struct list_head queued_bufs;
  200. spinlock_t queued_bufs_lock;
  201. /*
  202. * Frame currently being filled, this only gets touched by the
  203. * isoc urb complete handler, and by stream start / stop since
  204. * start / stop touch it before / after starting / killing the urbs
  205. * no locking is needed around this
  206. */
  207. struct pwc_frame_buf *fill_buf;
  208. int frame_header_size, frame_trailer_size;
  209. int frame_size;
  210. int frame_total_size; /* including header & trailer */
  211. int drop_frames;
  212. void *decompress_data; /* private data for decompression engine */
  213. /*
  214. * We have an 'image' and a 'view', where 'image' is the fixed-size img
  215. * as delivered by the camera, and 'view' is the size requested by the
  216. * program. The camera image is centered in this viewport, laced with
  217. * a gray or black border. view_min <= image <= view <= view_max;
  218. */
  219. int image_mask; /* supported sizes */
  220. struct pwc_coord view_min, view_max; /* minimum and maximum view */
  221. struct pwc_coord abs_max; /* maximum supported size */
  222. struct pwc_coord image, view; /* image and viewport size */
  223. struct pwc_coord offset; /* offset of the viewport */
  224. /*** motorized pan/tilt feature */
  225. struct pwc_mpt_range angle_range;
  226. int pan_angle; /* in degrees * 100 */
  227. int tilt_angle; /* absolute angle; 0,0 is home */
  228. /*
  229. * Set to 1 when the user push the button, reset to 0
  230. * when this value is read from sysfs.
  231. */
  232. int snapshot_button_status;
  233. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  234. struct input_dev *button_dev; /* webcam snapshot button input */
  235. char button_phys[64];
  236. #endif
  237. /* controls */
  238. struct v4l2_ctrl_handler ctrl_handler;
  239. u16 saturation_fmt;
  240. struct v4l2_ctrl *brightness;
  241. struct v4l2_ctrl *contrast;
  242. struct v4l2_ctrl *saturation;
  243. struct v4l2_ctrl *gamma;
  244. struct {
  245. /* awb / red-blue balance cluster */
  246. struct v4l2_ctrl *auto_white_balance;
  247. struct v4l2_ctrl *red_balance;
  248. struct v4l2_ctrl *blue_balance;
  249. /* usb ctrl transfers are slow, so we cache things */
  250. int color_bal_valid;
  251. unsigned long last_color_bal_update; /* In jiffies */
  252. s32 last_red_balance;
  253. s32 last_blue_balance;
  254. };
  255. struct {
  256. /* autogain / gain cluster */
  257. struct v4l2_ctrl *autogain;
  258. struct v4l2_ctrl *gain;
  259. int gain_valid;
  260. unsigned long last_gain_update; /* In jiffies */
  261. s32 last_gain;
  262. };
  263. struct {
  264. /* exposure_auto / exposure cluster */
  265. struct v4l2_ctrl *exposure_auto;
  266. struct v4l2_ctrl *exposure;
  267. int exposure_valid;
  268. unsigned long last_exposure_update; /* In jiffies */
  269. s32 last_exposure;
  270. };
  271. struct v4l2_ctrl *colorfx;
  272. struct {
  273. /* autocontour/contour cluster */
  274. struct v4l2_ctrl *autocontour;
  275. struct v4l2_ctrl *contour;
  276. };
  277. struct v4l2_ctrl *backlight;
  278. struct v4l2_ctrl *flicker;
  279. struct v4l2_ctrl *noise_reduction;
  280. struct v4l2_ctrl *save_user;
  281. struct v4l2_ctrl *restore_user;
  282. struct v4l2_ctrl *restore_factory;
  283. struct {
  284. /* motor control cluster */
  285. struct v4l2_ctrl *motor_pan;
  286. struct v4l2_ctrl *motor_tilt;
  287. struct v4l2_ctrl *motor_pan_reset;
  288. struct v4l2_ctrl *motor_tilt_reset;
  289. };
  290. /* CODEC3 models have both gain and exposure controlled by autogain */
  291. struct v4l2_ctrl *autogain_expo_cluster[3];
  292. };
  293. /* Global variables */
  294. #ifdef CONFIG_USB_PWC_DEBUG
  295. extern int pwc_trace;
  296. #endif
  297. /** Functions in pwc-misc.c */
  298. /* sizes in pixels */
  299. extern const struct pwc_coord pwc_image_sizes[PSZ_MAX];
  300. int pwc_decode_size(struct pwc_device *pdev, int width, int height);
  301. void pwc_construct(struct pwc_device *pdev);
  302. /** Functions in pwc-ctrl.c */
  303. /* Request a certain video mode. Returns < 0 if not possible */
  304. extern int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot);
  305. extern unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size);
  306. extern int pwc_mpt_reset(struct pwc_device *pdev, int flags);
  307. extern int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt);
  308. extern int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value);
  309. extern int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor);
  310. extern int send_control_msg(struct pwc_device *pdev,
  311. u8 request, u16 value, void *buf, int buflen);
  312. /* Control get / set helpers */
  313. int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data);
  314. int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data);
  315. int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data);
  316. #define pwc_set_s8_ctrl pwc_set_u8_ctrl
  317. int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *dat);
  318. int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data);
  319. int pwc_button_ctrl(struct pwc_device *pdev, u16 value);
  320. int pwc_init_controls(struct pwc_device *pdev);
  321. /* Power down or up the camera; not supported by all models */
  322. extern void pwc_camera_power(struct pwc_device *pdev, int power);
  323. /* Private ioctl()s; see pwc-ioctl.h */
  324. extern long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg);
  325. extern const struct v4l2_ioctl_ops pwc_ioctl_ops;
  326. /** pwc-uncompress.c */
  327. /* Expand frame to image, possibly including decompression. Uses read_frame and fill_image */
  328. int pwc_decompress(struct pwc_device *pdev, struct pwc_frame_buf *fbuf);
  329. #endif