zr364xx.c 24 KB

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