zr364xx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Zoran 364xx based USB webcam module version 0.72
  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. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/version.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/usb.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/slab.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/highmem.h>
  35. #include <media/v4l2-common.h>
  36. /* Version Information */
  37. #define DRIVER_VERSION "v0.72"
  38. #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
  39. #define DRIVER_DESC "Zoran 364xx"
  40. /* Camera */
  41. #define FRAMES 2
  42. #define MAX_FRAME_SIZE 100000
  43. #define BUFFER_SIZE 0x1000
  44. #define CTRL_TIMEOUT 500
  45. /* Debug macro */
  46. #define DBG(x...) if (debug) info(x)
  47. /* Init methods, need to find nicer names for these
  48. * the exact names of the chipsets would be the best if someone finds it */
  49. #define METHOD0 0
  50. #define METHOD1 1
  51. #define METHOD2 2
  52. /* Module parameters */
  53. static int debug = 0;
  54. static int mode = 0;
  55. /* Module parameters interface */
  56. module_param(debug, int, 0644);
  57. MODULE_PARM_DESC(debug, "Debug level");
  58. module_param(mode, int, 0644);
  59. MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
  60. /* Devices supported by this driver
  61. * .driver_info contains the init method used by the camera */
  62. static struct usb_device_id device_table[] = {
  63. {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
  64. {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
  65. {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
  66. {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
  67. {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
  68. {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
  69. {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
  70. {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
  71. {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
  72. {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
  73. {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
  74. {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
  75. {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
  76. {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
  77. {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
  78. {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
  79. {} /* Terminating entry */
  80. };
  81. MODULE_DEVICE_TABLE(usb, device_table);
  82. /* Camera stuff */
  83. struct zr364xx_camera {
  84. struct usb_device *udev; /* save off the usb device pointer */
  85. struct usb_interface *interface;/* the interface for this device */
  86. struct video_device *vdev; /* v4l video device */
  87. u8 *framebuf;
  88. int nb;
  89. unsigned char *buffer;
  90. int skip;
  91. int brightness;
  92. int width;
  93. int height;
  94. int method;
  95. struct mutex lock;
  96. };
  97. /* function used to send initialisation commands to the camera */
  98. static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
  99. u16 index, unsigned char *cp, u16 size)
  100. {
  101. int status;
  102. unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
  103. if (!transfer_buffer) {
  104. info("kmalloc(%d) failed", size);
  105. return -ENOMEM;
  106. }
  107. memcpy(transfer_buffer, cp, size);
  108. status = usb_control_msg(udev,
  109. usb_sndctrlpipe(udev, 0),
  110. request,
  111. USB_DIR_OUT | USB_TYPE_VENDOR |
  112. USB_RECIP_DEVICE, value, index,
  113. transfer_buffer, size, CTRL_TIMEOUT);
  114. kfree(transfer_buffer);
  115. if (status < 0)
  116. info("Failed sending control message, error %d.", status);
  117. return status;
  118. }
  119. /* Control messages sent to the camera to initialize it
  120. * and launch the capture */
  121. typedef struct {
  122. unsigned int value;
  123. unsigned int size;
  124. unsigned char *bytes;
  125. } message;
  126. /* method 0 */
  127. static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  128. static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
  129. static unsigned char m0d3[] = { 0, 0 };
  130. static message m0[] = {
  131. {0x1f30, 0, NULL},
  132. {0xd000, 0, NULL},
  133. {0x3370, sizeof(m0d1), m0d1},
  134. {0x2000, 0, NULL},
  135. {0x2f0f, 0, NULL},
  136. {0x2610, sizeof(m0d2), m0d2},
  137. {0xe107, 0, NULL},
  138. {0x2502, 0, NULL},
  139. {0x1f70, 0, NULL},
  140. {0xd000, 0, NULL},
  141. {0x9a01, sizeof(m0d3), m0d3},
  142. {-1, -1, NULL}
  143. };
  144. /* method 1 */
  145. static unsigned char m1d1[] = { 0xff, 0xff };
  146. static unsigned char m1d2[] = { 0x00, 0x00 };
  147. static message m1[] = {
  148. {0x1f30, 0, NULL},
  149. {0xd000, 0, NULL},
  150. {0xf000, 0, NULL},
  151. {0x2000, 0, NULL},
  152. {0x2f0f, 0, NULL},
  153. {0x2650, 0, NULL},
  154. {0xe107, 0, NULL},
  155. {0x2502, sizeof(m1d1), m1d1},
  156. {0x1f70, 0, NULL},
  157. {0xd000, 0, NULL},
  158. {0xd000, 0, NULL},
  159. {0xd000, 0, NULL},
  160. {0x9a01, sizeof(m1d2), m1d2},
  161. {-1, -1, NULL}
  162. };
  163. /* method 2 */
  164. static unsigned char m2d1[] = { 0xff, 0xff };
  165. static message m2[] = {
  166. {0x1f30, 0, NULL},
  167. {0xf000, 0, NULL},
  168. {0x2000, 0, NULL},
  169. {0x2f0f, 0, NULL},
  170. {0x2650, 0, NULL},
  171. {0xe107, 0, NULL},
  172. {0x2502, sizeof(m2d1), m2d1},
  173. {0x1f70, 0, NULL},
  174. {-1, -1, NULL}
  175. };
  176. /* init table */
  177. static message *init[3] = { m0, m1, m2 };
  178. /* JPEG static data in header (Huffman table, etc) */
  179. static unsigned char header1[] = {
  180. 0xFF, 0xD8,
  181. /*
  182. 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
  183. 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
  184. */
  185. 0xFF, 0xDB, 0x00, 0x84
  186. };
  187. static unsigned char header2[] = {
  188. 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
  189. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  191. 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  192. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
  193. 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  194. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  195. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
  196. 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
  197. 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  198. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
  199. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
  200. 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
  201. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
  202. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  203. 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  204. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  205. 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  206. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
  207. 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
  208. 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  209. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  210. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
  211. 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
  212. 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  213. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  214. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
  215. 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
  216. 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
  217. 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  218. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
  219. 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  220. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
  221. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  222. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  223. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
  224. 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
  225. 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
  226. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  227. 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
  228. 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
  229. 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
  230. 0x00, 0x3F, 0x00
  231. };
  232. static unsigned char header3;
  233. /********************/
  234. /* V4L2 integration */
  235. /********************/
  236. /* this function reads a full JPEG picture synchronously
  237. * TODO: do it asynchronously... */
  238. static int read_frame(struct zr364xx_camera *cam, int framenum)
  239. {
  240. int i, n, temp, head, size, actual_length;
  241. unsigned char *ptr = NULL, *jpeg, swap;
  242. redo:
  243. /* hardware brightness */
  244. n = send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  245. temp = (0x60 << 8) + 127 - cam->brightness;
  246. n = send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  247. /* during the first loop we are going to insert JPEG header */
  248. head = 0;
  249. /* this is the place in memory where we are going to build
  250. * the JPEG image */
  251. jpeg = cam->framebuf + framenum * MAX_FRAME_SIZE;
  252. /* read data... */
  253. do {
  254. n = usb_bulk_msg(cam->udev,
  255. usb_rcvbulkpipe(cam->udev, 0x81),
  256. cam->buffer, BUFFER_SIZE, &actual_length,
  257. CTRL_TIMEOUT);
  258. DBG("buffer : %d %d", cam->buffer[0], cam->buffer[1]);
  259. DBG("bulk : n=%d size=%d", n, actual_length);
  260. if (n < 0) {
  261. info("error reading bulk msg");
  262. return 0;
  263. }
  264. if (actual_length < 0 || actual_length > BUFFER_SIZE) {
  265. info("wrong number of bytes");
  266. return 0;
  267. }
  268. /* swap bytes if camera needs it */
  269. if (cam->method == METHOD0)
  270. for (i = 0; i < BUFFER_SIZE; i += 2) {
  271. swap = cam->buffer[i];
  272. cam->buffer[i] = cam->buffer[i + 1];
  273. cam->buffer[i + 1] = swap;
  274. }
  275. /* write the JPEG header */
  276. if (!head) {
  277. DBG("jpeg header");
  278. ptr = jpeg;
  279. memcpy(ptr, header1, sizeof(header1));
  280. ptr += sizeof(header1);
  281. header3 = 0;
  282. memcpy(ptr, &header3, 1);
  283. ptr++;
  284. memcpy(ptr, cam->buffer, 64);
  285. ptr += 64;
  286. header3 = 1;
  287. memcpy(ptr, &header3, 1);
  288. ptr++;
  289. memcpy(ptr, cam->buffer + 64, 64);
  290. ptr += 64;
  291. memcpy(ptr, header2, sizeof(header2));
  292. ptr += sizeof(header2);
  293. memcpy(ptr, cam->buffer + 128,
  294. actual_length - 128);
  295. ptr += actual_length - 128;
  296. head = 1;
  297. DBG("header : %d %d %d %d %d %d %d %d %d",
  298. cam->buffer[0], cam->buffer[1], cam->buffer[2],
  299. cam->buffer[3], cam->buffer[4], cam->buffer[5],
  300. cam->buffer[6], cam->buffer[7], cam->buffer[8]);
  301. } else {
  302. memcpy(ptr, cam->buffer, actual_length);
  303. ptr += actual_length;
  304. }
  305. }
  306. /* ... until there is no more */
  307. while (actual_length == BUFFER_SIZE);
  308. /* we skip the 2 first frames which are usually buggy */
  309. if (cam->skip) {
  310. cam->skip--;
  311. goto redo;
  312. }
  313. /* go back to find the JPEG EOI marker */
  314. size = ptr - jpeg;
  315. ptr -= 2;
  316. while (ptr > jpeg) {
  317. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  318. && *(ptr + 2) == 0xFF)
  319. break;
  320. ptr--;
  321. }
  322. if (ptr == jpeg)
  323. DBG("No EOI marker");
  324. /* Sometimes there is junk data in the middle of the picture,
  325. * we want to skip this bogus frames */
  326. while (ptr > jpeg) {
  327. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  328. && *(ptr + 2) == 0xFF)
  329. break;
  330. ptr--;
  331. }
  332. if (ptr != jpeg) {
  333. DBG("Bogus frame ? %d", cam->nb);
  334. goto redo;
  335. }
  336. DBG("jpeg : %d %d %d %d %d %d %d %d",
  337. jpeg[0], jpeg[1], jpeg[2], jpeg[3],
  338. jpeg[4], jpeg[5], jpeg[6], jpeg[7]);
  339. return size;
  340. }
  341. static ssize_t zr364xx_read(struct file *file, char *buf, size_t cnt,
  342. loff_t * ppos)
  343. {
  344. unsigned long count = cnt;
  345. struct video_device *vdev = video_devdata(file);
  346. struct zr364xx_camera *cam;
  347. DBG("zr364xx_read: read %d bytes.", (int) count);
  348. if (vdev == NULL)
  349. return -ENODEV;
  350. cam = video_get_drvdata(vdev);
  351. if (!buf)
  352. return -EINVAL;
  353. if (!count)
  354. return -EINVAL;
  355. /* NoMan Sux ! */
  356. count = read_frame(cam, 0);
  357. if (copy_to_user(buf, cam->framebuf, count))
  358. return -EFAULT;
  359. return count;
  360. }
  361. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  362. struct v4l2_capability *cap)
  363. {
  364. memset(cap, 0, sizeof(*cap));
  365. strcpy(cap->driver, DRIVER_DESC);
  366. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  367. return 0;
  368. }
  369. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  370. struct v4l2_input *i)
  371. {
  372. if (i->index != 0)
  373. return -EINVAL;
  374. memset(i, 0, sizeof(*i));
  375. i->index = 0;
  376. strcpy(i->name, DRIVER_DESC " Camera");
  377. i->type = V4L2_INPUT_TYPE_CAMERA;
  378. return 0;
  379. }
  380. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  381. unsigned int *i)
  382. {
  383. *i = 0;
  384. return 0;
  385. }
  386. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  387. unsigned int i)
  388. {
  389. if (i != 0)
  390. return -EINVAL;
  391. return 0;
  392. }
  393. static int zr364xx_vidioc_queryctrl(struct file *file, void *priv,
  394. struct v4l2_queryctrl *c)
  395. {
  396. struct video_device *vdev = video_devdata(file);
  397. struct zr364xx_camera *cam;
  398. if (vdev == NULL)
  399. return -ENODEV;
  400. cam = video_get_drvdata(vdev);
  401. switch (c->id) {
  402. case V4L2_CID_BRIGHTNESS:
  403. c->type = V4L2_CTRL_TYPE_INTEGER;
  404. strcpy(c->name, "Brightness");
  405. c->minimum = 0;
  406. c->maximum = 127;
  407. c->step = 1;
  408. c->default_value = cam->brightness;
  409. c->flags = 0;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. static int zr364xx_vidioc_s_ctrl(struct file *file, void *priv,
  417. struct v4l2_control *c)
  418. {
  419. struct video_device *vdev = video_devdata(file);
  420. struct zr364xx_camera *cam;
  421. if (vdev == NULL)
  422. return -ENODEV;
  423. cam = video_get_drvdata(vdev);
  424. switch (c->id) {
  425. case V4L2_CID_BRIGHTNESS:
  426. cam->brightness = c->value;
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. static int zr364xx_vidioc_g_ctrl(struct file *file, void *priv,
  434. struct v4l2_control *c)
  435. {
  436. struct video_device *vdev = video_devdata(file);
  437. struct zr364xx_camera *cam;
  438. if (vdev == NULL)
  439. return -ENODEV;
  440. cam = video_get_drvdata(vdev);
  441. switch (c->id) {
  442. case V4L2_CID_BRIGHTNESS:
  443. c->value = cam->brightness;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. static int zr364xx_vidioc_enum_fmt_cap(struct file *file,
  451. void *priv, struct v4l2_fmtdesc *f)
  452. {
  453. if (f->index > 0)
  454. return -EINVAL;
  455. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  456. return -EINVAL;
  457. memset(f, 0, sizeof(*f));
  458. f->index = 0;
  459. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  460. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  461. strcpy(f->description, "JPEG");
  462. f->pixelformat = V4L2_PIX_FMT_JPEG;
  463. return 0;
  464. }
  465. static int zr364xx_vidioc_try_fmt_cap(struct file *file, void *priv,
  466. struct v4l2_format *f)
  467. {
  468. struct video_device *vdev = video_devdata(file);
  469. struct zr364xx_camera *cam;
  470. if (vdev == NULL)
  471. return -ENODEV;
  472. cam = video_get_drvdata(vdev);
  473. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  474. return -EINVAL;
  475. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  476. return -EINVAL;
  477. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  478. f->fmt.pix.field != V4L2_FIELD_NONE)
  479. return -EINVAL;
  480. f->fmt.pix.field = V4L2_FIELD_NONE;
  481. f->fmt.pix.width = cam->width;
  482. f->fmt.pix.height = cam->height;
  483. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  484. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  485. f->fmt.pix.colorspace = 0;
  486. f->fmt.pix.priv = 0;
  487. return 0;
  488. }
  489. static int zr364xx_vidioc_g_fmt_cap(struct file *file, void *priv,
  490. struct v4l2_format *f)
  491. {
  492. struct video_device *vdev = video_devdata(file);
  493. struct zr364xx_camera *cam;
  494. if (vdev == NULL)
  495. return -ENODEV;
  496. cam = video_get_drvdata(vdev);
  497. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  498. return -EINVAL;
  499. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  500. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  501. f->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
  502. f->fmt.pix.field = V4L2_FIELD_NONE;
  503. f->fmt.pix.width = cam->width;
  504. f->fmt.pix.height = cam->height;
  505. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  506. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  507. f->fmt.pix.colorspace = 0;
  508. f->fmt.pix.priv = 0;
  509. return 0;
  510. }
  511. static int zr364xx_vidioc_s_fmt_cap(struct file *file, void *priv,
  512. struct v4l2_format *f)
  513. {
  514. struct video_device *vdev = video_devdata(file);
  515. struct zr364xx_camera *cam;
  516. if (vdev == NULL)
  517. return -ENODEV;
  518. cam = video_get_drvdata(vdev);
  519. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  520. return -EINVAL;
  521. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  522. return -EINVAL;
  523. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  524. f->fmt.pix.field != V4L2_FIELD_NONE)
  525. return -EINVAL;
  526. f->fmt.pix.field = V4L2_FIELD_NONE;
  527. f->fmt.pix.width = cam->width;
  528. f->fmt.pix.height = cam->height;
  529. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  530. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  531. f->fmt.pix.colorspace = 0;
  532. f->fmt.pix.priv = 0;
  533. DBG("ok!");
  534. return 0;
  535. }
  536. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  537. enum v4l2_buf_type type)
  538. {
  539. return 0;
  540. }
  541. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  542. enum v4l2_buf_type type)
  543. {
  544. return 0;
  545. }
  546. /* open the camera */
  547. static int zr364xx_open(struct inode *inode, struct file *file)
  548. {
  549. struct video_device *vdev = video_devdata(file);
  550. struct zr364xx_camera *cam = video_get_drvdata(vdev);
  551. struct usb_device *udev = cam->udev;
  552. int i, err;
  553. DBG("zr364xx_open");
  554. cam->skip = 2;
  555. err = video_exclusive_open(inode, file);
  556. if (err < 0)
  557. return err;
  558. if (!cam->framebuf) {
  559. cam->framebuf = vmalloc_32(MAX_FRAME_SIZE * FRAMES);
  560. if (!cam->framebuf) {
  561. info("vmalloc_32 failed!");
  562. return -ENOMEM;
  563. }
  564. }
  565. mutex_lock(&cam->lock);
  566. for (i = 0; init[cam->method][i].size != -1; i++) {
  567. err =
  568. send_control_msg(udev, 1, init[cam->method][i].value,
  569. 0, init[cam->method][i].bytes,
  570. init[cam->method][i].size);
  571. if (err < 0) {
  572. info("error during open sequence: %d", i);
  573. mutex_unlock(&cam->lock);
  574. return err;
  575. }
  576. }
  577. file->private_data = vdev;
  578. /* Added some delay here, since opening/closing the camera quickly,
  579. * like Ekiga does during its startup, can crash the webcam
  580. */
  581. mdelay(100);
  582. mutex_unlock(&cam->lock);
  583. return 0;
  584. }
  585. /* release the camera */
  586. static int zr364xx_release(struct inode *inode, struct file *file)
  587. {
  588. struct video_device *vdev = video_devdata(file);
  589. struct zr364xx_camera *cam;
  590. struct usb_device *udev;
  591. int i, err;
  592. DBG("zr364xx_release");
  593. if (vdev == NULL)
  594. return -ENODEV;
  595. cam = video_get_drvdata(vdev);
  596. udev = cam->udev;
  597. mutex_lock(&cam->lock);
  598. for (i = 0; i < 2; i++) {
  599. err =
  600. send_control_msg(udev, 1, init[cam->method][i].value,
  601. 0, init[i][cam->method].bytes,
  602. init[cam->method][i].size);
  603. if (err < 0) {
  604. info("error during release sequence");
  605. mutex_unlock(&cam->lock);
  606. return err;
  607. }
  608. }
  609. file->private_data = NULL;
  610. video_exclusive_release(inode, file);
  611. /* Added some delay here, since opening/closing the camera quickly,
  612. * like Ekiga does during its startup, can crash the webcam
  613. */
  614. mdelay(100);
  615. mutex_unlock(&cam->lock);
  616. return 0;
  617. }
  618. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  619. {
  620. void *pos;
  621. unsigned long start = vma->vm_start;
  622. unsigned long size = vma->vm_end - vma->vm_start;
  623. struct video_device *vdev = video_devdata(file);
  624. struct zr364xx_camera *cam;
  625. DBG("zr364xx_mmap: %ld\n", size);
  626. if (vdev == NULL)
  627. return -ENODEV;
  628. cam = video_get_drvdata(vdev);
  629. pos = cam->framebuf;
  630. while (size > 0) {
  631. if (vm_insert_page(vma, start, vmalloc_to_page(pos)))
  632. return -EAGAIN;
  633. start += PAGE_SIZE;
  634. pos += PAGE_SIZE;
  635. if (size > PAGE_SIZE)
  636. size -= PAGE_SIZE;
  637. else
  638. size = 0;
  639. }
  640. return 0;
  641. }
  642. static struct file_operations zr364xx_fops = {
  643. .owner = THIS_MODULE,
  644. .open = zr364xx_open,
  645. .release = zr364xx_release,
  646. .read = zr364xx_read,
  647. .mmap = zr364xx_mmap,
  648. .ioctl = video_ioctl2,
  649. .llseek = no_llseek,
  650. };
  651. static struct video_device zr364xx_template = {
  652. .owner = THIS_MODULE,
  653. .name = DRIVER_DESC,
  654. .type = VID_TYPE_CAPTURE,
  655. .fops = &zr364xx_fops,
  656. .release = video_device_release,
  657. .minor = -1,
  658. .vidioc_querycap = zr364xx_vidioc_querycap,
  659. .vidioc_enum_fmt_cap = zr364xx_vidioc_enum_fmt_cap,
  660. .vidioc_try_fmt_cap = zr364xx_vidioc_try_fmt_cap,
  661. .vidioc_s_fmt_cap = zr364xx_vidioc_s_fmt_cap,
  662. .vidioc_g_fmt_cap = zr364xx_vidioc_g_fmt_cap,
  663. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  664. .vidioc_g_input = zr364xx_vidioc_g_input,
  665. .vidioc_s_input = zr364xx_vidioc_s_input,
  666. .vidioc_streamon = zr364xx_vidioc_streamon,
  667. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  668. .vidioc_queryctrl = zr364xx_vidioc_queryctrl,
  669. .vidioc_g_ctrl = zr364xx_vidioc_g_ctrl,
  670. .vidioc_s_ctrl = zr364xx_vidioc_s_ctrl,
  671. };
  672. /*******************/
  673. /* USB integration */
  674. /*******************/
  675. static int zr364xx_probe(struct usb_interface *intf,
  676. const struct usb_device_id *id)
  677. {
  678. struct usb_device *udev = interface_to_usbdev(intf);
  679. struct zr364xx_camera *cam = NULL;
  680. DBG("probing...");
  681. info(DRIVER_DESC " compatible webcam plugged");
  682. info("model %04x:%04x detected", udev->descriptor.idVendor,
  683. udev->descriptor.idProduct);
  684. if ((cam =
  685. kmalloc(sizeof(struct zr364xx_camera), GFP_KERNEL)) == NULL) {
  686. info("cam: out of memory !");
  687. return -ENODEV;
  688. }
  689. memset(cam, 0x00, sizeof(struct zr364xx_camera));
  690. /* save the init method used by this camera */
  691. cam->method = id->driver_info;
  692. cam->vdev = video_device_alloc();
  693. if (cam->vdev == NULL) {
  694. info("cam->vdev: out of memory !");
  695. kfree(cam);
  696. return -ENODEV;
  697. }
  698. memcpy(cam->vdev, &zr364xx_template, sizeof(zr364xx_template));
  699. video_set_drvdata(cam->vdev, cam);
  700. if (debug)
  701. cam->vdev->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  702. cam->udev = udev;
  703. if ((cam->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL)) == NULL) {
  704. info("cam->buffer: out of memory !");
  705. video_device_release(cam->vdev);
  706. kfree(cam);
  707. return -ENODEV;
  708. }
  709. switch (mode) {
  710. case 1:
  711. info("160x120 mode selected");
  712. cam->width = 160;
  713. cam->height = 120;
  714. break;
  715. case 2:
  716. info("640x480 mode selected");
  717. cam->width = 640;
  718. cam->height = 480;
  719. break;
  720. default:
  721. info("320x240 mode selected");
  722. cam->width = 320;
  723. cam->height = 240;
  724. break;
  725. }
  726. m0d1[0] = mode;
  727. m1[2].value = 0xf000 + mode;
  728. m2[1].value = 0xf000 + mode;
  729. header2[437] = cam->height / 256;
  730. header2[438] = cam->height % 256;
  731. header2[439] = cam->width / 256;
  732. header2[440] = cam->width % 256;
  733. cam->nb = 0;
  734. cam->brightness = 64;
  735. mutex_init(&cam->lock);
  736. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, -1) == -1) {
  737. info("video_register_device failed");
  738. video_device_release(cam->vdev);
  739. kfree(cam->buffer);
  740. kfree(cam);
  741. return -ENODEV;
  742. }
  743. usb_set_intfdata(intf, cam);
  744. info(DRIVER_DESC " controlling video device %d", cam->vdev->minor);
  745. return 0;
  746. }
  747. static void zr364xx_disconnect(struct usb_interface *intf)
  748. {
  749. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  750. usb_set_intfdata(intf, NULL);
  751. dev_set_drvdata(&intf->dev, NULL);
  752. info(DRIVER_DESC " webcam unplugged");
  753. if (cam->vdev)
  754. video_unregister_device(cam->vdev);
  755. cam->vdev = NULL;
  756. kfree(cam->buffer);
  757. if (cam->framebuf)
  758. vfree(cam->framebuf);
  759. kfree(cam);
  760. }
  761. /**********************/
  762. /* Module integration */
  763. /**********************/
  764. static struct usb_driver zr364xx_driver = {
  765. .name = "zr364xx",
  766. .probe = zr364xx_probe,
  767. .disconnect = zr364xx_disconnect,
  768. .id_table = device_table
  769. };
  770. static int __init zr364xx_init(void)
  771. {
  772. int retval;
  773. retval = usb_register(&zr364xx_driver) < 0;
  774. if (retval)
  775. info("usb_register failed!");
  776. else
  777. info(DRIVER_DESC " module loaded");
  778. return retval;
  779. }
  780. static void __exit zr364xx_exit(void)
  781. {
  782. info(DRIVER_DESC " module unloaded");
  783. usb_deregister(&zr364xx_driver);
  784. }
  785. module_init(zr364xx_init);
  786. module_exit(zr364xx_exit);
  787. MODULE_AUTHOR(DRIVER_AUTHOR);
  788. MODULE_DESCRIPTION(DRIVER_DESC);
  789. MODULE_LICENSE("GPL");