w9968cf.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /***************************************************************************
  2. * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
  3. * *
  4. * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  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., 675 Mass Ave, Cambridge, MA 02139, USA. *
  19. ***************************************************************************/
  20. #ifndef _W9968CF_H_
  21. #define _W9968CF_H_
  22. #include <linux/videodev2.h>
  23. #include <linux/usb.h>
  24. #include <linux/i2c.h>
  25. #include <linux/device.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/list.h>
  28. #include <linux/wait.h>
  29. #include <linux/param.h>
  30. #include <linux/types.h>
  31. #include <linux/rwsem.h>
  32. #include <linux/mutex.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/ovcamchip.h>
  35. #include "w9968cf_vpp.h"
  36. /****************************************************************************
  37. * Default values *
  38. ****************************************************************************/
  39. #define W9968CF_VPPMOD_LOAD 1 /* automatic 'w9968cf-vpp' module loading */
  40. /* Comment/uncomment the following line to enable/disable debugging messages */
  41. #define W9968CF_DEBUG
  42. /* These have effect only if W9968CF_DEBUG is defined */
  43. #define W9968CF_DEBUG_LEVEL 2 /* from 0 to 6. 0 for no debug informations */
  44. #define W9968CF_SPECIFIC_DEBUG 0 /* 0 or 1 */
  45. #define W9968CF_MAX_DEVICES 32
  46. #define W9968CF_SIMCAMS W9968CF_MAX_DEVICES /* simultaneous cameras */
  47. #define W9968CF_MAX_BUFFERS 32
  48. #define W9968CF_BUFFERS 2 /* n. of frame buffers from 2 to MAX_BUFFERS */
  49. /* Maximum data payload sizes in bytes for alternate settings */
  50. static const u16 wMaxPacketSize[] = {1023, 959, 895, 831, 767, 703, 639, 575,
  51. 511, 447, 383, 319, 255, 191, 127, 63};
  52. #define W9968CF_PACKET_SIZE 1023 /* according to wMaxPacketSizes[] */
  53. #define W9968CF_MIN_PACKET_SIZE 63 /* minimum value */
  54. #define W9968CF_ISO_PACKETS 5 /* n.of packets for isochronous transfers */
  55. #define W9968CF_USB_CTRL_TIMEOUT 1000 /* timeout (ms) for usb control commands */
  56. #define W9968CF_URBS 2 /* n. of scheduled URBs for ISO transfer */
  57. #define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */
  58. #define W9968CF_I2C_RW_RETRIES 15 /* number of max I2C r/w retries */
  59. /* Available video formats */
  60. struct w9968cf_format {
  61. const u16 palette;
  62. const u16 depth;
  63. const u8 compression;
  64. };
  65. static const struct w9968cf_format w9968cf_formatlist[] = {
  66. { VIDEO_PALETTE_UYVY, 16, 0 }, /* original video */
  67. { VIDEO_PALETTE_YUV422P, 16, 1 }, /* with JPEG compression */
  68. { VIDEO_PALETTE_YUV420P, 12, 1 }, /* with JPEG compression */
  69. { VIDEO_PALETTE_YUV420, 12, 1 }, /* same as YUV420P */
  70. { VIDEO_PALETTE_YUYV, 16, 0 }, /* software conversion */
  71. { VIDEO_PALETTE_YUV422, 16, 0 }, /* software conversion */
  72. { VIDEO_PALETTE_GREY, 8, 0 }, /* software conversion */
  73. { VIDEO_PALETTE_RGB555, 16, 0 }, /* software conversion */
  74. { VIDEO_PALETTE_RGB565, 16, 0 }, /* software conversion */
  75. { VIDEO_PALETTE_RGB24, 24, 0 }, /* software conversion */
  76. { VIDEO_PALETTE_RGB32, 32, 0 }, /* software conversion */
  77. { 0, 0, 0 } /* 0 is a terminating entry */
  78. };
  79. #define W9968CF_DECOMPRESSION 2 /* decomp:0=disable,1=force,2=any formats */
  80. #define W9968CF_PALETTE_DECOMP_OFF VIDEO_PALETTE_UYVY /* when decomp=0 */
  81. #define W9968CF_PALETTE_DECOMP_FORCE VIDEO_PALETTE_YUV420P /* when decomp=1 */
  82. #define W9968CF_PALETTE_DECOMP_ON VIDEO_PALETTE_UYVY /* when decomp=2 */
  83. #define W9968CF_FORCE_RGB 0 /* read RGB instead of BGR, yes=1/no=0 */
  84. #define W9968CF_MAX_WIDTH 800 /* Has effect if up-scaling is on */
  85. #define W9968CF_MAX_HEIGHT 600 /* Has effect if up-scaling is on */
  86. #define W9968CF_WIDTH 320 /* from 128 to 352, multiple of 16 */
  87. #define W9968CF_HEIGHT 240 /* from 96 to 288, multiple of 16 */
  88. #define W9968CF_CLAMPING 0 /* 0 disable, 1 enable video data clamping */
  89. #define W9968CF_FILTER_TYPE 0 /* 0 disable 1 (1-2-1), 2 (2-3-6-3-2) */
  90. #define W9968CF_DOUBLE_BUFFER 1 /* 0 disable, 1 enable double buffer */
  91. #define W9968CF_LARGEVIEW 1 /* 0 disable, 1 enable */
  92. #define W9968CF_UPSCALING 0 /* 0 disable, 1 enable */
  93. #define W9968CF_MONOCHROME 0 /* 0 not monochrome, 1 monochrome sensor */
  94. #define W9968CF_BRIGHTNESS 31000 /* from 0 to 65535 */
  95. #define W9968CF_HUE 32768 /* from 0 to 65535 */
  96. #define W9968CF_COLOUR 32768 /* from 0 to 65535 */
  97. #define W9968CF_CONTRAST 50000 /* from 0 to 65535 */
  98. #define W9968CF_WHITENESS 32768 /* from 0 to 65535 */
  99. #define W9968CF_AUTOBRIGHT 0 /* 0 disable, 1 enable automatic brightness */
  100. #define W9968CF_AUTOEXP 1 /* 0 disable, 1 enable automatic exposure */
  101. #define W9968CF_LIGHTFREQ 50 /* light frequency. 50Hz (Europe) or 60Hz */
  102. #define W9968CF_BANDINGFILTER 0 /* 0 disable, 1 enable banding filter */
  103. #define W9968CF_BACKLIGHT 0 /* 0 or 1, 1=object is lit from behind */
  104. #define W9968CF_MIRROR 0 /* 0 or 1 [don't] reverse image horizontally*/
  105. #define W9968CF_CLOCKDIV -1 /* -1 = automatic clock divisor */
  106. #define W9968CF_DEF_CLOCKDIVISOR 0 /* default sensor clock divisor value */
  107. /****************************************************************************
  108. * Globals *
  109. ****************************************************************************/
  110. #define W9968CF_MODULE_NAME "V4L driver for W996[87]CF JPEG USB " \
  111. "Dual Mode Camera Chip"
  112. #define W9968CF_MODULE_VERSION "1:1.34-basic"
  113. #define W9968CF_MODULE_AUTHOR "(C) 2002-2004 Luca Risolia"
  114. #define W9968CF_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
  115. #define W9968CF_MODULE_LICENSE "GPL"
  116. static const struct usb_device_id winbond_id_table[] = {
  117. {
  118. /* Creative Labs Video Blaster WebCam Go Plus */
  119. USB_DEVICE(0x041e, 0x4003),
  120. .driver_info = (unsigned long)"w9968cf",
  121. },
  122. {
  123. /* Generic W996[87]CF JPEG USB Dual Mode Camera */
  124. USB_DEVICE(0x1046, 0x9967),
  125. .driver_info = (unsigned long)"w9968cf",
  126. },
  127. { } /* terminating entry */
  128. };
  129. /* W996[87]CF camera models, internal ids: */
  130. enum w9968cf_model_id {
  131. W9968CF_MOD_GENERIC = 1, /* Generic W996[87]CF based device */
  132. W9968CF_MOD_CLVBWGP = 11,/*Creative Labs Video Blaster WebCam Go Plus*/
  133. W9968CF_MOD_ADPVDMA = 21, /* Aroma Digi Pen VGA Dual Mode ADG-5000 */
  134. W9986CF_MOD_AAU = 31, /* AVerMedia AVerTV USB */
  135. W9968CF_MOD_CLVBWG = 34, /* Creative Labs Video Blaster WebCam Go */
  136. W9968CF_MOD_LL = 37, /* Lebon LDC-035A */
  137. W9968CF_MOD_EEEMC = 40, /* Ezonics EZ-802 EZMega Cam */
  138. W9968CF_MOD_OOE = 42, /* OmniVision OV8610-EDE */
  139. W9968CF_MOD_ODPVDMPC = 43,/* OPCOM Digi Pen VGA Dual Mode Pen Camera */
  140. W9968CF_MOD_PDPII = 46, /* Pretec Digi Pen-II */
  141. W9968CF_MOD_PDP480 = 49, /* Pretec DigiPen-480 */
  142. };
  143. enum w9968cf_frame_status {
  144. F_READY, /* finished grabbing & ready to be read/synced */
  145. F_GRABBING, /* in the process of being grabbed into */
  146. F_ERROR, /* something bad happened while processing */
  147. F_UNUSED /* unused (no VIDIOCMCAPTURE) */
  148. };
  149. struct w9968cf_frame_t {
  150. void* buffer;
  151. unsigned long size;
  152. u32 length;
  153. int number;
  154. enum w9968cf_frame_status status;
  155. struct w9968cf_frame_t* next;
  156. u8 queued;
  157. };
  158. enum w9968cf_vpp_flag {
  159. VPP_NONE = 0x00,
  160. VPP_UPSCALE = 0x01,
  161. VPP_SWAP_YUV_BYTES = 0x02,
  162. VPP_DECOMPRESSION = 0x04,
  163. VPP_UYVY_TO_RGBX = 0x08,
  164. };
  165. /* Main device driver structure */
  166. struct w9968cf_device {
  167. enum w9968cf_model_id id; /* private device identifier */
  168. struct v4l2_device v4l2_dev;
  169. struct video_device* v4ldev; /* -> V4L structure */
  170. struct list_head v4llist; /* entry of the list of V4L cameras */
  171. struct usb_device* usbdev; /* -> main USB structure */
  172. struct urb* urb[W9968CF_URBS]; /* -> USB request block structs */
  173. void* transfer_buffer[W9968CF_URBS]; /* -> ISO transfer buffers */
  174. u16* control_buffer; /* -> buffer for control req.*/
  175. u16* data_buffer; /* -> data to send to the FSB */
  176. struct w9968cf_frame_t frame[W9968CF_MAX_BUFFERS];
  177. struct w9968cf_frame_t frame_tmp; /* temporary frame */
  178. struct w9968cf_frame_t frame_vpp; /* helper frame.*/
  179. struct w9968cf_frame_t* frame_current; /* -> frame being grabbed */
  180. struct w9968cf_frame_t* requested_frame[W9968CF_MAX_BUFFERS];
  181. u8 max_buffers, /* number of requested buffers */
  182. force_palette, /* yes=1/no=0 */
  183. force_rgb, /* read RGB instead of BGR, yes=1, no=0 */
  184. double_buffer, /* hardware double buffering yes=1/no=0 */
  185. clamping, /* video data clamping yes=1/no=0 */
  186. filter_type, /* 0=disabled, 1=3 tap, 2=5 tap filter */
  187. capture, /* 0=disabled, 1=enabled */
  188. largeview, /* 0=disabled, 1=enabled */
  189. decompression, /* 0=disabled, 1=forced, 2=allowed */
  190. upscaling; /* software image scaling, 0=enabled, 1=disabled */
  191. struct video_picture picture; /* current picture settings */
  192. struct video_window window; /* current window settings */
  193. u16 hw_depth, /* depth (used by the chip) */
  194. hw_palette, /* palette (used by the chip) */
  195. hw_width, /* width (used by the chip) */
  196. hw_height, /* height (used by the chip) */
  197. hs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
  198. vs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
  199. start_cropx, /* pixels from HS inactive edge to 1st cropped pixel*/
  200. start_cropy; /* pixels from VS inactive edge to 1st cropped pixel*/
  201. enum w9968cf_vpp_flag vpp_flag; /* post-processing routines in use */
  202. u8 nbuffers, /* number of allocated frame buffers */
  203. altsetting, /* camera alternate setting */
  204. disconnected, /* flag: yes=1, no=0 */
  205. misconfigured, /* flag: yes=1, no=0 */
  206. users, /* flag: number of users holding the device */
  207. streaming; /* flag: yes=1, no=0 */
  208. u8 sensor_initialized; /* flag: yes=1, no=0 */
  209. /* Determined by the image sensor type: */
  210. int sensor, /* type of image sensor chip (CC_*) */
  211. monochrome; /* image sensor is (probably) monochrome */
  212. u16 maxwidth, /* maximum width supported by the image sensor */
  213. maxheight, /* maximum height supported by the image sensor */
  214. minwidth, /* minimum width supported by the image sensor */
  215. minheight; /* minimum height supported by the image sensor */
  216. u8 auto_brt, /* auto brightness enabled flag */
  217. auto_exp, /* auto exposure enabled flag */
  218. backlight, /* backlight exposure algorithm flag */
  219. mirror, /* image is reversed horizontally */
  220. lightfreq, /* power (lighting) frequency */
  221. bandfilt; /* banding filter enabled flag */
  222. s8 clockdiv; /* clock divisor */
  223. /* I2C interface to kernel */
  224. struct i2c_adapter i2c_adapter;
  225. struct v4l2_subdev *sensor_sd;
  226. /* Locks */
  227. struct mutex dev_mutex, /* for probe, disconnect,open and close */
  228. fileop_mutex; /* for read and ioctl */
  229. spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */
  230. flist_lock; /* for requested frame list accesses */
  231. wait_queue_head_t open, wait_queue;
  232. char command[16]; /* name of the program holding the device */
  233. };
  234. static inline struct w9968cf_device *to_cam(struct v4l2_device *v4l2_dev)
  235. {
  236. return container_of(v4l2_dev, struct w9968cf_device, v4l2_dev);
  237. }
  238. /****************************************************************************
  239. * Macros for debugging *
  240. ****************************************************************************/
  241. #undef DBG
  242. #undef KDBG
  243. #ifdef W9968CF_DEBUG
  244. /* For device specific debugging messages */
  245. # define DBG(level, fmt, args...) \
  246. { \
  247. if ( ((specific_debug) && (debug == (level))) || \
  248. ((!specific_debug) && (debug >= (level))) ) { \
  249. if ((level) == 1) \
  250. v4l2_err(&cam->v4l2_dev, fmt "\n", ## args); \
  251. else if ((level) == 2 || (level) == 3) \
  252. v4l2_info(&cam->v4l2_dev, fmt "\n", ## args); \
  253. else if ((level) == 4) \
  254. v4l2_warn(&cam->v4l2_dev, fmt "\n", ## args); \
  255. else if ((level) >= 5) \
  256. v4l2_info(&cam->v4l2_dev, "[%s:%d] " fmt "\n", \
  257. __func__, __LINE__ , ## args); \
  258. } \
  259. }
  260. /* For generic kernel (not device specific) messages */
  261. # define KDBG(level, fmt, args...) \
  262. { \
  263. if ( ((specific_debug) && (debug == (level))) || \
  264. ((!specific_debug) && (debug >= (level))) ) { \
  265. if ((level) >= 1 && (level) <= 4) \
  266. pr_info("w9968cf: " fmt "\n", ## args); \
  267. else if ((level) >= 5) \
  268. pr_debug("w9968cf: [%s:%d] " fmt "\n", __func__, \
  269. __LINE__ , ## args); \
  270. } \
  271. }
  272. #else
  273. /* Not debugging: nothing */
  274. # define DBG(level, fmt, args...) do {;} while(0);
  275. # define KDBG(level, fmt, args...) do {;} while(0);
  276. #endif
  277. #undef PDBG
  278. #define PDBG(fmt, args...) \
  279. v4l2_info(&cam->v4l2_dev, "[%s:%d] " fmt "\n", __func__, __LINE__ , ## args);
  280. #undef PDBGG
  281. #define PDBGG(fmt, args...) do {;} while(0); /* nothing: it's a placeholder */
  282. #endif /* _W9968CF_H_ */