zr364xx.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /*
  2. * Zoran 364xx based USB webcam module version 0.73
  3. *
  4. * Allows you to use your USB webcam with V4L2 applications
  5. * This is still in heavy developpement !
  6. *
  7. * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
  8. * http://royale.zerezo.com/zr364xx/
  9. *
  10. * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
  11. * V4L2 version inspired by meye.c driver
  12. *
  13. * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/version.h>
  31. #include <linux/init.h>
  32. #include <linux/usb.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/slab.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/highmem.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include <media/videobuf-vmalloc.h>
  40. /* Version Information */
  41. #define DRIVER_VERSION "v0.73"
  42. #define ZR364XX_VERSION_CODE KERNEL_VERSION(0, 7, 3)
  43. #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
  44. #define DRIVER_DESC "Zoran 364xx"
  45. /* Camera */
  46. #define FRAMES 1
  47. #define MAX_FRAME_SIZE 200000
  48. #define BUFFER_SIZE 0x1000
  49. #define CTRL_TIMEOUT 500
  50. #define ZR364XX_DEF_BUFS 4
  51. #define ZR364XX_READ_IDLE 0
  52. #define ZR364XX_READ_FRAME 1
  53. /* Debug macro */
  54. #define DBG(fmt, args...) \
  55. do { \
  56. if (debug) { \
  57. printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
  58. } \
  59. } while (0)
  60. /*#define FULL_DEBUG 1*/
  61. #ifdef FULL_DEBUG
  62. #define _DBG DBG
  63. #else
  64. #define _DBG(fmt, args...)
  65. #endif
  66. /* Init methods, need to find nicer names for these
  67. * the exact names of the chipsets would be the best if someone finds it */
  68. #define METHOD0 0
  69. #define METHOD1 1
  70. #define METHOD2 2
  71. /* Module parameters */
  72. static int debug;
  73. static int mode;
  74. /* Module parameters interface */
  75. module_param(debug, int, 0644);
  76. MODULE_PARM_DESC(debug, "Debug level");
  77. module_param(mode, int, 0644);
  78. MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
  79. /* Devices supported by this driver
  80. * .driver_info contains the init method used by the camera */
  81. static struct usb_device_id device_table[] = {
  82. {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
  83. {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
  84. {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
  85. {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
  86. {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
  87. {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
  88. {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
  89. {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
  90. {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
  91. {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
  92. {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
  93. {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
  94. {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
  95. {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
  96. {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
  97. {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
  98. {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
  99. {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
  100. {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
  101. {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD2 },
  102. {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
  103. {} /* Terminating entry */
  104. };
  105. MODULE_DEVICE_TABLE(usb, device_table);
  106. struct zr364xx_mode {
  107. u32 color; /* output video color format */
  108. u32 brightness; /* brightness */
  109. };
  110. /* frame structure */
  111. struct zr364xx_framei {
  112. unsigned long ulState; /* ulState:ZR364XX_READ_IDLE,
  113. ZR364XX_READ_FRAME */
  114. void *lpvbits; /* image data */
  115. unsigned long cur_size; /* current data copied to it */
  116. };
  117. /* image buffer structure */
  118. struct zr364xx_bufferi {
  119. unsigned long dwFrames; /* number of frames in buffer */
  120. struct zr364xx_framei frame[FRAMES]; /* array of FRAME structures */
  121. };
  122. struct zr364xx_dmaqueue {
  123. struct list_head active;
  124. struct zr364xx_camera *cam;
  125. };
  126. struct zr364xx_pipeinfo {
  127. u32 transfer_size;
  128. u8 *transfer_buffer;
  129. u32 state;
  130. void *stream_urb;
  131. void *cam; /* back pointer to zr364xx_camera struct */
  132. u32 err_count;
  133. u32 idx;
  134. };
  135. struct zr364xx_fmt {
  136. char *name;
  137. u32 fourcc;
  138. int depth;
  139. };
  140. /* image formats. */
  141. static const struct zr364xx_fmt formats[] = {
  142. {
  143. .name = "JPG",
  144. .fourcc = V4L2_PIX_FMT_JPEG,
  145. .depth = 24
  146. }
  147. };
  148. /* Camera stuff */
  149. struct zr364xx_camera {
  150. struct usb_device *udev; /* save off the usb device pointer */
  151. struct usb_interface *interface;/* the interface for this device */
  152. struct video_device *vdev; /* v4l video device */
  153. int nb;
  154. struct zr364xx_bufferi buffer;
  155. int skip;
  156. int width;
  157. int height;
  158. int method;
  159. struct mutex lock;
  160. struct mutex open_lock;
  161. int users;
  162. spinlock_t slock;
  163. struct zr364xx_dmaqueue vidq;
  164. int resources;
  165. int last_frame;
  166. int cur_frame;
  167. unsigned long frame_count;
  168. int b_acquire;
  169. struct zr364xx_pipeinfo pipe[1];
  170. u8 read_endpoint;
  171. const struct zr364xx_fmt *fmt;
  172. struct videobuf_queue vb_vidq;
  173. enum v4l2_buf_type type;
  174. struct zr364xx_mode mode;
  175. };
  176. /* buffer for one video frame */
  177. struct zr364xx_buffer {
  178. /* common v4l buffer stuff -- must be first */
  179. struct videobuf_buffer vb;
  180. const struct zr364xx_fmt *fmt;
  181. };
  182. /* function used to send initialisation commands to the camera */
  183. static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
  184. u16 index, unsigned char *cp, u16 size)
  185. {
  186. int status;
  187. unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
  188. if (!transfer_buffer) {
  189. dev_err(&udev->dev, "kmalloc(%d) failed\n", size);
  190. return -ENOMEM;
  191. }
  192. memcpy(transfer_buffer, cp, size);
  193. status = usb_control_msg(udev,
  194. usb_sndctrlpipe(udev, 0),
  195. request,
  196. USB_DIR_OUT | USB_TYPE_VENDOR |
  197. USB_RECIP_DEVICE, value, index,
  198. transfer_buffer, size, CTRL_TIMEOUT);
  199. kfree(transfer_buffer);
  200. if (status < 0)
  201. dev_err(&udev->dev,
  202. "Failed sending control message, error %d.\n", status);
  203. return status;
  204. }
  205. /* Control messages sent to the camera to initialize it
  206. * and launch the capture */
  207. typedef struct {
  208. unsigned int value;
  209. unsigned int size;
  210. unsigned char *bytes;
  211. } message;
  212. /* method 0 */
  213. static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  214. static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
  215. static unsigned char m0d3[] = { 0, 0 };
  216. static message m0[] = {
  217. {0x1f30, 0, NULL},
  218. {0xd000, 0, NULL},
  219. {0x3370, sizeof(m0d1), m0d1},
  220. {0x2000, 0, NULL},
  221. {0x2f0f, 0, NULL},
  222. {0x2610, sizeof(m0d2), m0d2},
  223. {0xe107, 0, NULL},
  224. {0x2502, 0, NULL},
  225. {0x1f70, 0, NULL},
  226. {0xd000, 0, NULL},
  227. {0x9a01, sizeof(m0d3), m0d3},
  228. {-1, -1, NULL}
  229. };
  230. /* method 1 */
  231. static unsigned char m1d1[] = { 0xff, 0xff };
  232. static unsigned char m1d2[] = { 0x00, 0x00 };
  233. static message m1[] = {
  234. {0x1f30, 0, NULL},
  235. {0xd000, 0, NULL},
  236. {0xf000, 0, NULL},
  237. {0x2000, 0, NULL},
  238. {0x2f0f, 0, NULL},
  239. {0x2650, 0, NULL},
  240. {0xe107, 0, NULL},
  241. {0x2502, sizeof(m1d1), m1d1},
  242. {0x1f70, 0, NULL},
  243. {0xd000, 0, NULL},
  244. {0xd000, 0, NULL},
  245. {0xd000, 0, NULL},
  246. {0x9a01, sizeof(m1d2), m1d2},
  247. {-1, -1, NULL}
  248. };
  249. /* method 2 */
  250. static unsigned char m2d1[] = { 0xff, 0xff };
  251. static message m2[] = {
  252. {0x1f30, 0, NULL},
  253. {0xf000, 0, NULL},
  254. {0x2000, 0, NULL},
  255. {0x2f0f, 0, NULL},
  256. {0x2650, 0, NULL},
  257. {0xe107, 0, NULL},
  258. {0x2502, sizeof(m2d1), m2d1},
  259. {0x1f70, 0, NULL},
  260. {-1, -1, NULL}
  261. };
  262. /* init table */
  263. static message *init[3] = { m0, m1, m2 };
  264. /* JPEG static data in header (Huffman table, etc) */
  265. static unsigned char header1[] = {
  266. 0xFF, 0xD8,
  267. /*
  268. 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
  269. 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
  270. */
  271. 0xFF, 0xDB, 0x00, 0x84
  272. };
  273. static unsigned char header2[] = {
  274. 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
  275. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  276. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  277. 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  278. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
  279. 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  280. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  281. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
  282. 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
  283. 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  284. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
  285. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
  286. 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
  287. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
  288. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  289. 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  290. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  291. 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  292. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
  293. 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
  294. 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  295. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  296. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
  297. 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
  298. 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  299. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  300. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
  301. 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
  302. 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
  303. 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  304. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
  305. 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  306. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
  307. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  308. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  309. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
  310. 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
  311. 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
  312. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  313. 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
  314. 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
  315. 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
  316. 0x00, 0x3F, 0x00
  317. };
  318. static unsigned char header3;
  319. /* ------------------------------------------------------------------
  320. Videobuf operations
  321. ------------------------------------------------------------------*/
  322. static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  323. unsigned int *size)
  324. {
  325. struct zr364xx_camera *cam = vq->priv_data;
  326. *size = cam->width * cam->height * (cam->fmt->depth >> 3);
  327. if (*count == 0)
  328. *count = ZR364XX_DEF_BUFS;
  329. while (*size * (*count) > ZR364XX_DEF_BUFS * 1024 * 1024)
  330. (*count)--;
  331. return 0;
  332. }
  333. static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
  334. {
  335. _DBG("%s\n", __func__);
  336. if (in_interrupt())
  337. BUG();
  338. videobuf_vmalloc_free(&buf->vb);
  339. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  340. }
  341. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  342. enum v4l2_field field)
  343. {
  344. struct zr364xx_camera *cam = vq->priv_data;
  345. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  346. vb);
  347. int rc;
  348. DBG("%s, field=%d, fmt name = %s\n", __func__, field, cam->fmt != NULL ?
  349. cam->fmt->name : "");
  350. if (cam->fmt == NULL)
  351. return -EINVAL;
  352. buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
  353. if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
  354. DBG("invalid buffer prepare\n");
  355. return -EINVAL;
  356. }
  357. buf->fmt = cam->fmt;
  358. buf->vb.width = cam->width;
  359. buf->vb.height = cam->height;
  360. buf->vb.field = field;
  361. if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
  362. rc = videobuf_iolock(vq, &buf->vb, NULL);
  363. if (rc < 0)
  364. goto fail;
  365. }
  366. buf->vb.state = VIDEOBUF_PREPARED;
  367. return 0;
  368. fail:
  369. free_buffer(vq, buf);
  370. return rc;
  371. }
  372. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  373. {
  374. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  375. vb);
  376. struct zr364xx_camera *cam = vq->priv_data;
  377. _DBG("%s\n", __func__);
  378. buf->vb.state = VIDEOBUF_QUEUED;
  379. list_add_tail(&buf->vb.queue, &cam->vidq.active);
  380. }
  381. static void buffer_release(struct videobuf_queue *vq,
  382. struct videobuf_buffer *vb)
  383. {
  384. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  385. vb);
  386. _DBG("%s\n", __func__);
  387. free_buffer(vq, buf);
  388. }
  389. static struct videobuf_queue_ops zr364xx_video_qops = {
  390. .buf_setup = buffer_setup,
  391. .buf_prepare = buffer_prepare,
  392. .buf_queue = buffer_queue,
  393. .buf_release = buffer_release,
  394. };
  395. /********************/
  396. /* V4L2 integration */
  397. /********************/
  398. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  399. enum v4l2_buf_type type);
  400. static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
  401. loff_t * ppos)
  402. {
  403. struct zr364xx_camera *cam = video_drvdata(file);
  404. _DBG("%s\n", __func__);
  405. if (!buf)
  406. return -EINVAL;
  407. if (!count)
  408. return -EINVAL;
  409. if (cam->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  410. zr364xx_vidioc_streamon(file, cam, cam->type) == 0) {
  411. DBG("%s: reading %d bytes at pos %d.\n", __func__, (int) count,
  412. (int) *ppos);
  413. /* NoMan Sux ! */
  414. return videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
  415. file->f_flags & O_NONBLOCK);
  416. }
  417. return 0;
  418. }
  419. /* video buffer vmalloc implementation based partly on VIVI driver which is
  420. * Copyright (c) 2006 by
  421. * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
  422. * Ted Walther <ted--a.t--enumera.com>
  423. * John Sokol <sokol--a.t--videotechnology.com>
  424. * http://v4l.videotechnology.com/
  425. *
  426. */
  427. static void zr364xx_fillbuff(struct zr364xx_camera *cam,
  428. struct zr364xx_buffer *buf,
  429. int jpgsize)
  430. {
  431. int pos = 0;
  432. struct timeval ts;
  433. const char *tmpbuf;
  434. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  435. unsigned long last_frame;
  436. struct zr364xx_framei *frm;
  437. if (!vbuf)
  438. return;
  439. last_frame = cam->last_frame;
  440. if (last_frame != -1) {
  441. frm = &cam->buffer.frame[last_frame];
  442. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  443. switch (buf->fmt->fourcc) {
  444. case V4L2_PIX_FMT_JPEG:
  445. buf->vb.size = jpgsize;
  446. memcpy(vbuf, tmpbuf, buf->vb.size);
  447. break;
  448. default:
  449. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  450. }
  451. cam->last_frame = -1;
  452. } else {
  453. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  454. return;
  455. }
  456. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  457. (unsigned long)vbuf, pos);
  458. /* tell v4l buffer was filled */
  459. buf->vb.field_count = cam->frame_count * 2;
  460. do_gettimeofday(&ts);
  461. buf->vb.ts = ts;
  462. buf->vb.state = VIDEOBUF_DONE;
  463. }
  464. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  465. {
  466. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  467. struct zr364xx_buffer *buf;
  468. unsigned long flags = 0;
  469. int rc = 0;
  470. DBG("wakeup: %p\n", &dma_q);
  471. spin_lock_irqsave(&cam->slock, flags);
  472. if (list_empty(&dma_q->active)) {
  473. DBG("No active queue to serve\n");
  474. rc = -1;
  475. goto unlock;
  476. }
  477. buf = list_entry(dma_q->active.next,
  478. struct zr364xx_buffer, vb.queue);
  479. if (!waitqueue_active(&buf->vb.done)) {
  480. /* no one active */
  481. rc = -1;
  482. goto unlock;
  483. }
  484. list_del(&buf->vb.queue);
  485. do_gettimeofday(&buf->vb.ts);
  486. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  487. zr364xx_fillbuff(cam, buf, jpgsize);
  488. wake_up(&buf->vb.done);
  489. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  490. unlock:
  491. spin_unlock_irqrestore(&cam->slock, flags);
  492. return 0;
  493. }
  494. /* this function moves the usb stream read pipe data
  495. * into the system buffers.
  496. * returns 0 on success, EAGAIN if more data to process (call this
  497. * function again).
  498. */
  499. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  500. struct zr364xx_pipeinfo *pipe_info,
  501. struct urb *purb)
  502. {
  503. unsigned char *pdest;
  504. unsigned char *psrc;
  505. s32 idx = -1;
  506. struct zr364xx_framei *frm;
  507. int i = 0;
  508. unsigned char *ptr = NULL;
  509. _DBG("buffer to user\n");
  510. idx = cam->cur_frame;
  511. frm = &cam->buffer.frame[idx];
  512. /* swap bytes if camera needs it */
  513. if (cam->method == METHOD0) {
  514. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  515. for (i = 0; i < purb->actual_length/2; i++)
  516. swab16s(buf + i);
  517. }
  518. /* search done. now find out if should be acquiring */
  519. if (!cam->b_acquire) {
  520. /* we found a frame, but this channel is turned off */
  521. frm->ulState = ZR364XX_READ_IDLE;
  522. return -EINVAL;
  523. }
  524. psrc = (u8 *)pipe_info->transfer_buffer;
  525. ptr = pdest = frm->lpvbits;
  526. if (frm->ulState == ZR364XX_READ_IDLE) {
  527. frm->ulState = ZR364XX_READ_FRAME;
  528. frm->cur_size = 0;
  529. _DBG("jpeg header, ");
  530. memcpy(ptr, header1, sizeof(header1));
  531. ptr += sizeof(header1);
  532. header3 = 0;
  533. memcpy(ptr, &header3, 1);
  534. ptr++;
  535. memcpy(ptr, psrc, 64);
  536. ptr += 64;
  537. header3 = 1;
  538. memcpy(ptr, &header3, 1);
  539. ptr++;
  540. memcpy(ptr, psrc + 64, 64);
  541. ptr += 64;
  542. memcpy(ptr, header2, sizeof(header2));
  543. ptr += sizeof(header2);
  544. memcpy(ptr, psrc + 128,
  545. purb->actual_length - 128);
  546. ptr += purb->actual_length - 128;
  547. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  548. psrc[0], psrc[1], psrc[2],
  549. psrc[3], psrc[4], psrc[5],
  550. psrc[6], psrc[7], psrc[8]);
  551. frm->cur_size = ptr - pdest;
  552. } else {
  553. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  554. dev_info(&cam->udev->dev,
  555. "%s: buffer (%d bytes) too small to hold "
  556. "frame data. Discarding frame data.\n",
  557. __func__, MAX_FRAME_SIZE);
  558. } else {
  559. pdest += frm->cur_size;
  560. memcpy(pdest, psrc, purb->actual_length);
  561. frm->cur_size += purb->actual_length;
  562. }
  563. }
  564. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  565. purb->actual_length);*/
  566. if (purb->actual_length < pipe_info->transfer_size) {
  567. _DBG("****************Buffer[%d]full*************\n", idx);
  568. cam->last_frame = cam->cur_frame;
  569. cam->cur_frame++;
  570. /* end of system frame ring buffer, start at zero */
  571. if (cam->cur_frame == cam->buffer.dwFrames)
  572. cam->cur_frame = 0;
  573. /* frame ready */
  574. /* go back to find the JPEG EOI marker */
  575. ptr = pdest = frm->lpvbits;
  576. ptr += frm->cur_size - 2;
  577. while (ptr > pdest) {
  578. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  579. && *(ptr + 2) == 0xFF)
  580. break;
  581. ptr--;
  582. }
  583. if (ptr == pdest)
  584. DBG("No EOI marker\n");
  585. /* Sometimes there is junk data in the middle of the picture,
  586. * we want to skip this bogus frames */
  587. while (ptr > pdest) {
  588. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  589. && *(ptr + 2) == 0xFF)
  590. break;
  591. ptr--;
  592. }
  593. if (ptr != pdest) {
  594. DBG("Bogus frame ? %d\n", ++(cam->nb));
  595. } else if (cam->b_acquire) {
  596. /* we skip the 2 first frames which are usually buggy */
  597. if (cam->skip)
  598. cam->skip--;
  599. else {
  600. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  601. frm->cur_size,
  602. pdest[0], pdest[1], pdest[2], pdest[3],
  603. pdest[4], pdest[5], pdest[6], pdest[7]);
  604. zr364xx_got_frame(cam, frm->cur_size);
  605. }
  606. }
  607. cam->frame_count++;
  608. frm->ulState = ZR364XX_READ_IDLE;
  609. frm->cur_size = 0;
  610. }
  611. /* done successfully */
  612. return 0;
  613. }
  614. static int res_get(struct zr364xx_camera *cam)
  615. {
  616. /* is it free? */
  617. mutex_lock(&cam->lock);
  618. if (cam->resources) {
  619. /* no, someone else uses it */
  620. mutex_unlock(&cam->lock);
  621. return 0;
  622. }
  623. /* it's free, grab it */
  624. cam->resources = 1;
  625. _DBG("res: get\n");
  626. mutex_unlock(&cam->lock);
  627. return 1;
  628. }
  629. static inline int res_check(struct zr364xx_camera *cam)
  630. {
  631. return cam->resources;
  632. }
  633. static void res_free(struct zr364xx_camera *cam)
  634. {
  635. mutex_lock(&cam->lock);
  636. cam->resources = 0;
  637. mutex_unlock(&cam->lock);
  638. _DBG("res: put\n");
  639. }
  640. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  641. struct v4l2_capability *cap)
  642. {
  643. struct zr364xx_camera *cam = video_drvdata(file);
  644. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  645. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  646. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  647. sizeof(cap->bus_info));
  648. cap->version = ZR364XX_VERSION_CODE;
  649. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  650. V4L2_CAP_READWRITE |
  651. V4L2_CAP_STREAMING;
  652. return 0;
  653. }
  654. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  655. struct v4l2_input *i)
  656. {
  657. if (i->index != 0)
  658. return -EINVAL;
  659. strcpy(i->name, DRIVER_DESC " Camera");
  660. i->type = V4L2_INPUT_TYPE_CAMERA;
  661. return 0;
  662. }
  663. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  664. unsigned int *i)
  665. {
  666. *i = 0;
  667. return 0;
  668. }
  669. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  670. unsigned int i)
  671. {
  672. if (i != 0)
  673. return -EINVAL;
  674. return 0;
  675. }
  676. static int zr364xx_vidioc_queryctrl(struct file *file, void *priv,
  677. struct v4l2_queryctrl *c)
  678. {
  679. struct zr364xx_camera *cam;
  680. if (file == NULL)
  681. return -ENODEV;
  682. cam = video_drvdata(file);
  683. switch (c->id) {
  684. case V4L2_CID_BRIGHTNESS:
  685. c->type = V4L2_CTRL_TYPE_INTEGER;
  686. strcpy(c->name, "Brightness");
  687. c->minimum = 0;
  688. c->maximum = 127;
  689. c->step = 1;
  690. c->default_value = cam->mode.brightness;
  691. c->flags = 0;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }
  698. static int zr364xx_vidioc_s_ctrl(struct file *file, void *priv,
  699. struct v4l2_control *c)
  700. {
  701. struct zr364xx_camera *cam;
  702. int temp;
  703. if (file == NULL)
  704. return -ENODEV;
  705. cam = video_drvdata(file);
  706. switch (c->id) {
  707. case V4L2_CID_BRIGHTNESS:
  708. cam->mode.brightness = c->value;
  709. /* hardware brightness */
  710. mutex_lock(&cam->lock);
  711. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  712. temp = (0x60 << 8) + 127 - cam->mode.brightness;
  713. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  714. mutex_unlock(&cam->lock);
  715. break;
  716. default:
  717. return -EINVAL;
  718. }
  719. return 0;
  720. }
  721. static int zr364xx_vidioc_g_ctrl(struct file *file, void *priv,
  722. struct v4l2_control *c)
  723. {
  724. struct zr364xx_camera *cam;
  725. if (file == NULL)
  726. return -ENODEV;
  727. cam = video_drvdata(file);
  728. switch (c->id) {
  729. case V4L2_CID_BRIGHTNESS:
  730. c->value = cam->mode.brightness;
  731. break;
  732. default:
  733. return -EINVAL;
  734. }
  735. return 0;
  736. }
  737. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  738. void *priv, struct v4l2_fmtdesc *f)
  739. {
  740. if (f->index > 0)
  741. return -EINVAL;
  742. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  743. strcpy(f->description, formats[0].name);
  744. f->pixelformat = formats[0].fourcc;
  745. return 0;
  746. }
  747. static char *decode_fourcc(__u32 pixelformat, char *buf)
  748. {
  749. buf[0] = pixelformat & 0xff;
  750. buf[1] = (pixelformat >> 8) & 0xff;
  751. buf[2] = (pixelformat >> 16) & 0xff;
  752. buf[3] = (pixelformat >> 24) & 0xff;
  753. buf[4] = '\0';
  754. return buf;
  755. }
  756. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  757. struct v4l2_format *f)
  758. {
  759. struct zr364xx_camera *cam = video_drvdata(file);
  760. char pixelformat_name[5];
  761. if (cam == NULL)
  762. return -ENODEV;
  763. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  764. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  765. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  766. return -EINVAL;
  767. }
  768. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  769. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  770. f->fmt.pix.width = 320;
  771. f->fmt.pix.height = 240;
  772. }
  773. f->fmt.pix.field = V4L2_FIELD_NONE;
  774. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  775. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  776. f->fmt.pix.colorspace = 0;
  777. f->fmt.pix.priv = 0;
  778. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  779. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  780. f->fmt.pix.field);
  781. return 0;
  782. }
  783. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  784. struct v4l2_format *f)
  785. {
  786. struct zr364xx_camera *cam;
  787. if (file == NULL)
  788. return -ENODEV;
  789. cam = video_drvdata(file);
  790. f->fmt.pix.pixelformat = formats[0].fourcc;
  791. f->fmt.pix.field = V4L2_FIELD_NONE;
  792. f->fmt.pix.width = cam->width;
  793. f->fmt.pix.height = cam->height;
  794. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  795. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  796. f->fmt.pix.colorspace = 0;
  797. f->fmt.pix.priv = 0;
  798. return 0;
  799. }
  800. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  801. struct v4l2_format *f)
  802. {
  803. struct zr364xx_camera *cam = video_drvdata(file);
  804. struct videobuf_queue *q = &cam->vb_vidq;
  805. char pixelformat_name[5];
  806. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  807. int i;
  808. if (ret < 0)
  809. return ret;
  810. mutex_lock(&q->vb_lock);
  811. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  812. DBG("%s queue busy\n", __func__);
  813. ret = -EBUSY;
  814. goto out;
  815. }
  816. if (res_check(cam)) {
  817. DBG("%s can't change format after started\n", __func__);
  818. ret = -EBUSY;
  819. goto out;
  820. }
  821. cam->width = f->fmt.pix.width;
  822. cam->height = f->fmt.pix.height;
  823. dev_info(&cam->udev->dev, "%s: %dx%d mode selected\n", __func__,
  824. cam->width, cam->height);
  825. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  826. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  827. f->fmt.pix.colorspace = 0;
  828. f->fmt.pix.priv = 0;
  829. cam->vb_vidq.field = f->fmt.pix.field;
  830. cam->mode.color = V4L2_PIX_FMT_JPEG;
  831. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  832. mode = 1;
  833. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  834. mode = 2;
  835. else
  836. mode = 0;
  837. m0d1[0] = mode;
  838. m1[2].value = 0xf000 + mode;
  839. m2[1].value = 0xf000 + mode;
  840. header2[437] = cam->height / 256;
  841. header2[438] = cam->height % 256;
  842. header2[439] = cam->width / 256;
  843. header2[440] = cam->width % 256;
  844. for (i = 0; init[cam->method][i].size != -1; i++) {
  845. ret =
  846. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  847. 0, init[cam->method][i].bytes,
  848. init[cam->method][i].size);
  849. if (ret < 0) {
  850. dev_err(&cam->udev->dev,
  851. "error during resolution change sequence: %d\n", i);
  852. goto out;
  853. }
  854. }
  855. /* Added some delay here, since opening/closing the camera quickly,
  856. * like Ekiga does during its startup, can crash the webcam
  857. */
  858. mdelay(100);
  859. cam->skip = 2;
  860. ret = 0;
  861. out:
  862. mutex_unlock(&q->vb_lock);
  863. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  864. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  865. f->fmt.pix.field);
  866. return ret;
  867. }
  868. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  869. struct v4l2_requestbuffers *p)
  870. {
  871. int rc;
  872. struct zr364xx_camera *cam = video_drvdata(file);
  873. rc = videobuf_reqbufs(&cam->vb_vidq, p);
  874. return rc;
  875. }
  876. static int zr364xx_vidioc_querybuf(struct file *file,
  877. void *priv,
  878. struct v4l2_buffer *p)
  879. {
  880. int rc;
  881. struct zr364xx_camera *cam = video_drvdata(file);
  882. rc = videobuf_querybuf(&cam->vb_vidq, p);
  883. return rc;
  884. }
  885. static int zr364xx_vidioc_qbuf(struct file *file,
  886. void *priv,
  887. struct v4l2_buffer *p)
  888. {
  889. int rc;
  890. struct zr364xx_camera *cam = video_drvdata(file);
  891. _DBG("%s\n", __func__);
  892. rc = videobuf_qbuf(&cam->vb_vidq, p);
  893. return rc;
  894. }
  895. static int zr364xx_vidioc_dqbuf(struct file *file,
  896. void *priv,
  897. struct v4l2_buffer *p)
  898. {
  899. int rc;
  900. struct zr364xx_camera *cam = video_drvdata(file);
  901. _DBG("%s\n", __func__);
  902. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  903. return rc;
  904. }
  905. static void read_pipe_completion(struct urb *purb)
  906. {
  907. struct zr364xx_pipeinfo *pipe_info;
  908. struct zr364xx_camera *cam;
  909. int pipe;
  910. pipe_info = purb->context;
  911. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  912. if (pipe_info == NULL) {
  913. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  914. return;
  915. }
  916. cam = pipe_info->cam;
  917. if (cam == NULL) {
  918. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  919. return;
  920. }
  921. /* if shutting down, do not resubmit, exit immediately */
  922. if (purb->status == -ESHUTDOWN) {
  923. DBG("%s, err shutdown\n", __func__);
  924. pipe_info->err_count++;
  925. return;
  926. }
  927. if (pipe_info->state == 0) {
  928. DBG("exiting USB pipe\n");
  929. return;
  930. }
  931. if (purb->actual_length < 0 ||
  932. purb->actual_length > pipe_info->transfer_size) {
  933. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  934. return;
  935. }
  936. if (purb->status == 0)
  937. zr364xx_read_video_callback(cam, pipe_info, purb);
  938. else {
  939. pipe_info->err_count++;
  940. DBG("%s: failed URB %d\n", __func__, purb->status);
  941. }
  942. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  943. /* reuse urb */
  944. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  945. pipe,
  946. pipe_info->transfer_buffer,
  947. pipe_info->transfer_size,
  948. read_pipe_completion, pipe_info);
  949. if (pipe_info->state != 0) {
  950. purb->status = usb_submit_urb(pipe_info->stream_urb,
  951. GFP_ATOMIC);
  952. if (purb->status)
  953. dev_err(&cam->udev->dev,
  954. "error submitting urb (error=%i)\n",
  955. purb->status);
  956. } else
  957. DBG("read pipe complete state 0\n");
  958. }
  959. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  960. {
  961. int pipe;
  962. int retval;
  963. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  964. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  965. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  966. pipe_info->state = 1;
  967. pipe_info->err_count = 0;
  968. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  969. if (!pipe_info->stream_urb) {
  970. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  971. return -ENOMEM;
  972. }
  973. /* transfer buffer allocated in board_init */
  974. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  975. pipe,
  976. pipe_info->transfer_buffer,
  977. pipe_info->transfer_size,
  978. read_pipe_completion, pipe_info);
  979. DBG("submitting URB %p\n", pipe_info->stream_urb);
  980. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  981. if (retval) {
  982. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  983. return retval;
  984. }
  985. return 0;
  986. }
  987. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  988. {
  989. struct zr364xx_pipeinfo *pipe_info;
  990. if (cam == NULL) {
  991. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  992. return;
  993. }
  994. DBG("stop read pipe\n");
  995. pipe_info = cam->pipe;
  996. if (pipe_info) {
  997. if (pipe_info->state != 0)
  998. pipe_info->state = 0;
  999. if (pipe_info->stream_urb) {
  1000. /* cancel urb */
  1001. usb_kill_urb(pipe_info->stream_urb);
  1002. usb_free_urb(pipe_info->stream_urb);
  1003. pipe_info->stream_urb = NULL;
  1004. }
  1005. }
  1006. return;
  1007. }
  1008. /* starts acquisition process */
  1009. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  1010. {
  1011. int j;
  1012. DBG("start acquire\n");
  1013. cam->last_frame = -1;
  1014. cam->cur_frame = 0;
  1015. for (j = 0; j < FRAMES; j++) {
  1016. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  1017. cam->buffer.frame[j].cur_size = 0;
  1018. }
  1019. cam->b_acquire = 1;
  1020. return 0;
  1021. }
  1022. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  1023. {
  1024. cam->b_acquire = 0;
  1025. return 0;
  1026. }
  1027. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  1028. enum v4l2_buf_type type)
  1029. {
  1030. struct zr364xx_camera *cam = video_drvdata(file);
  1031. int j;
  1032. int res;
  1033. DBG("%s\n", __func__);
  1034. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1035. dev_err(&cam->udev->dev, "invalid fh type0\n");
  1036. return -EINVAL;
  1037. }
  1038. if (cam->type != type) {
  1039. dev_err(&cam->udev->dev, "invalid fh type1\n");
  1040. return -EINVAL;
  1041. }
  1042. if (!res_get(cam)) {
  1043. dev_err(&cam->udev->dev, "stream busy\n");
  1044. return -EBUSY;
  1045. }
  1046. cam->last_frame = -1;
  1047. cam->cur_frame = 0;
  1048. cam->frame_count = 0;
  1049. for (j = 0; j < FRAMES; j++) {
  1050. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  1051. cam->buffer.frame[j].cur_size = 0;
  1052. }
  1053. res = videobuf_streamon(&cam->vb_vidq);
  1054. if (res == 0) {
  1055. zr364xx_start_acquire(cam);
  1056. } else {
  1057. res_free(cam);
  1058. }
  1059. return res;
  1060. }
  1061. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1062. enum v4l2_buf_type type)
  1063. {
  1064. int res;
  1065. struct zr364xx_camera *cam = video_drvdata(file);
  1066. DBG("%s\n", __func__);
  1067. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1068. dev_err(&cam->udev->dev, "invalid fh type0\n");
  1069. return -EINVAL;
  1070. }
  1071. if (cam->type != type) {
  1072. dev_err(&cam->udev->dev, "invalid fh type1\n");
  1073. return -EINVAL;
  1074. }
  1075. zr364xx_stop_acquire(cam);
  1076. res = videobuf_streamoff(&cam->vb_vidq);
  1077. if (res < 0)
  1078. return res;
  1079. res_free(cam);
  1080. return 0;
  1081. }
  1082. /* open the camera */
  1083. static int zr364xx_open(struct file *file)
  1084. {
  1085. struct video_device *vdev = video_devdata(file);
  1086. struct zr364xx_camera *cam = video_drvdata(file);
  1087. struct usb_device *udev = cam->udev;
  1088. int i, err;
  1089. DBG("%s\n", __func__);
  1090. mutex_lock(&cam->open_lock);
  1091. if (cam->users) {
  1092. err = -EBUSY;
  1093. goto out;
  1094. }
  1095. for (i = 0; init[cam->method][i].size != -1; i++) {
  1096. err =
  1097. send_control_msg(udev, 1, init[cam->method][i].value,
  1098. 0, init[cam->method][i].bytes,
  1099. init[cam->method][i].size);
  1100. if (err < 0) {
  1101. dev_err(&cam->udev->dev,
  1102. "error during open sequence: %d\n", i);
  1103. goto out;
  1104. }
  1105. }
  1106. cam->skip = 2;
  1107. cam->users++;
  1108. file->private_data = vdev;
  1109. cam->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1110. cam->fmt = formats;
  1111. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1112. NULL, &cam->slock,
  1113. cam->type,
  1114. V4L2_FIELD_NONE,
  1115. sizeof(struct zr364xx_buffer), cam);
  1116. /* Added some delay here, since opening/closing the camera quickly,
  1117. * like Ekiga does during its startup, can crash the webcam
  1118. */
  1119. mdelay(100);
  1120. err = 0;
  1121. out:
  1122. mutex_unlock(&cam->open_lock);
  1123. DBG("%s: %d\n", __func__, err);
  1124. return err;
  1125. }
  1126. static void zr364xx_destroy(struct zr364xx_camera *cam)
  1127. {
  1128. unsigned long i;
  1129. if (!cam) {
  1130. printk(KERN_ERR KBUILD_MODNAME ", %s: no device\n", __func__);
  1131. return;
  1132. }
  1133. mutex_lock(&cam->open_lock);
  1134. if (cam->vdev)
  1135. video_unregister_device(cam->vdev);
  1136. cam->vdev = NULL;
  1137. /* stops the read pipe if it is running */
  1138. if (cam->b_acquire)
  1139. zr364xx_stop_acquire(cam);
  1140. zr364xx_stop_readpipe(cam);
  1141. /* release sys buffers */
  1142. for (i = 0; i < FRAMES; i++) {
  1143. if (cam->buffer.frame[i].lpvbits) {
  1144. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1145. vfree(cam->buffer.frame[i].lpvbits);
  1146. }
  1147. cam->buffer.frame[i].lpvbits = NULL;
  1148. }
  1149. /* release transfer buffer */
  1150. kfree(cam->pipe->transfer_buffer);
  1151. cam->pipe->transfer_buffer = NULL;
  1152. mutex_unlock(&cam->open_lock);
  1153. kfree(cam);
  1154. cam = NULL;
  1155. }
  1156. /* release the camera */
  1157. static int zr364xx_release(struct file *file)
  1158. {
  1159. struct zr364xx_camera *cam;
  1160. struct usb_device *udev;
  1161. int i, err;
  1162. DBG("%s\n", __func__);
  1163. cam = video_drvdata(file);
  1164. if (!cam)
  1165. return -ENODEV;
  1166. mutex_lock(&cam->open_lock);
  1167. udev = cam->udev;
  1168. /* turn off stream */
  1169. if (res_check(cam)) {
  1170. if (cam->b_acquire)
  1171. zr364xx_stop_acquire(cam);
  1172. videobuf_streamoff(&cam->vb_vidq);
  1173. res_free(cam);
  1174. }
  1175. cam->users--;
  1176. file->private_data = NULL;
  1177. for (i = 0; i < 2; i++) {
  1178. err =
  1179. send_control_msg(udev, 1, init[cam->method][i].value,
  1180. 0, init[cam->method][i].bytes,
  1181. init[cam->method][i].size);
  1182. if (err < 0) {
  1183. dev_err(&udev->dev, "error during release sequence\n");
  1184. goto out;
  1185. }
  1186. }
  1187. /* Added some delay here, since opening/closing the camera quickly,
  1188. * like Ekiga does during its startup, can crash the webcam
  1189. */
  1190. mdelay(100);
  1191. err = 0;
  1192. out:
  1193. mutex_unlock(&cam->open_lock);
  1194. return err;
  1195. }
  1196. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1197. {
  1198. struct zr364xx_camera *cam = video_drvdata(file);
  1199. int ret;
  1200. if (cam == NULL) {
  1201. DBG("%s: cam == NULL\n", __func__);
  1202. return -ENODEV;
  1203. }
  1204. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1205. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1206. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1207. (unsigned long)vma->vm_start,
  1208. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1209. return ret;
  1210. }
  1211. static unsigned int zr364xx_poll(struct file *file,
  1212. struct poll_table_struct *wait)
  1213. {
  1214. struct zr364xx_camera *cam = video_drvdata(file);
  1215. struct videobuf_queue *q = &cam->vb_vidq;
  1216. _DBG("%s\n", __func__);
  1217. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1218. return POLLERR;
  1219. return videobuf_poll_stream(file, q, wait);
  1220. }
  1221. static const struct v4l2_file_operations zr364xx_fops = {
  1222. .owner = THIS_MODULE,
  1223. .open = zr364xx_open,
  1224. .release = zr364xx_release,
  1225. .read = zr364xx_read,
  1226. .mmap = zr364xx_mmap,
  1227. .ioctl = video_ioctl2,
  1228. .poll = zr364xx_poll,
  1229. };
  1230. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1231. .vidioc_querycap = zr364xx_vidioc_querycap,
  1232. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1233. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1234. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1235. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1236. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1237. .vidioc_g_input = zr364xx_vidioc_g_input,
  1238. .vidioc_s_input = zr364xx_vidioc_s_input,
  1239. .vidioc_streamon = zr364xx_vidioc_streamon,
  1240. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1241. .vidioc_queryctrl = zr364xx_vidioc_queryctrl,
  1242. .vidioc_g_ctrl = zr364xx_vidioc_g_ctrl,
  1243. .vidioc_s_ctrl = zr364xx_vidioc_s_ctrl,
  1244. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1245. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1246. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1247. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1248. };
  1249. static struct video_device zr364xx_template = {
  1250. .name = DRIVER_DESC,
  1251. .fops = &zr364xx_fops,
  1252. .ioctl_ops = &zr364xx_ioctl_ops,
  1253. .release = video_device_release,
  1254. };
  1255. /*******************/
  1256. /* USB integration */
  1257. /*******************/
  1258. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1259. {
  1260. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1261. unsigned long i;
  1262. DBG("board init: %p\n", cam);
  1263. memset(pipe, 0, sizeof(*pipe));
  1264. pipe->cam = cam;
  1265. pipe->transfer_size = BUFFER_SIZE;
  1266. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1267. GFP_KERNEL);
  1268. if (pipe->transfer_buffer == NULL) {
  1269. DBG("out of memory!\n");
  1270. return -ENOMEM;
  1271. }
  1272. cam->b_acquire = 0;
  1273. cam->frame_count = 0;
  1274. /*** start create system buffers ***/
  1275. for (i = 0; i < FRAMES; i++) {
  1276. /* always allocate maximum size for system buffers */
  1277. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1278. DBG("valloc %p, idx %lu, pdata %p\n",
  1279. &cam->buffer.frame[i], i,
  1280. cam->buffer.frame[i].lpvbits);
  1281. if (cam->buffer.frame[i].lpvbits == NULL) {
  1282. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1283. "Using less frames\n");
  1284. break;
  1285. }
  1286. }
  1287. if (i == 0) {
  1288. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1289. kfree(cam->pipe->transfer_buffer);
  1290. cam->pipe->transfer_buffer = NULL;
  1291. return -ENOMEM;
  1292. } else
  1293. cam->buffer.dwFrames = i;
  1294. /* make sure internal states are set */
  1295. for (i = 0; i < FRAMES; i++) {
  1296. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1297. cam->buffer.frame[i].cur_size = 0;
  1298. }
  1299. cam->cur_frame = 0;
  1300. cam->last_frame = -1;
  1301. /*** end create system buffers ***/
  1302. /* start read pipe */
  1303. zr364xx_start_readpipe(cam);
  1304. DBG(": board initialized\n");
  1305. return 0;
  1306. }
  1307. static int zr364xx_probe(struct usb_interface *intf,
  1308. const struct usb_device_id *id)
  1309. {
  1310. struct usb_device *udev = interface_to_usbdev(intf);
  1311. struct zr364xx_camera *cam = NULL;
  1312. struct usb_host_interface *iface_desc;
  1313. struct usb_endpoint_descriptor *endpoint;
  1314. int err;
  1315. int i;
  1316. DBG("probing...\n");
  1317. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1318. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1319. le16_to_cpu(udev->descriptor.idVendor),
  1320. le16_to_cpu(udev->descriptor.idProduct));
  1321. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1322. if (cam == NULL) {
  1323. dev_err(&udev->dev, "cam: out of memory !\n");
  1324. return -ENOMEM;
  1325. }
  1326. /* save the init method used by this camera */
  1327. cam->method = id->driver_info;
  1328. cam->vdev = video_device_alloc();
  1329. if (cam->vdev == NULL) {
  1330. dev_err(&udev->dev, "cam->vdev: out of memory !\n");
  1331. kfree(cam);
  1332. cam = NULL;
  1333. return -ENOMEM;
  1334. }
  1335. memcpy(cam->vdev, &zr364xx_template, sizeof(zr364xx_template));
  1336. cam->vdev->parent = &intf->dev;
  1337. video_set_drvdata(cam->vdev, cam);
  1338. if (debug)
  1339. cam->vdev->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1340. cam->udev = udev;
  1341. switch (mode) {
  1342. case 1:
  1343. dev_info(&udev->dev, "160x120 mode selected\n");
  1344. cam->width = 160;
  1345. cam->height = 120;
  1346. break;
  1347. case 2:
  1348. dev_info(&udev->dev, "640x480 mode selected\n");
  1349. cam->width = 640;
  1350. cam->height = 480;
  1351. break;
  1352. default:
  1353. dev_info(&udev->dev, "320x240 mode selected\n");
  1354. cam->width = 320;
  1355. cam->height = 240;
  1356. break;
  1357. }
  1358. m0d1[0] = mode;
  1359. m1[2].value = 0xf000 + mode;
  1360. m2[1].value = 0xf000 + mode;
  1361. header2[437] = cam->height / 256;
  1362. header2[438] = cam->height % 256;
  1363. header2[439] = cam->width / 256;
  1364. header2[440] = cam->width % 256;
  1365. cam->users = 0;
  1366. cam->nb = 0;
  1367. cam->mode.brightness = 64;
  1368. mutex_init(&cam->lock);
  1369. mutex_init(&cam->open_lock);
  1370. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1371. /* set up the endpoint information */
  1372. iface_desc = intf->cur_altsetting;
  1373. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1374. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1375. endpoint = &iface_desc->endpoint[i].desc;
  1376. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1377. /* we found the bulk in endpoint */
  1378. cam->read_endpoint = endpoint->bEndpointAddress;
  1379. }
  1380. }
  1381. if (!cam->read_endpoint) {
  1382. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1383. return -ENOMEM;
  1384. }
  1385. /* v4l */
  1386. INIT_LIST_HEAD(&cam->vidq.active);
  1387. cam->vidq.cam = cam;
  1388. err = video_register_device(cam->vdev, VFL_TYPE_GRABBER, -1);
  1389. if (err) {
  1390. dev_err(&udev->dev, "video_register_device failed\n");
  1391. video_device_release(cam->vdev);
  1392. kfree(cam);
  1393. cam = NULL;
  1394. return err;
  1395. }
  1396. usb_set_intfdata(intf, cam);
  1397. /* load zr364xx board specific */
  1398. err = zr364xx_board_init(cam);
  1399. if (err) {
  1400. spin_lock_init(&cam->slock);
  1401. return err;
  1402. }
  1403. spin_lock_init(&cam->slock);
  1404. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1405. video_device_node_name(cam->vdev));
  1406. return 0;
  1407. }
  1408. static void zr364xx_disconnect(struct usb_interface *intf)
  1409. {
  1410. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1411. videobuf_mmap_free(&cam->vb_vidq);
  1412. usb_set_intfdata(intf, NULL);
  1413. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1414. zr364xx_destroy(cam);
  1415. }
  1416. /**********************/
  1417. /* Module integration */
  1418. /**********************/
  1419. static struct usb_driver zr364xx_driver = {
  1420. .name = "zr364xx",
  1421. .probe = zr364xx_probe,
  1422. .disconnect = zr364xx_disconnect,
  1423. .id_table = device_table
  1424. };
  1425. static int __init zr364xx_init(void)
  1426. {
  1427. int retval;
  1428. retval = usb_register(&zr364xx_driver);
  1429. if (retval)
  1430. printk(KERN_ERR KBUILD_MODNAME ": usb_register failed!\n");
  1431. else
  1432. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1433. return retval;
  1434. }
  1435. static void __exit zr364xx_exit(void)
  1436. {
  1437. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC " module unloaded\n");
  1438. usb_deregister(&zr364xx_driver);
  1439. }
  1440. module_init(zr364xx_init);
  1441. module_exit(zr364xx_exit);
  1442. MODULE_AUTHOR(DRIVER_AUTHOR);
  1443. MODULE_DESCRIPTION(DRIVER_DESC);
  1444. MODULE_LICENSE("GPL");