zr364xx.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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. if (purb->actual_length < 128) {
  523. /* header incomplete */
  524. dev_info(&cam->udev->dev,
  525. "%s: buffer (%d bytes) too small to hold jpeg header. Discarding.\n",
  526. __func__, purb->actual_length);
  527. return -EINVAL;
  528. }
  529. frm->ulState = ZR364XX_READ_FRAME;
  530. frm->cur_size = 0;
  531. _DBG("jpeg header, ");
  532. memcpy(ptr, header1, sizeof(header1));
  533. ptr += sizeof(header1);
  534. header3 = 0;
  535. memcpy(ptr, &header3, 1);
  536. ptr++;
  537. memcpy(ptr, psrc, 64);
  538. ptr += 64;
  539. header3 = 1;
  540. memcpy(ptr, &header3, 1);
  541. ptr++;
  542. memcpy(ptr, psrc + 64, 64);
  543. ptr += 64;
  544. memcpy(ptr, header2, sizeof(header2));
  545. ptr += sizeof(header2);
  546. memcpy(ptr, psrc + 128,
  547. purb->actual_length - 128);
  548. ptr += purb->actual_length - 128;
  549. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  550. psrc[0], psrc[1], psrc[2],
  551. psrc[3], psrc[4], psrc[5],
  552. psrc[6], psrc[7], psrc[8]);
  553. frm->cur_size = ptr - pdest;
  554. } else {
  555. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  556. dev_info(&cam->udev->dev,
  557. "%s: buffer (%d bytes) too small to hold "
  558. "frame data. Discarding frame data.\n",
  559. __func__, MAX_FRAME_SIZE);
  560. } else {
  561. pdest += frm->cur_size;
  562. memcpy(pdest, psrc, purb->actual_length);
  563. frm->cur_size += purb->actual_length;
  564. }
  565. }
  566. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  567. purb->actual_length);*/
  568. if (purb->actual_length < pipe_info->transfer_size) {
  569. _DBG("****************Buffer[%d]full*************\n", idx);
  570. cam->last_frame = cam->cur_frame;
  571. cam->cur_frame++;
  572. /* end of system frame ring buffer, start at zero */
  573. if (cam->cur_frame == cam->buffer.dwFrames)
  574. cam->cur_frame = 0;
  575. /* frame ready */
  576. /* go back to find the JPEG EOI marker */
  577. ptr = pdest = frm->lpvbits;
  578. ptr += frm->cur_size - 2;
  579. while (ptr > pdest) {
  580. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  581. && *(ptr + 2) == 0xFF)
  582. break;
  583. ptr--;
  584. }
  585. if (ptr == pdest)
  586. DBG("No EOI marker\n");
  587. /* Sometimes there is junk data in the middle of the picture,
  588. * we want to skip this bogus frames */
  589. while (ptr > pdest) {
  590. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  591. && *(ptr + 2) == 0xFF)
  592. break;
  593. ptr--;
  594. }
  595. if (ptr != pdest) {
  596. DBG("Bogus frame ? %d\n", ++(cam->nb));
  597. } else if (cam->b_acquire) {
  598. /* we skip the 2 first frames which are usually buggy */
  599. if (cam->skip)
  600. cam->skip--;
  601. else {
  602. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  603. frm->cur_size,
  604. pdest[0], pdest[1], pdest[2], pdest[3],
  605. pdest[4], pdest[5], pdest[6], pdest[7]);
  606. zr364xx_got_frame(cam, frm->cur_size);
  607. }
  608. }
  609. cam->frame_count++;
  610. frm->ulState = ZR364XX_READ_IDLE;
  611. frm->cur_size = 0;
  612. }
  613. /* done successfully */
  614. return 0;
  615. }
  616. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  617. struct v4l2_capability *cap)
  618. {
  619. struct zr364xx_camera *cam = video_drvdata(file);
  620. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  621. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  622. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  623. sizeof(cap->bus_info));
  624. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  625. V4L2_CAP_READWRITE |
  626. V4L2_CAP_STREAMING;
  627. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  628. return 0;
  629. }
  630. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  631. struct v4l2_input *i)
  632. {
  633. if (i->index != 0)
  634. return -EINVAL;
  635. strcpy(i->name, DRIVER_DESC " Camera");
  636. i->type = V4L2_INPUT_TYPE_CAMERA;
  637. return 0;
  638. }
  639. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  640. unsigned int *i)
  641. {
  642. *i = 0;
  643. return 0;
  644. }
  645. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  646. unsigned int i)
  647. {
  648. if (i != 0)
  649. return -EINVAL;
  650. return 0;
  651. }
  652. static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
  653. {
  654. struct zr364xx_camera *cam =
  655. container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
  656. int temp;
  657. switch (ctrl->id) {
  658. case V4L2_CID_BRIGHTNESS:
  659. /* hardware brightness */
  660. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  661. temp = (0x60 << 8) + 127 - ctrl->val;
  662. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  663. break;
  664. default:
  665. return -EINVAL;
  666. }
  667. return 0;
  668. }
  669. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  670. void *priv, struct v4l2_fmtdesc *f)
  671. {
  672. if (f->index > 0)
  673. return -EINVAL;
  674. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  675. strcpy(f->description, formats[0].name);
  676. f->pixelformat = formats[0].fourcc;
  677. return 0;
  678. }
  679. static char *decode_fourcc(__u32 pixelformat, char *buf)
  680. {
  681. buf[0] = pixelformat & 0xff;
  682. buf[1] = (pixelformat >> 8) & 0xff;
  683. buf[2] = (pixelformat >> 16) & 0xff;
  684. buf[3] = (pixelformat >> 24) & 0xff;
  685. buf[4] = '\0';
  686. return buf;
  687. }
  688. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  689. struct v4l2_format *f)
  690. {
  691. struct zr364xx_camera *cam = video_drvdata(file);
  692. char pixelformat_name[5];
  693. if (cam == NULL)
  694. return -ENODEV;
  695. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  696. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  697. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  698. return -EINVAL;
  699. }
  700. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  701. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  702. f->fmt.pix.width = 320;
  703. f->fmt.pix.height = 240;
  704. }
  705. f->fmt.pix.field = V4L2_FIELD_NONE;
  706. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  707. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  708. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  709. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  710. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  711. f->fmt.pix.field);
  712. return 0;
  713. }
  714. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  715. struct v4l2_format *f)
  716. {
  717. struct zr364xx_camera *cam;
  718. if (file == NULL)
  719. return -ENODEV;
  720. cam = video_drvdata(file);
  721. f->fmt.pix.pixelformat = formats[0].fourcc;
  722. f->fmt.pix.field = V4L2_FIELD_NONE;
  723. f->fmt.pix.width = cam->width;
  724. f->fmt.pix.height = cam->height;
  725. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  726. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  727. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  728. return 0;
  729. }
  730. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  731. struct v4l2_format *f)
  732. {
  733. struct zr364xx_camera *cam = video_drvdata(file);
  734. struct videobuf_queue *q = &cam->vb_vidq;
  735. char pixelformat_name[5];
  736. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  737. int i;
  738. if (ret < 0)
  739. return ret;
  740. mutex_lock(&q->vb_lock);
  741. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  742. DBG("%s queue busy\n", __func__);
  743. ret = -EBUSY;
  744. goto out;
  745. }
  746. if (cam->owner) {
  747. DBG("%s can't change format after started\n", __func__);
  748. ret = -EBUSY;
  749. goto out;
  750. }
  751. cam->width = f->fmt.pix.width;
  752. cam->height = f->fmt.pix.height;
  753. DBG("%s: %dx%d mode selected\n", __func__,
  754. cam->width, cam->height);
  755. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  756. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  757. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  758. cam->vb_vidq.field = f->fmt.pix.field;
  759. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  760. mode = 1;
  761. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  762. mode = 2;
  763. else
  764. mode = 0;
  765. m0d1[0] = mode;
  766. m1[2].value = 0xf000 + mode;
  767. m2[1].value = 0xf000 + mode;
  768. /* special case for METHOD3, the modes are different */
  769. if (cam->method == METHOD3) {
  770. switch (mode) {
  771. case 1:
  772. m2[1].value = 0xf000 + 4;
  773. break;
  774. case 2:
  775. m2[1].value = 0xf000 + 0;
  776. break;
  777. default:
  778. m2[1].value = 0xf000 + 1;
  779. break;
  780. }
  781. }
  782. header2[437] = cam->height / 256;
  783. header2[438] = cam->height % 256;
  784. header2[439] = cam->width / 256;
  785. header2[440] = cam->width % 256;
  786. for (i = 0; init[cam->method][i].size != -1; i++) {
  787. ret =
  788. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  789. 0, init[cam->method][i].bytes,
  790. init[cam->method][i].size);
  791. if (ret < 0) {
  792. dev_err(&cam->udev->dev,
  793. "error during resolution change sequence: %d\n", i);
  794. goto out;
  795. }
  796. }
  797. /* Added some delay here, since opening/closing the camera quickly,
  798. * like Ekiga does during its startup, can crash the webcam
  799. */
  800. mdelay(100);
  801. cam->skip = 2;
  802. ret = 0;
  803. out:
  804. mutex_unlock(&q->vb_lock);
  805. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  806. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  807. f->fmt.pix.field);
  808. return ret;
  809. }
  810. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  811. struct v4l2_requestbuffers *p)
  812. {
  813. struct zr364xx_camera *cam = video_drvdata(file);
  814. if (cam->owner && cam->owner != priv)
  815. return -EBUSY;
  816. return videobuf_reqbufs(&cam->vb_vidq, p);
  817. }
  818. static int zr364xx_vidioc_querybuf(struct file *file,
  819. void *priv,
  820. struct v4l2_buffer *p)
  821. {
  822. int rc;
  823. struct zr364xx_camera *cam = video_drvdata(file);
  824. rc = videobuf_querybuf(&cam->vb_vidq, p);
  825. return rc;
  826. }
  827. static int zr364xx_vidioc_qbuf(struct file *file,
  828. void *priv,
  829. struct v4l2_buffer *p)
  830. {
  831. int rc;
  832. struct zr364xx_camera *cam = video_drvdata(file);
  833. _DBG("%s\n", __func__);
  834. if (cam->owner && cam->owner != priv)
  835. return -EBUSY;
  836. rc = videobuf_qbuf(&cam->vb_vidq, p);
  837. return rc;
  838. }
  839. static int zr364xx_vidioc_dqbuf(struct file *file,
  840. void *priv,
  841. struct v4l2_buffer *p)
  842. {
  843. int rc;
  844. struct zr364xx_camera *cam = video_drvdata(file);
  845. _DBG("%s\n", __func__);
  846. if (cam->owner && cam->owner != priv)
  847. return -EBUSY;
  848. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  849. return rc;
  850. }
  851. static void read_pipe_completion(struct urb *purb)
  852. {
  853. struct zr364xx_pipeinfo *pipe_info;
  854. struct zr364xx_camera *cam;
  855. int pipe;
  856. pipe_info = purb->context;
  857. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  858. if (pipe_info == NULL) {
  859. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  860. return;
  861. }
  862. cam = pipe_info->cam;
  863. if (cam == NULL) {
  864. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  865. return;
  866. }
  867. /* if shutting down, do not resubmit, exit immediately */
  868. if (purb->status == -ESHUTDOWN) {
  869. DBG("%s, err shutdown\n", __func__);
  870. pipe_info->err_count++;
  871. return;
  872. }
  873. if (pipe_info->state == 0) {
  874. DBG("exiting USB pipe\n");
  875. return;
  876. }
  877. if (purb->actual_length > pipe_info->transfer_size) {
  878. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  879. return;
  880. }
  881. if (purb->status == 0)
  882. zr364xx_read_video_callback(cam, pipe_info, purb);
  883. else {
  884. pipe_info->err_count++;
  885. DBG("%s: failed URB %d\n", __func__, purb->status);
  886. }
  887. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  888. /* reuse urb */
  889. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  890. pipe,
  891. pipe_info->transfer_buffer,
  892. pipe_info->transfer_size,
  893. read_pipe_completion, pipe_info);
  894. if (pipe_info->state != 0) {
  895. purb->status = usb_submit_urb(pipe_info->stream_urb,
  896. GFP_ATOMIC);
  897. if (purb->status)
  898. dev_err(&cam->udev->dev,
  899. "error submitting urb (error=%i)\n",
  900. purb->status);
  901. } else
  902. DBG("read pipe complete state 0\n");
  903. }
  904. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  905. {
  906. int pipe;
  907. int retval;
  908. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  909. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  910. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  911. pipe_info->state = 1;
  912. pipe_info->err_count = 0;
  913. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  914. if (!pipe_info->stream_urb) {
  915. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  916. return -ENOMEM;
  917. }
  918. /* transfer buffer allocated in board_init */
  919. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  920. pipe,
  921. pipe_info->transfer_buffer,
  922. pipe_info->transfer_size,
  923. read_pipe_completion, pipe_info);
  924. DBG("submitting URB %p\n", pipe_info->stream_urb);
  925. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  926. if (retval) {
  927. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  928. return retval;
  929. }
  930. return 0;
  931. }
  932. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  933. {
  934. struct zr364xx_pipeinfo *pipe_info;
  935. if (cam == NULL) {
  936. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  937. return;
  938. }
  939. DBG("stop read pipe\n");
  940. pipe_info = cam->pipe;
  941. if (pipe_info) {
  942. if (pipe_info->state != 0)
  943. pipe_info->state = 0;
  944. if (pipe_info->stream_urb) {
  945. /* cancel urb */
  946. usb_kill_urb(pipe_info->stream_urb);
  947. usb_free_urb(pipe_info->stream_urb);
  948. pipe_info->stream_urb = NULL;
  949. }
  950. }
  951. return;
  952. }
  953. /* starts acquisition process */
  954. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  955. {
  956. int j;
  957. DBG("start acquire\n");
  958. cam->last_frame = -1;
  959. cam->cur_frame = 0;
  960. for (j = 0; j < FRAMES; j++) {
  961. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  962. cam->buffer.frame[j].cur_size = 0;
  963. }
  964. cam->b_acquire = 1;
  965. return 0;
  966. }
  967. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  968. {
  969. cam->b_acquire = 0;
  970. return 0;
  971. }
  972. static int zr364xx_prepare(struct zr364xx_camera *cam)
  973. {
  974. int res;
  975. int i, j;
  976. for (i = 0; init[cam->method][i].size != -1; i++) {
  977. res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
  978. 0, init[cam->method][i].bytes,
  979. init[cam->method][i].size);
  980. if (res < 0) {
  981. dev_err(&cam->udev->dev,
  982. "error during open sequence: %d\n", i);
  983. return res;
  984. }
  985. }
  986. cam->skip = 2;
  987. cam->last_frame = -1;
  988. cam->cur_frame = 0;
  989. cam->frame_count = 0;
  990. for (j = 0; j < FRAMES; j++) {
  991. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  992. cam->buffer.frame[j].cur_size = 0;
  993. }
  994. v4l2_ctrl_handler_setup(&cam->ctrl_handler);
  995. return 0;
  996. }
  997. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  998. enum v4l2_buf_type type)
  999. {
  1000. struct zr364xx_camera *cam = video_drvdata(file);
  1001. int res;
  1002. DBG("%s\n", __func__);
  1003. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1004. return -EINVAL;
  1005. if (cam->owner && cam->owner != priv)
  1006. return -EBUSY;
  1007. res = zr364xx_prepare(cam);
  1008. if (res)
  1009. return res;
  1010. res = videobuf_streamon(&cam->vb_vidq);
  1011. if (res == 0) {
  1012. zr364xx_start_acquire(cam);
  1013. cam->owner = file->private_data;
  1014. }
  1015. return res;
  1016. }
  1017. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1018. enum v4l2_buf_type type)
  1019. {
  1020. struct zr364xx_camera *cam = video_drvdata(file);
  1021. DBG("%s\n", __func__);
  1022. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1023. return -EINVAL;
  1024. if (cam->owner && cam->owner != priv)
  1025. return -EBUSY;
  1026. zr364xx_stop_acquire(cam);
  1027. return videobuf_streamoff(&cam->vb_vidq);
  1028. }
  1029. /* open the camera */
  1030. static int zr364xx_open(struct file *file)
  1031. {
  1032. struct zr364xx_camera *cam = video_drvdata(file);
  1033. int err;
  1034. DBG("%s\n", __func__);
  1035. if (mutex_lock_interruptible(&cam->lock))
  1036. return -ERESTARTSYS;
  1037. err = v4l2_fh_open(file);
  1038. if (err)
  1039. goto out;
  1040. /* Added some delay here, since opening/closing the camera quickly,
  1041. * like Ekiga does during its startup, can crash the webcam
  1042. */
  1043. mdelay(100);
  1044. err = 0;
  1045. out:
  1046. mutex_unlock(&cam->lock);
  1047. DBG("%s: %d\n", __func__, err);
  1048. return err;
  1049. }
  1050. static void zr364xx_release(struct v4l2_device *v4l2_dev)
  1051. {
  1052. struct zr364xx_camera *cam =
  1053. container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
  1054. unsigned long i;
  1055. v4l2_device_unregister(&cam->v4l2_dev);
  1056. videobuf_mmap_free(&cam->vb_vidq);
  1057. /* release sys buffers */
  1058. for (i = 0; i < FRAMES; i++) {
  1059. if (cam->buffer.frame[i].lpvbits) {
  1060. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1061. vfree(cam->buffer.frame[i].lpvbits);
  1062. }
  1063. cam->buffer.frame[i].lpvbits = NULL;
  1064. }
  1065. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1066. /* release transfer buffer */
  1067. kfree(cam->pipe->transfer_buffer);
  1068. kfree(cam);
  1069. }
  1070. /* release the camera */
  1071. static int zr364xx_close(struct file *file)
  1072. {
  1073. struct zr364xx_camera *cam;
  1074. struct usb_device *udev;
  1075. int i;
  1076. DBG("%s\n", __func__);
  1077. cam = video_drvdata(file);
  1078. mutex_lock(&cam->lock);
  1079. udev = cam->udev;
  1080. if (file->private_data == cam->owner) {
  1081. /* turn off stream */
  1082. if (cam->b_acquire)
  1083. zr364xx_stop_acquire(cam);
  1084. videobuf_streamoff(&cam->vb_vidq);
  1085. for (i = 0; i < 2; i++) {
  1086. send_control_msg(udev, 1, init[cam->method][i].value,
  1087. 0, init[cam->method][i].bytes,
  1088. init[cam->method][i].size);
  1089. }
  1090. cam->owner = NULL;
  1091. }
  1092. /* Added some delay here, since opening/closing the camera quickly,
  1093. * like Ekiga does during its startup, can crash the webcam
  1094. */
  1095. mdelay(100);
  1096. mutex_unlock(&cam->lock);
  1097. return v4l2_fh_release(file);
  1098. }
  1099. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1100. {
  1101. struct zr364xx_camera *cam = video_drvdata(file);
  1102. int ret;
  1103. if (cam == NULL) {
  1104. DBG("%s: cam == NULL\n", __func__);
  1105. return -ENODEV;
  1106. }
  1107. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1108. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1109. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1110. (unsigned long)vma->vm_start,
  1111. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1112. return ret;
  1113. }
  1114. static unsigned int zr364xx_poll(struct file *file,
  1115. struct poll_table_struct *wait)
  1116. {
  1117. struct zr364xx_camera *cam = video_drvdata(file);
  1118. struct videobuf_queue *q = &cam->vb_vidq;
  1119. unsigned res = v4l2_ctrl_poll(file, wait);
  1120. _DBG("%s\n", __func__);
  1121. return res | videobuf_poll_stream(file, q, wait);
  1122. }
  1123. static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
  1124. .s_ctrl = zr364xx_s_ctrl,
  1125. };
  1126. static const struct v4l2_file_operations zr364xx_fops = {
  1127. .owner = THIS_MODULE,
  1128. .open = zr364xx_open,
  1129. .release = zr364xx_close,
  1130. .read = zr364xx_read,
  1131. .mmap = zr364xx_mmap,
  1132. .unlocked_ioctl = video_ioctl2,
  1133. .poll = zr364xx_poll,
  1134. };
  1135. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1136. .vidioc_querycap = zr364xx_vidioc_querycap,
  1137. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1138. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1139. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1140. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1141. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1142. .vidioc_g_input = zr364xx_vidioc_g_input,
  1143. .vidioc_s_input = zr364xx_vidioc_s_input,
  1144. .vidioc_streamon = zr364xx_vidioc_streamon,
  1145. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1146. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1147. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1148. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1149. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1150. .vidioc_log_status = v4l2_ctrl_log_status,
  1151. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1152. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1153. };
  1154. static struct video_device zr364xx_template = {
  1155. .name = DRIVER_DESC,
  1156. .fops = &zr364xx_fops,
  1157. .ioctl_ops = &zr364xx_ioctl_ops,
  1158. .release = video_device_release_empty,
  1159. };
  1160. /*******************/
  1161. /* USB integration */
  1162. /*******************/
  1163. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1164. {
  1165. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1166. unsigned long i;
  1167. DBG("board init: %p\n", cam);
  1168. memset(pipe, 0, sizeof(*pipe));
  1169. pipe->cam = cam;
  1170. pipe->transfer_size = BUFFER_SIZE;
  1171. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1172. GFP_KERNEL);
  1173. if (pipe->transfer_buffer == NULL) {
  1174. DBG("out of memory!\n");
  1175. return -ENOMEM;
  1176. }
  1177. cam->b_acquire = 0;
  1178. cam->frame_count = 0;
  1179. /*** start create system buffers ***/
  1180. for (i = 0; i < FRAMES; i++) {
  1181. /* always allocate maximum size for system buffers */
  1182. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1183. DBG("valloc %p, idx %lu, pdata %p\n",
  1184. &cam->buffer.frame[i], i,
  1185. cam->buffer.frame[i].lpvbits);
  1186. if (cam->buffer.frame[i].lpvbits == NULL) {
  1187. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1188. "Using less frames\n");
  1189. break;
  1190. }
  1191. }
  1192. if (i == 0) {
  1193. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1194. kfree(cam->pipe->transfer_buffer);
  1195. cam->pipe->transfer_buffer = NULL;
  1196. return -ENOMEM;
  1197. } else
  1198. cam->buffer.dwFrames = i;
  1199. /* make sure internal states are set */
  1200. for (i = 0; i < FRAMES; i++) {
  1201. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1202. cam->buffer.frame[i].cur_size = 0;
  1203. }
  1204. cam->cur_frame = 0;
  1205. cam->last_frame = -1;
  1206. /*** end create system buffers ***/
  1207. /* start read pipe */
  1208. zr364xx_start_readpipe(cam);
  1209. DBG(": board initialized\n");
  1210. return 0;
  1211. }
  1212. static int zr364xx_probe(struct usb_interface *intf,
  1213. const struct usb_device_id *id)
  1214. {
  1215. struct usb_device *udev = interface_to_usbdev(intf);
  1216. struct zr364xx_camera *cam = NULL;
  1217. struct usb_host_interface *iface_desc;
  1218. struct usb_endpoint_descriptor *endpoint;
  1219. struct v4l2_ctrl_handler *hdl;
  1220. int err;
  1221. int i;
  1222. DBG("probing...\n");
  1223. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1224. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1225. le16_to_cpu(udev->descriptor.idVendor),
  1226. le16_to_cpu(udev->descriptor.idProduct));
  1227. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1228. if (cam == NULL) {
  1229. dev_err(&udev->dev, "cam: out of memory !\n");
  1230. return -ENOMEM;
  1231. }
  1232. cam->v4l2_dev.release = zr364xx_release;
  1233. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  1234. if (err < 0) {
  1235. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1236. kfree(cam);
  1237. return err;
  1238. }
  1239. hdl = &cam->ctrl_handler;
  1240. v4l2_ctrl_handler_init(hdl, 1);
  1241. v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
  1242. V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
  1243. if (hdl->error) {
  1244. err = hdl->error;
  1245. dev_err(&udev->dev, "couldn't register control\n");
  1246. goto fail;
  1247. }
  1248. /* save the init method used by this camera */
  1249. cam->method = id->driver_info;
  1250. mutex_init(&cam->lock);
  1251. cam->vdev = zr364xx_template;
  1252. cam->vdev.lock = &cam->lock;
  1253. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1254. cam->vdev.ctrl_handler = &cam->ctrl_handler;
  1255. video_set_drvdata(&cam->vdev, cam);
  1256. cam->udev = udev;
  1257. switch (mode) {
  1258. case 1:
  1259. dev_info(&udev->dev, "160x120 mode selected\n");
  1260. cam->width = 160;
  1261. cam->height = 120;
  1262. break;
  1263. case 2:
  1264. dev_info(&udev->dev, "640x480 mode selected\n");
  1265. cam->width = 640;
  1266. cam->height = 480;
  1267. break;
  1268. default:
  1269. dev_info(&udev->dev, "320x240 mode selected\n");
  1270. cam->width = 320;
  1271. cam->height = 240;
  1272. break;
  1273. }
  1274. m0d1[0] = mode;
  1275. m1[2].value = 0xf000 + mode;
  1276. m2[1].value = 0xf000 + mode;
  1277. /* special case for METHOD3, the modes are different */
  1278. if (cam->method == METHOD3) {
  1279. switch (mode) {
  1280. case 1:
  1281. m2[1].value = 0xf000 + 4;
  1282. break;
  1283. case 2:
  1284. m2[1].value = 0xf000 + 0;
  1285. break;
  1286. default:
  1287. m2[1].value = 0xf000 + 1;
  1288. break;
  1289. }
  1290. }
  1291. header2[437] = cam->height / 256;
  1292. header2[438] = cam->height % 256;
  1293. header2[439] = cam->width / 256;
  1294. header2[440] = cam->width % 256;
  1295. cam->nb = 0;
  1296. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1297. /* set up the endpoint information */
  1298. iface_desc = intf->cur_altsetting;
  1299. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1300. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1301. endpoint = &iface_desc->endpoint[i].desc;
  1302. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1303. /* we found the bulk in endpoint */
  1304. cam->read_endpoint = endpoint->bEndpointAddress;
  1305. }
  1306. }
  1307. if (!cam->read_endpoint) {
  1308. err = -ENOMEM;
  1309. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1310. goto fail;
  1311. }
  1312. /* v4l */
  1313. INIT_LIST_HEAD(&cam->vidq.active);
  1314. cam->vidq.cam = cam;
  1315. usb_set_intfdata(intf, cam);
  1316. /* load zr364xx board specific */
  1317. err = zr364xx_board_init(cam);
  1318. if (!err)
  1319. err = v4l2_ctrl_handler_setup(hdl);
  1320. if (err)
  1321. goto fail;
  1322. spin_lock_init(&cam->slock);
  1323. cam->fmt = formats;
  1324. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1325. NULL, &cam->slock,
  1326. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1327. V4L2_FIELD_NONE,
  1328. sizeof(struct zr364xx_buffer), cam, &cam->lock);
  1329. err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1330. if (err) {
  1331. dev_err(&udev->dev, "video_register_device failed\n");
  1332. goto fail;
  1333. }
  1334. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1335. video_device_node_name(&cam->vdev));
  1336. return 0;
  1337. fail:
  1338. v4l2_ctrl_handler_free(hdl);
  1339. v4l2_device_unregister(&cam->v4l2_dev);
  1340. kfree(cam);
  1341. return err;
  1342. }
  1343. static void zr364xx_disconnect(struct usb_interface *intf)
  1344. {
  1345. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1346. mutex_lock(&cam->lock);
  1347. usb_set_intfdata(intf, NULL);
  1348. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1349. video_unregister_device(&cam->vdev);
  1350. v4l2_device_disconnect(&cam->v4l2_dev);
  1351. /* stops the read pipe if it is running */
  1352. if (cam->b_acquire)
  1353. zr364xx_stop_acquire(cam);
  1354. zr364xx_stop_readpipe(cam);
  1355. mutex_unlock(&cam->lock);
  1356. v4l2_device_put(&cam->v4l2_dev);
  1357. }
  1358. #ifdef CONFIG_PM
  1359. static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
  1360. {
  1361. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1362. cam->was_streaming = cam->b_acquire;
  1363. if (!cam->was_streaming)
  1364. return 0;
  1365. zr364xx_stop_acquire(cam);
  1366. zr364xx_stop_readpipe(cam);
  1367. return 0;
  1368. }
  1369. static int zr364xx_resume(struct usb_interface *intf)
  1370. {
  1371. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1372. int res;
  1373. if (!cam->was_streaming)
  1374. return 0;
  1375. zr364xx_start_readpipe(cam);
  1376. res = zr364xx_prepare(cam);
  1377. if (!res)
  1378. zr364xx_start_acquire(cam);
  1379. return res;
  1380. }
  1381. #endif
  1382. /**********************/
  1383. /* Module integration */
  1384. /**********************/
  1385. static struct usb_driver zr364xx_driver = {
  1386. .name = "zr364xx",
  1387. .probe = zr364xx_probe,
  1388. .disconnect = zr364xx_disconnect,
  1389. #ifdef CONFIG_PM
  1390. .suspend = zr364xx_suspend,
  1391. .resume = zr364xx_resume,
  1392. .reset_resume = zr364xx_resume,
  1393. #endif
  1394. .id_table = device_table
  1395. };
  1396. module_usb_driver(zr364xx_driver);
  1397. MODULE_AUTHOR(DRIVER_AUTHOR);
  1398. MODULE_DESCRIPTION(DRIVER_DESC);
  1399. MODULE_LICENSE("GPL");
  1400. MODULE_VERSION(DRIVER_VERSION);