zr364xx.c 41 KB

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