zr364xx.c 42 KB

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