zr364xx.c 42 KB

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