zr364xx.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * Zoran 364xx based USB webcam module version 0.73
  3. *
  4. * Allows you to use your USB webcam with V4L2 applications
  5. * This is still in heavy developpement !
  6. *
  7. * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
  8. * http://royale.zerezo.com/zr364xx/
  9. *
  10. * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
  11. * V4L2 version inspired by meye.c driver
  12. *
  13. * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * 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. BUG_ON(in_interrupt());
  332. videobuf_vmalloc_free(&buf->vb);
  333. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  334. }
  335. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  336. enum v4l2_field field)
  337. {
  338. struct zr364xx_camera *cam = vq->priv_data;
  339. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  340. vb);
  341. int rc;
  342. DBG("%s, field=%d, fmt name = %s\n", __func__, field, cam->fmt != NULL ?
  343. cam->fmt->name : "");
  344. if (cam->fmt == NULL)
  345. return -EINVAL;
  346. buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
  347. if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
  348. DBG("invalid buffer prepare\n");
  349. return -EINVAL;
  350. }
  351. buf->fmt = cam->fmt;
  352. buf->vb.width = cam->width;
  353. buf->vb.height = cam->height;
  354. buf->vb.field = field;
  355. if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
  356. rc = videobuf_iolock(vq, &buf->vb, NULL);
  357. if (rc < 0)
  358. goto fail;
  359. }
  360. buf->vb.state = VIDEOBUF_PREPARED;
  361. return 0;
  362. fail:
  363. free_buffer(vq, buf);
  364. return rc;
  365. }
  366. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  367. {
  368. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  369. vb);
  370. struct zr364xx_camera *cam = vq->priv_data;
  371. _DBG("%s\n", __func__);
  372. buf->vb.state = VIDEOBUF_QUEUED;
  373. list_add_tail(&buf->vb.queue, &cam->vidq.active);
  374. }
  375. static void buffer_release(struct videobuf_queue *vq,
  376. struct videobuf_buffer *vb)
  377. {
  378. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  379. vb);
  380. _DBG("%s\n", __func__);
  381. free_buffer(vq, buf);
  382. }
  383. static struct videobuf_queue_ops zr364xx_video_qops = {
  384. .buf_setup = buffer_setup,
  385. .buf_prepare = buffer_prepare,
  386. .buf_queue = buffer_queue,
  387. .buf_release = buffer_release,
  388. };
  389. /********************/
  390. /* V4L2 integration */
  391. /********************/
  392. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  393. enum v4l2_buf_type type);
  394. static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
  395. loff_t * ppos)
  396. {
  397. struct zr364xx_camera *cam = video_drvdata(file);
  398. int err = 0;
  399. _DBG("%s\n", __func__);
  400. if (!buf)
  401. return -EINVAL;
  402. if (!count)
  403. return -EINVAL;
  404. if (mutex_lock_interruptible(&cam->lock))
  405. return -ERESTARTSYS;
  406. err = zr364xx_vidioc_streamon(file, file->private_data,
  407. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  408. if (err == 0) {
  409. DBG("%s: reading %d bytes at pos %d.\n", __func__,
  410. (int) count, (int) *ppos);
  411. /* NoMan Sux ! */
  412. err = videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
  413. file->f_flags & O_NONBLOCK);
  414. }
  415. mutex_unlock(&cam->lock);
  416. return err;
  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. const char *tmpbuf;
  432. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  433. unsigned long last_frame;
  434. if (!vbuf)
  435. return;
  436. last_frame = cam->last_frame;
  437. if (last_frame != -1) {
  438. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  439. switch (buf->fmt->fourcc) {
  440. case V4L2_PIX_FMT_JPEG:
  441. buf->vb.size = jpgsize;
  442. memcpy(vbuf, tmpbuf, buf->vb.size);
  443. break;
  444. default:
  445. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  446. }
  447. cam->last_frame = -1;
  448. } else {
  449. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  450. return;
  451. }
  452. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  453. (unsigned long)vbuf, pos);
  454. /* tell v4l buffer was filled */
  455. buf->vb.field_count = cam->frame_count * 2;
  456. v4l2_get_timestamp(&buf->vb.ts);
  457. buf->vb.state = VIDEOBUF_DONE;
  458. }
  459. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  460. {
  461. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  462. struct zr364xx_buffer *buf;
  463. unsigned long flags = 0;
  464. int rc = 0;
  465. DBG("wakeup: %p\n", &dma_q);
  466. spin_lock_irqsave(&cam->slock, flags);
  467. if (list_empty(&dma_q->active)) {
  468. DBG("No active queue to serve\n");
  469. rc = -1;
  470. goto unlock;
  471. }
  472. buf = list_entry(dma_q->active.next,
  473. struct zr364xx_buffer, vb.queue);
  474. if (!waitqueue_active(&buf->vb.done)) {
  475. /* no one active */
  476. rc = -1;
  477. goto unlock;
  478. }
  479. list_del(&buf->vb.queue);
  480. v4l2_get_timestamp(&buf->vb.ts);
  481. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  482. zr364xx_fillbuff(cam, buf, jpgsize);
  483. wake_up(&buf->vb.done);
  484. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  485. unlock:
  486. spin_unlock_irqrestore(&cam->slock, flags);
  487. return rc;
  488. }
  489. /* this function moves the usb stream read pipe data
  490. * into the system buffers.
  491. * returns 0 on success, EAGAIN if more data to process (call this
  492. * function again).
  493. */
  494. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  495. struct zr364xx_pipeinfo *pipe_info,
  496. struct urb *purb)
  497. {
  498. unsigned char *pdest;
  499. unsigned char *psrc;
  500. s32 idx = -1;
  501. struct zr364xx_framei *frm;
  502. int i = 0;
  503. unsigned char *ptr = NULL;
  504. _DBG("buffer to user\n");
  505. idx = cam->cur_frame;
  506. frm = &cam->buffer.frame[idx];
  507. /* swap bytes if camera needs it */
  508. if (cam->method == METHOD0) {
  509. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  510. for (i = 0; i < purb->actual_length/2; i++)
  511. swab16s(buf + i);
  512. }
  513. /* search done. now find out if should be acquiring */
  514. if (!cam->b_acquire) {
  515. /* we found a frame, but this channel is turned off */
  516. frm->ulState = ZR364XX_READ_IDLE;
  517. return -EINVAL;
  518. }
  519. psrc = (u8 *)pipe_info->transfer_buffer;
  520. ptr = pdest = frm->lpvbits;
  521. if (frm->ulState == ZR364XX_READ_IDLE) {
  522. frm->ulState = ZR364XX_READ_FRAME;
  523. frm->cur_size = 0;
  524. _DBG("jpeg header, ");
  525. memcpy(ptr, header1, sizeof(header1));
  526. ptr += sizeof(header1);
  527. header3 = 0;
  528. memcpy(ptr, &header3, 1);
  529. ptr++;
  530. memcpy(ptr, psrc, 64);
  531. ptr += 64;
  532. header3 = 1;
  533. memcpy(ptr, &header3, 1);
  534. ptr++;
  535. memcpy(ptr, psrc + 64, 64);
  536. ptr += 64;
  537. memcpy(ptr, header2, sizeof(header2));
  538. ptr += sizeof(header2);
  539. memcpy(ptr, psrc + 128,
  540. purb->actual_length - 128);
  541. ptr += purb->actual_length - 128;
  542. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  543. psrc[0], psrc[1], psrc[2],
  544. psrc[3], psrc[4], psrc[5],
  545. psrc[6], psrc[7], psrc[8]);
  546. frm->cur_size = ptr - pdest;
  547. } else {
  548. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  549. dev_info(&cam->udev->dev,
  550. "%s: buffer (%d bytes) too small to hold "
  551. "frame data. Discarding frame data.\n",
  552. __func__, MAX_FRAME_SIZE);
  553. } else {
  554. pdest += frm->cur_size;
  555. memcpy(pdest, psrc, purb->actual_length);
  556. frm->cur_size += purb->actual_length;
  557. }
  558. }
  559. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  560. purb->actual_length);*/
  561. if (purb->actual_length < pipe_info->transfer_size) {
  562. _DBG("****************Buffer[%d]full*************\n", idx);
  563. cam->last_frame = cam->cur_frame;
  564. cam->cur_frame++;
  565. /* end of system frame ring buffer, start at zero */
  566. if (cam->cur_frame == cam->buffer.dwFrames)
  567. cam->cur_frame = 0;
  568. /* frame ready */
  569. /* go back to find the JPEG EOI marker */
  570. ptr = pdest = frm->lpvbits;
  571. ptr += frm->cur_size - 2;
  572. while (ptr > pdest) {
  573. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  574. && *(ptr + 2) == 0xFF)
  575. break;
  576. ptr--;
  577. }
  578. if (ptr == pdest)
  579. DBG("No EOI marker\n");
  580. /* Sometimes there is junk data in the middle of the picture,
  581. * we want to skip this bogus frames */
  582. while (ptr > pdest) {
  583. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  584. && *(ptr + 2) == 0xFF)
  585. break;
  586. ptr--;
  587. }
  588. if (ptr != pdest) {
  589. DBG("Bogus frame ? %d\n", ++(cam->nb));
  590. } else if (cam->b_acquire) {
  591. /* we skip the 2 first frames which are usually buggy */
  592. if (cam->skip)
  593. cam->skip--;
  594. else {
  595. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  596. frm->cur_size,
  597. pdest[0], pdest[1], pdest[2], pdest[3],
  598. pdest[4], pdest[5], pdest[6], pdest[7]);
  599. zr364xx_got_frame(cam, frm->cur_size);
  600. }
  601. }
  602. cam->frame_count++;
  603. frm->ulState = ZR364XX_READ_IDLE;
  604. frm->cur_size = 0;
  605. }
  606. /* done successfully */
  607. return 0;
  608. }
  609. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  610. struct v4l2_capability *cap)
  611. {
  612. struct zr364xx_camera *cam = video_drvdata(file);
  613. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  614. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  615. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  616. sizeof(cap->bus_info));
  617. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  618. V4L2_CAP_READWRITE |
  619. V4L2_CAP_STREAMING;
  620. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  621. return 0;
  622. }
  623. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  624. struct v4l2_input *i)
  625. {
  626. if (i->index != 0)
  627. return -EINVAL;
  628. strcpy(i->name, DRIVER_DESC " Camera");
  629. i->type = V4L2_INPUT_TYPE_CAMERA;
  630. return 0;
  631. }
  632. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  633. unsigned int *i)
  634. {
  635. *i = 0;
  636. return 0;
  637. }
  638. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  639. unsigned int i)
  640. {
  641. if (i != 0)
  642. return -EINVAL;
  643. return 0;
  644. }
  645. static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
  646. {
  647. struct zr364xx_camera *cam =
  648. container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
  649. int temp;
  650. switch (ctrl->id) {
  651. case V4L2_CID_BRIGHTNESS:
  652. /* hardware brightness */
  653. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  654. temp = (0x60 << 8) + 127 - ctrl->val;
  655. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  663. void *priv, struct v4l2_fmtdesc *f)
  664. {
  665. if (f->index > 0)
  666. return -EINVAL;
  667. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  668. strcpy(f->description, formats[0].name);
  669. f->pixelformat = formats[0].fourcc;
  670. return 0;
  671. }
  672. static char *decode_fourcc(__u32 pixelformat, char *buf)
  673. {
  674. buf[0] = pixelformat & 0xff;
  675. buf[1] = (pixelformat >> 8) & 0xff;
  676. buf[2] = (pixelformat >> 16) & 0xff;
  677. buf[3] = (pixelformat >> 24) & 0xff;
  678. buf[4] = '\0';
  679. return buf;
  680. }
  681. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  682. struct v4l2_format *f)
  683. {
  684. struct zr364xx_camera *cam = video_drvdata(file);
  685. char pixelformat_name[5];
  686. if (cam == NULL)
  687. return -ENODEV;
  688. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  689. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  690. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  691. return -EINVAL;
  692. }
  693. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  694. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  695. f->fmt.pix.width = 320;
  696. f->fmt.pix.height = 240;
  697. }
  698. f->fmt.pix.field = V4L2_FIELD_NONE;
  699. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  700. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  701. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  702. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  703. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  704. f->fmt.pix.field);
  705. return 0;
  706. }
  707. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  708. struct v4l2_format *f)
  709. {
  710. struct zr364xx_camera *cam;
  711. if (file == NULL)
  712. return -ENODEV;
  713. cam = video_drvdata(file);
  714. f->fmt.pix.pixelformat = formats[0].fourcc;
  715. f->fmt.pix.field = V4L2_FIELD_NONE;
  716. f->fmt.pix.width = cam->width;
  717. f->fmt.pix.height = cam->height;
  718. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  719. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  720. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  721. return 0;
  722. }
  723. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  724. struct v4l2_format *f)
  725. {
  726. struct zr364xx_camera *cam = video_drvdata(file);
  727. struct videobuf_queue *q = &cam->vb_vidq;
  728. char pixelformat_name[5];
  729. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  730. int i;
  731. if (ret < 0)
  732. return ret;
  733. mutex_lock(&q->vb_lock);
  734. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  735. DBG("%s queue busy\n", __func__);
  736. ret = -EBUSY;
  737. goto out;
  738. }
  739. if (cam->owner) {
  740. DBG("%s can't change format after started\n", __func__);
  741. ret = -EBUSY;
  742. goto out;
  743. }
  744. cam->width = f->fmt.pix.width;
  745. cam->height = f->fmt.pix.height;
  746. DBG("%s: %dx%d mode selected\n", __func__,
  747. cam->width, cam->height);
  748. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  749. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  750. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  751. cam->vb_vidq.field = f->fmt.pix.field;
  752. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  753. mode = 1;
  754. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  755. mode = 2;
  756. else
  757. mode = 0;
  758. m0d1[0] = mode;
  759. m1[2].value = 0xf000 + mode;
  760. m2[1].value = 0xf000 + mode;
  761. /* special case for METHOD3, the modes are different */
  762. if (cam->method == METHOD3) {
  763. switch (mode) {
  764. case 1:
  765. m2[1].value = 0xf000 + 4;
  766. break;
  767. case 2:
  768. m2[1].value = 0xf000 + 0;
  769. break;
  770. default:
  771. m2[1].value = 0xf000 + 1;
  772. break;
  773. }
  774. }
  775. header2[437] = cam->height / 256;
  776. header2[438] = cam->height % 256;
  777. header2[439] = cam->width / 256;
  778. header2[440] = cam->width % 256;
  779. for (i = 0; init[cam->method][i].size != -1; i++) {
  780. ret =
  781. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  782. 0, init[cam->method][i].bytes,
  783. init[cam->method][i].size);
  784. if (ret < 0) {
  785. dev_err(&cam->udev->dev,
  786. "error during resolution change sequence: %d\n", i);
  787. goto out;
  788. }
  789. }
  790. /* Added some delay here, since opening/closing the camera quickly,
  791. * like Ekiga does during its startup, can crash the webcam
  792. */
  793. mdelay(100);
  794. cam->skip = 2;
  795. ret = 0;
  796. out:
  797. mutex_unlock(&q->vb_lock);
  798. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  799. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  800. f->fmt.pix.field);
  801. return ret;
  802. }
  803. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  804. struct v4l2_requestbuffers *p)
  805. {
  806. struct zr364xx_camera *cam = video_drvdata(file);
  807. if (cam->owner && cam->owner != priv)
  808. return -EBUSY;
  809. return videobuf_reqbufs(&cam->vb_vidq, p);
  810. }
  811. static int zr364xx_vidioc_querybuf(struct file *file,
  812. void *priv,
  813. struct v4l2_buffer *p)
  814. {
  815. int rc;
  816. struct zr364xx_camera *cam = video_drvdata(file);
  817. rc = videobuf_querybuf(&cam->vb_vidq, p);
  818. return rc;
  819. }
  820. static int zr364xx_vidioc_qbuf(struct file *file,
  821. void *priv,
  822. struct v4l2_buffer *p)
  823. {
  824. int rc;
  825. struct zr364xx_camera *cam = video_drvdata(file);
  826. _DBG("%s\n", __func__);
  827. if (cam->owner && cam->owner != priv)
  828. return -EBUSY;
  829. rc = videobuf_qbuf(&cam->vb_vidq, p);
  830. return rc;
  831. }
  832. static int zr364xx_vidioc_dqbuf(struct file *file,
  833. void *priv,
  834. struct v4l2_buffer *p)
  835. {
  836. int rc;
  837. struct zr364xx_camera *cam = video_drvdata(file);
  838. _DBG("%s\n", __func__);
  839. if (cam->owner && cam->owner != priv)
  840. return -EBUSY;
  841. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  842. return rc;
  843. }
  844. static void read_pipe_completion(struct urb *purb)
  845. {
  846. struct zr364xx_pipeinfo *pipe_info;
  847. struct zr364xx_camera *cam;
  848. int pipe;
  849. pipe_info = purb->context;
  850. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  851. if (pipe_info == NULL) {
  852. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  853. return;
  854. }
  855. cam = pipe_info->cam;
  856. if (cam == NULL) {
  857. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  858. return;
  859. }
  860. /* if shutting down, do not resubmit, exit immediately */
  861. if (purb->status == -ESHUTDOWN) {
  862. DBG("%s, err shutdown\n", __func__);
  863. pipe_info->err_count++;
  864. return;
  865. }
  866. if (pipe_info->state == 0) {
  867. DBG("exiting USB pipe\n");
  868. return;
  869. }
  870. if (purb->actual_length > pipe_info->transfer_size) {
  871. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  872. return;
  873. }
  874. if (purb->status == 0)
  875. zr364xx_read_video_callback(cam, pipe_info, purb);
  876. else {
  877. pipe_info->err_count++;
  878. DBG("%s: failed URB %d\n", __func__, purb->status);
  879. }
  880. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  881. /* reuse urb */
  882. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  883. pipe,
  884. pipe_info->transfer_buffer,
  885. pipe_info->transfer_size,
  886. read_pipe_completion, pipe_info);
  887. if (pipe_info->state != 0) {
  888. purb->status = usb_submit_urb(pipe_info->stream_urb,
  889. GFP_ATOMIC);
  890. if (purb->status)
  891. dev_err(&cam->udev->dev,
  892. "error submitting urb (error=%i)\n",
  893. purb->status);
  894. } else
  895. DBG("read pipe complete state 0\n");
  896. }
  897. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  898. {
  899. int pipe;
  900. int retval;
  901. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  902. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  903. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  904. pipe_info->state = 1;
  905. pipe_info->err_count = 0;
  906. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  907. if (!pipe_info->stream_urb) {
  908. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  909. return -ENOMEM;
  910. }
  911. /* transfer buffer allocated in board_init */
  912. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  913. pipe,
  914. pipe_info->transfer_buffer,
  915. pipe_info->transfer_size,
  916. read_pipe_completion, pipe_info);
  917. DBG("submitting URB %p\n", pipe_info->stream_urb);
  918. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  919. if (retval) {
  920. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  921. return retval;
  922. }
  923. return 0;
  924. }
  925. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  926. {
  927. struct zr364xx_pipeinfo *pipe_info;
  928. if (cam == NULL) {
  929. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  930. return;
  931. }
  932. DBG("stop read pipe\n");
  933. pipe_info = cam->pipe;
  934. if (pipe_info) {
  935. if (pipe_info->state != 0)
  936. pipe_info->state = 0;
  937. if (pipe_info->stream_urb) {
  938. /* cancel urb */
  939. usb_kill_urb(pipe_info->stream_urb);
  940. usb_free_urb(pipe_info->stream_urb);
  941. pipe_info->stream_urb = NULL;
  942. }
  943. }
  944. return;
  945. }
  946. /* starts acquisition process */
  947. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  948. {
  949. int j;
  950. DBG("start acquire\n");
  951. cam->last_frame = -1;
  952. cam->cur_frame = 0;
  953. for (j = 0; j < FRAMES; j++) {
  954. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  955. cam->buffer.frame[j].cur_size = 0;
  956. }
  957. cam->b_acquire = 1;
  958. return 0;
  959. }
  960. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  961. {
  962. cam->b_acquire = 0;
  963. return 0;
  964. }
  965. static int zr364xx_prepare(struct zr364xx_camera *cam)
  966. {
  967. int res;
  968. int i, j;
  969. for (i = 0; init[cam->method][i].size != -1; i++) {
  970. res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
  971. 0, init[cam->method][i].bytes,
  972. init[cam->method][i].size);
  973. if (res < 0) {
  974. dev_err(&cam->udev->dev,
  975. "error during open sequence: %d\n", i);
  976. return res;
  977. }
  978. }
  979. cam->skip = 2;
  980. cam->last_frame = -1;
  981. cam->cur_frame = 0;
  982. cam->frame_count = 0;
  983. for (j = 0; j < FRAMES; j++) {
  984. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  985. cam->buffer.frame[j].cur_size = 0;
  986. }
  987. v4l2_ctrl_handler_setup(&cam->ctrl_handler);
  988. return 0;
  989. }
  990. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  991. enum v4l2_buf_type type)
  992. {
  993. struct zr364xx_camera *cam = video_drvdata(file);
  994. int res;
  995. DBG("%s\n", __func__);
  996. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  997. return -EINVAL;
  998. if (cam->owner && cam->owner != priv)
  999. return -EBUSY;
  1000. res = zr364xx_prepare(cam);
  1001. if (res)
  1002. return res;
  1003. res = videobuf_streamon(&cam->vb_vidq);
  1004. if (res == 0) {
  1005. zr364xx_start_acquire(cam);
  1006. cam->owner = file->private_data;
  1007. }
  1008. return res;
  1009. }
  1010. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1011. enum v4l2_buf_type type)
  1012. {
  1013. struct zr364xx_camera *cam = video_drvdata(file);
  1014. DBG("%s\n", __func__);
  1015. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1016. return -EINVAL;
  1017. if (cam->owner && cam->owner != priv)
  1018. return -EBUSY;
  1019. zr364xx_stop_acquire(cam);
  1020. return videobuf_streamoff(&cam->vb_vidq);
  1021. }
  1022. /* open the camera */
  1023. static int zr364xx_open(struct file *file)
  1024. {
  1025. struct zr364xx_camera *cam = video_drvdata(file);
  1026. int err;
  1027. DBG("%s\n", __func__);
  1028. if (mutex_lock_interruptible(&cam->lock))
  1029. return -ERESTARTSYS;
  1030. err = v4l2_fh_open(file);
  1031. if (err)
  1032. goto out;
  1033. /* Added some delay here, since opening/closing the camera quickly,
  1034. * like Ekiga does during its startup, can crash the webcam
  1035. */
  1036. mdelay(100);
  1037. err = 0;
  1038. out:
  1039. mutex_unlock(&cam->lock);
  1040. DBG("%s: %d\n", __func__, err);
  1041. return err;
  1042. }
  1043. static void zr364xx_release(struct v4l2_device *v4l2_dev)
  1044. {
  1045. struct zr364xx_camera *cam =
  1046. container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
  1047. unsigned long i;
  1048. v4l2_device_unregister(&cam->v4l2_dev);
  1049. videobuf_mmap_free(&cam->vb_vidq);
  1050. /* release sys buffers */
  1051. for (i = 0; i < FRAMES; i++) {
  1052. if (cam->buffer.frame[i].lpvbits) {
  1053. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1054. vfree(cam->buffer.frame[i].lpvbits);
  1055. }
  1056. cam->buffer.frame[i].lpvbits = NULL;
  1057. }
  1058. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1059. /* release transfer buffer */
  1060. kfree(cam->pipe->transfer_buffer);
  1061. kfree(cam);
  1062. }
  1063. /* release the camera */
  1064. static int zr364xx_close(struct file *file)
  1065. {
  1066. struct zr364xx_camera *cam;
  1067. struct usb_device *udev;
  1068. int i;
  1069. DBG("%s\n", __func__);
  1070. cam = video_drvdata(file);
  1071. mutex_lock(&cam->lock);
  1072. udev = cam->udev;
  1073. if (file->private_data == cam->owner) {
  1074. /* turn off stream */
  1075. if (cam->b_acquire)
  1076. zr364xx_stop_acquire(cam);
  1077. videobuf_streamoff(&cam->vb_vidq);
  1078. for (i = 0; i < 2; i++) {
  1079. send_control_msg(udev, 1, init[cam->method][i].value,
  1080. 0, init[cam->method][i].bytes,
  1081. init[cam->method][i].size);
  1082. }
  1083. cam->owner = NULL;
  1084. }
  1085. /* Added some delay here, since opening/closing the camera quickly,
  1086. * like Ekiga does during its startup, can crash the webcam
  1087. */
  1088. mdelay(100);
  1089. mutex_unlock(&cam->lock);
  1090. return v4l2_fh_release(file);
  1091. }
  1092. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1093. {
  1094. struct zr364xx_camera *cam = video_drvdata(file);
  1095. int ret;
  1096. if (cam == NULL) {
  1097. DBG("%s: cam == NULL\n", __func__);
  1098. return -ENODEV;
  1099. }
  1100. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1101. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1102. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1103. (unsigned long)vma->vm_start,
  1104. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1105. return ret;
  1106. }
  1107. static unsigned int zr364xx_poll(struct file *file,
  1108. struct poll_table_struct *wait)
  1109. {
  1110. struct zr364xx_camera *cam = video_drvdata(file);
  1111. struct videobuf_queue *q = &cam->vb_vidq;
  1112. unsigned res = v4l2_ctrl_poll(file, wait);
  1113. _DBG("%s\n", __func__);
  1114. return res | videobuf_poll_stream(file, q, wait);
  1115. }
  1116. static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
  1117. .s_ctrl = zr364xx_s_ctrl,
  1118. };
  1119. static const struct v4l2_file_operations zr364xx_fops = {
  1120. .owner = THIS_MODULE,
  1121. .open = zr364xx_open,
  1122. .release = zr364xx_close,
  1123. .read = zr364xx_read,
  1124. .mmap = zr364xx_mmap,
  1125. .unlocked_ioctl = video_ioctl2,
  1126. .poll = zr364xx_poll,
  1127. };
  1128. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1129. .vidioc_querycap = zr364xx_vidioc_querycap,
  1130. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1131. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1132. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1133. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1134. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1135. .vidioc_g_input = zr364xx_vidioc_g_input,
  1136. .vidioc_s_input = zr364xx_vidioc_s_input,
  1137. .vidioc_streamon = zr364xx_vidioc_streamon,
  1138. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1139. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1140. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1141. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1142. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1143. .vidioc_log_status = v4l2_ctrl_log_status,
  1144. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1145. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1146. };
  1147. static struct video_device zr364xx_template = {
  1148. .name = DRIVER_DESC,
  1149. .fops = &zr364xx_fops,
  1150. .ioctl_ops = &zr364xx_ioctl_ops,
  1151. .release = video_device_release_empty,
  1152. };
  1153. /*******************/
  1154. /* USB integration */
  1155. /*******************/
  1156. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1157. {
  1158. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1159. unsigned long i;
  1160. DBG("board init: %p\n", cam);
  1161. memset(pipe, 0, sizeof(*pipe));
  1162. pipe->cam = cam;
  1163. pipe->transfer_size = BUFFER_SIZE;
  1164. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1165. GFP_KERNEL);
  1166. if (pipe->transfer_buffer == NULL) {
  1167. DBG("out of memory!\n");
  1168. return -ENOMEM;
  1169. }
  1170. cam->b_acquire = 0;
  1171. cam->frame_count = 0;
  1172. /*** start create system buffers ***/
  1173. for (i = 0; i < FRAMES; i++) {
  1174. /* always allocate maximum size for system buffers */
  1175. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1176. DBG("valloc %p, idx %lu, pdata %p\n",
  1177. &cam->buffer.frame[i], i,
  1178. cam->buffer.frame[i].lpvbits);
  1179. if (cam->buffer.frame[i].lpvbits == NULL) {
  1180. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1181. "Using less frames\n");
  1182. break;
  1183. }
  1184. }
  1185. if (i == 0) {
  1186. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1187. kfree(cam->pipe->transfer_buffer);
  1188. cam->pipe->transfer_buffer = NULL;
  1189. return -ENOMEM;
  1190. } else
  1191. cam->buffer.dwFrames = i;
  1192. /* make sure internal states are set */
  1193. for (i = 0; i < FRAMES; i++) {
  1194. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1195. cam->buffer.frame[i].cur_size = 0;
  1196. }
  1197. cam->cur_frame = 0;
  1198. cam->last_frame = -1;
  1199. /*** end create system buffers ***/
  1200. /* start read pipe */
  1201. zr364xx_start_readpipe(cam);
  1202. DBG(": board initialized\n");
  1203. return 0;
  1204. }
  1205. static int zr364xx_probe(struct usb_interface *intf,
  1206. const struct usb_device_id *id)
  1207. {
  1208. struct usb_device *udev = interface_to_usbdev(intf);
  1209. struct zr364xx_camera *cam = NULL;
  1210. struct usb_host_interface *iface_desc;
  1211. struct usb_endpoint_descriptor *endpoint;
  1212. struct v4l2_ctrl_handler *hdl;
  1213. int err;
  1214. int i;
  1215. DBG("probing...\n");
  1216. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1217. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1218. le16_to_cpu(udev->descriptor.idVendor),
  1219. le16_to_cpu(udev->descriptor.idProduct));
  1220. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1221. if (cam == NULL) {
  1222. dev_err(&udev->dev, "cam: out of memory !\n");
  1223. return -ENOMEM;
  1224. }
  1225. cam->v4l2_dev.release = zr364xx_release;
  1226. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  1227. if (err < 0) {
  1228. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1229. kfree(cam);
  1230. return err;
  1231. }
  1232. hdl = &cam->ctrl_handler;
  1233. v4l2_ctrl_handler_init(hdl, 1);
  1234. v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
  1235. V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
  1236. if (hdl->error) {
  1237. err = hdl->error;
  1238. dev_err(&udev->dev, "couldn't register control\n");
  1239. goto fail;
  1240. }
  1241. /* save the init method used by this camera */
  1242. cam->method = id->driver_info;
  1243. mutex_init(&cam->lock);
  1244. cam->vdev = zr364xx_template;
  1245. cam->vdev.lock = &cam->lock;
  1246. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1247. cam->vdev.ctrl_handler = &cam->ctrl_handler;
  1248. video_set_drvdata(&cam->vdev, cam);
  1249. cam->udev = udev;
  1250. switch (mode) {
  1251. case 1:
  1252. dev_info(&udev->dev, "160x120 mode selected\n");
  1253. cam->width = 160;
  1254. cam->height = 120;
  1255. break;
  1256. case 2:
  1257. dev_info(&udev->dev, "640x480 mode selected\n");
  1258. cam->width = 640;
  1259. cam->height = 480;
  1260. break;
  1261. default:
  1262. dev_info(&udev->dev, "320x240 mode selected\n");
  1263. cam->width = 320;
  1264. cam->height = 240;
  1265. break;
  1266. }
  1267. m0d1[0] = mode;
  1268. m1[2].value = 0xf000 + mode;
  1269. m2[1].value = 0xf000 + mode;
  1270. /* special case for METHOD3, the modes are different */
  1271. if (cam->method == METHOD3) {
  1272. switch (mode) {
  1273. case 1:
  1274. m2[1].value = 0xf000 + 4;
  1275. break;
  1276. case 2:
  1277. m2[1].value = 0xf000 + 0;
  1278. break;
  1279. default:
  1280. m2[1].value = 0xf000 + 1;
  1281. break;
  1282. }
  1283. }
  1284. header2[437] = cam->height / 256;
  1285. header2[438] = cam->height % 256;
  1286. header2[439] = cam->width / 256;
  1287. header2[440] = cam->width % 256;
  1288. cam->nb = 0;
  1289. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1290. /* set up the endpoint information */
  1291. iface_desc = intf->cur_altsetting;
  1292. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1293. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1294. endpoint = &iface_desc->endpoint[i].desc;
  1295. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1296. /* we found the bulk in endpoint */
  1297. cam->read_endpoint = endpoint->bEndpointAddress;
  1298. }
  1299. }
  1300. if (!cam->read_endpoint) {
  1301. err = -ENOMEM;
  1302. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1303. goto fail;
  1304. }
  1305. /* v4l */
  1306. INIT_LIST_HEAD(&cam->vidq.active);
  1307. cam->vidq.cam = cam;
  1308. usb_set_intfdata(intf, cam);
  1309. /* load zr364xx board specific */
  1310. err = zr364xx_board_init(cam);
  1311. if (!err)
  1312. err = v4l2_ctrl_handler_setup(hdl);
  1313. if (err)
  1314. goto fail;
  1315. spin_lock_init(&cam->slock);
  1316. cam->fmt = formats;
  1317. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1318. NULL, &cam->slock,
  1319. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1320. V4L2_FIELD_NONE,
  1321. sizeof(struct zr364xx_buffer), cam, &cam->lock);
  1322. err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1323. if (err) {
  1324. dev_err(&udev->dev, "video_register_device failed\n");
  1325. goto fail;
  1326. }
  1327. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1328. video_device_node_name(&cam->vdev));
  1329. return 0;
  1330. fail:
  1331. v4l2_ctrl_handler_free(hdl);
  1332. v4l2_device_unregister(&cam->v4l2_dev);
  1333. kfree(cam);
  1334. return err;
  1335. }
  1336. static void zr364xx_disconnect(struct usb_interface *intf)
  1337. {
  1338. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1339. mutex_lock(&cam->lock);
  1340. usb_set_intfdata(intf, NULL);
  1341. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1342. video_unregister_device(&cam->vdev);
  1343. v4l2_device_disconnect(&cam->v4l2_dev);
  1344. /* stops the read pipe if it is running */
  1345. if (cam->b_acquire)
  1346. zr364xx_stop_acquire(cam);
  1347. zr364xx_stop_readpipe(cam);
  1348. mutex_unlock(&cam->lock);
  1349. v4l2_device_put(&cam->v4l2_dev);
  1350. }
  1351. #ifdef CONFIG_PM
  1352. static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
  1353. {
  1354. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1355. cam->was_streaming = cam->b_acquire;
  1356. if (!cam->was_streaming)
  1357. return 0;
  1358. zr364xx_stop_acquire(cam);
  1359. zr364xx_stop_readpipe(cam);
  1360. return 0;
  1361. }
  1362. static int zr364xx_resume(struct usb_interface *intf)
  1363. {
  1364. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1365. int res;
  1366. if (!cam->was_streaming)
  1367. return 0;
  1368. zr364xx_start_readpipe(cam);
  1369. res = zr364xx_prepare(cam);
  1370. if (!res)
  1371. zr364xx_start_acquire(cam);
  1372. return res;
  1373. }
  1374. #endif
  1375. /**********************/
  1376. /* Module integration */
  1377. /**********************/
  1378. static struct usb_driver zr364xx_driver = {
  1379. .name = "zr364xx",
  1380. .probe = zr364xx_probe,
  1381. .disconnect = zr364xx_disconnect,
  1382. #ifdef CONFIG_PM
  1383. .suspend = zr364xx_suspend,
  1384. .resume = zr364xx_resume,
  1385. .reset_resume = zr364xx_resume,
  1386. #endif
  1387. .id_table = device_table
  1388. };
  1389. module_usb_driver(zr364xx_driver);
  1390. MODULE_AUTHOR(DRIVER_AUTHOR);
  1391. MODULE_DESCRIPTION(DRIVER_DESC);
  1392. MODULE_LICENSE("GPL");
  1393. MODULE_VERSION(DRIVER_VERSION);