zr364xx.c 42 KB

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