zr364xx.c 42 KB

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