zr364xx.c 42 KB

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