zr364xx.c 41 KB

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