jeilinj.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Jeilinj subdriver
  3. *
  4. * Supports some Jeilin dual-mode cameras which use bulk transport and
  5. * download raw JPEG data.
  6. *
  7. * Copyright (C) 2009 Theodore Kilgore
  8. * Copyright (C) 2011 Patrice Chotard
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #define MODULE_NAME "jeilinj"
  25. #include <linux/slab.h>
  26. #include "gspca.h"
  27. #include "jpeg.h"
  28. MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
  29. MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
  30. MODULE_LICENSE("GPL");
  31. /* Default timeouts, in ms */
  32. #define JEILINJ_CMD_TIMEOUT 500
  33. #define JEILINJ_CMD_DELAY 160
  34. #define JEILINJ_DATA_TIMEOUT 1000
  35. /* Maximum transfer size to use. */
  36. #define JEILINJ_MAX_TRANSFER 0x200
  37. #define FRAME_HEADER_LEN 0x10
  38. #define FRAME_START 0xFFFFFFFF
  39. enum {
  40. SAKAR_57379,
  41. SPORTSCAM_DV15,
  42. };
  43. /* Structure to hold all of our device specific stuff */
  44. struct sd {
  45. struct gspca_dev gspca_dev; /* !! must be the first item */
  46. int blocks_left;
  47. const struct v4l2_pix_format *cap_mode;
  48. /* Driver stuff */
  49. u8 type;
  50. u8 quality; /* image quality */
  51. u8 jpeg_hdr[JPEG_HDR_SZ];
  52. };
  53. struct jlj_command {
  54. unsigned char instruction[2];
  55. unsigned char ack_wanted;
  56. unsigned char delay;
  57. };
  58. /* AFAICT these cameras will only do 320x240. */
  59. static struct v4l2_pix_format jlj_mode[] = {
  60. { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  61. .bytesperline = 320,
  62. .sizeimage = 320 * 240,
  63. .colorspace = V4L2_COLORSPACE_JPEG,
  64. .priv = 0},
  65. { 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  66. .bytesperline = 640,
  67. .sizeimage = 640 * 480,
  68. .colorspace = V4L2_COLORSPACE_JPEG,
  69. .priv = 0}
  70. };
  71. /*
  72. * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
  73. * and 0x82 for bulk transfer.
  74. */
  75. /* All commands are two bytes only */
  76. static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
  77. {
  78. int retval;
  79. if (gspca_dev->usb_err < 0)
  80. return;
  81. memcpy(gspca_dev->usb_buf, command, 2);
  82. retval = usb_bulk_msg(gspca_dev->dev,
  83. usb_sndbulkpipe(gspca_dev->dev, 3),
  84. gspca_dev->usb_buf, 2, NULL, 500);
  85. if (retval < 0) {
  86. err("command write [%02x] error %d",
  87. gspca_dev->usb_buf[0], retval);
  88. gspca_dev->usb_err = retval;
  89. }
  90. }
  91. /* Responses are one byte only */
  92. static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
  93. {
  94. int retval;
  95. if (gspca_dev->usb_err < 0)
  96. return;
  97. retval = usb_bulk_msg(gspca_dev->dev,
  98. usb_rcvbulkpipe(gspca_dev->dev, 0x84),
  99. gspca_dev->usb_buf, 1, NULL, 500);
  100. response = gspca_dev->usb_buf[0];
  101. if (retval < 0) {
  102. err("read command [%02x] error %d",
  103. gspca_dev->usb_buf[0], retval);
  104. gspca_dev->usb_err = retval;
  105. }
  106. }
  107. static int jlj_start(struct gspca_dev *gspca_dev)
  108. {
  109. int i;
  110. int start_commands_size;
  111. u8 response = 0xff;
  112. struct sd *sd = (struct sd *) gspca_dev;
  113. struct jlj_command start_commands[] = {
  114. {{0x71, 0x81}, 0, 0},
  115. {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
  116. {{0x95, 0x70}, 1, 0},
  117. {{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
  118. {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
  119. {{0x95, 0x70}, 1, 0},
  120. {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
  121. {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
  122. {{0x95, 0x70}, 1, 0},
  123. #define SPORTSCAM_DV15_CMD_SIZE 9
  124. {{0x94, 0x02}, 0, 0},
  125. {{0xde, 0x24}, 0, 0},
  126. {{0x94, 0x02}, 0, 0},
  127. {{0xdd, 0xf0}, 0, 0},
  128. {{0x94, 0x02}, 0, 0},
  129. {{0xe3, 0x2c}, 0, 0},
  130. {{0x94, 0x02}, 0, 0},
  131. {{0xe4, 0x00}, 0, 0},
  132. {{0x94, 0x02}, 0, 0},
  133. {{0xe5, 0x00}, 0, 0},
  134. {{0x94, 0x02}, 0, 0},
  135. {{0xe6, 0x2c}, 0, 0},
  136. {{0x94, 0x03}, 0, 0},
  137. {{0xaa, 0x00}, 0, 0},
  138. {{0x71, 0x1e}, 0, 0},
  139. {{0x70, 0x06}, 0, 0},
  140. {{0x71, 0x80}, 0, 0},
  141. {{0x70, 0x07}, 0, 0}
  142. };
  143. sd->blocks_left = 0;
  144. /* Under Windows, USB spy shows that only the 9 first start
  145. * commands are used for SPORTSCAM_DV15 webcam
  146. */
  147. if (sd->type == SPORTSCAM_DV15)
  148. start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
  149. else
  150. start_commands_size = ARRAY_SIZE(start_commands);
  151. for (i = 0; i < start_commands_size; i++) {
  152. jlj_write2(gspca_dev, start_commands[i].instruction);
  153. if (start_commands[i].delay)
  154. msleep(start_commands[i].delay);
  155. if (start_commands[i].ack_wanted)
  156. jlj_read1(gspca_dev, response);
  157. }
  158. if (gspca_dev->usb_err < 0)
  159. PDEBUG(D_ERR, "Start streaming command failed");
  160. return gspca_dev->usb_err;
  161. }
  162. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  163. u8 *data, int len)
  164. {
  165. struct sd *sd = (struct sd *) gspca_dev;
  166. int packet_type;
  167. u32 header_marker;
  168. PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
  169. len, JEILINJ_MAX_TRANSFER);
  170. if (len != JEILINJ_MAX_TRANSFER) {
  171. PDEBUG(D_PACK, "bad length");
  172. goto discard;
  173. }
  174. /* check if it's start of frame */
  175. header_marker = ((u32 *)data)[0];
  176. if (header_marker == FRAME_START) {
  177. sd->blocks_left = data[0x0a] - 1;
  178. PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
  179. /* Start a new frame, and add the JPEG header, first thing */
  180. gspca_frame_add(gspca_dev, FIRST_PACKET,
  181. sd->jpeg_hdr, JPEG_HDR_SZ);
  182. /* Toss line 0 of data block 0, keep the rest. */
  183. gspca_frame_add(gspca_dev, INTER_PACKET,
  184. data + FRAME_HEADER_LEN,
  185. JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
  186. } else if (sd->blocks_left > 0) {
  187. PDEBUG(D_STREAM, "%d blocks remaining for frame",
  188. sd->blocks_left);
  189. sd->blocks_left -= 1;
  190. if (sd->blocks_left == 0)
  191. packet_type = LAST_PACKET;
  192. else
  193. packet_type = INTER_PACKET;
  194. gspca_frame_add(gspca_dev, packet_type,
  195. data, JEILINJ_MAX_TRANSFER);
  196. } else
  197. goto discard;
  198. return;
  199. discard:
  200. /* Discard data until a new frame starts. */
  201. gspca_dev->last_packet_type = DISCARD_PACKET;
  202. }
  203. /* This function is called at probe time just before sd_init */
  204. static int sd_config(struct gspca_dev *gspca_dev,
  205. const struct usb_device_id *id)
  206. {
  207. struct cam *cam = &gspca_dev->cam;
  208. struct sd *dev = (struct sd *) gspca_dev;
  209. dev->type = id->driver_info;
  210. dev->quality = 85;
  211. PDEBUG(D_PROBE,
  212. "JEILINJ camera detected"
  213. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  214. cam->cam_mode = jlj_mode;
  215. cam->nmodes = ARRAY_SIZE(jlj_mode);
  216. cam->bulk = 1;
  217. cam->bulk_nurbs = 1;
  218. cam->bulk_size = JEILINJ_MAX_TRANSFER;
  219. return 0;
  220. }
  221. static void sd_stopN(struct gspca_dev *gspca_dev)
  222. {
  223. int i;
  224. u8 *buf;
  225. u8 stop_commands[][2] = {
  226. {0x71, 0x00},
  227. {0x70, 0x09},
  228. {0x71, 0x80},
  229. {0x70, 0x05}
  230. };
  231. for (;;) {
  232. /* get the image remaining blocks */
  233. usb_bulk_msg(gspca_dev->dev,
  234. gspca_dev->urb[0]->pipe,
  235. gspca_dev->urb[0]->transfer_buffer,
  236. JEILINJ_MAX_TRANSFER, NULL,
  237. JEILINJ_DATA_TIMEOUT);
  238. /* search for 0xff 0xd9 (EOF for JPEG) */
  239. i = 0;
  240. buf = gspca_dev->urb[0]->transfer_buffer;
  241. while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
  242. ((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
  243. i++;
  244. if (i != (JEILINJ_MAX_TRANSFER - 1))
  245. /* last remaining block found */
  246. break;
  247. }
  248. for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
  249. jlj_write2(gspca_dev, stop_commands[i]);
  250. }
  251. /* this function is called at probe and resume time */
  252. static int sd_init(struct gspca_dev *gspca_dev)
  253. {
  254. return gspca_dev->usb_err;
  255. }
  256. /* Set up for getting frames. */
  257. static int sd_start(struct gspca_dev *gspca_dev)
  258. {
  259. struct sd *dev = (struct sd *) gspca_dev;
  260. /* create the JPEG header */
  261. jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  262. 0x21); /* JPEG 422 */
  263. jpeg_set_qual(dev->jpeg_hdr, dev->quality);
  264. PDEBUG(D_STREAM, "Start streaming at %dx%d",
  265. gspca_dev->height, gspca_dev->width);
  266. jlj_start(gspca_dev);
  267. return gspca_dev->usb_err;
  268. }
  269. /* Table of supported USB devices */
  270. static const struct usb_device_id device_table[] = {
  271. {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
  272. {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
  273. {}
  274. };
  275. MODULE_DEVICE_TABLE(usb, device_table);
  276. /* sub-driver description */
  277. static const struct sd_desc sd_desc_sakar_57379 = {
  278. .name = MODULE_NAME,
  279. .config = sd_config,
  280. .init = sd_init,
  281. .start = sd_start,
  282. .stopN = sd_stopN,
  283. .pkt_scan = sd_pkt_scan,
  284. };
  285. /* sub-driver description */
  286. static const struct sd_desc sd_desc_sportscam_dv15 = {
  287. .name = MODULE_NAME,
  288. .config = sd_config,
  289. .init = sd_init,
  290. .start = sd_start,
  291. .stopN = sd_stopN,
  292. .pkt_scan = sd_pkt_scan,
  293. };
  294. static const struct sd_desc *sd_desc[2] = {
  295. &sd_desc_sakar_57379,
  296. &sd_desc_sportscam_dv15
  297. };
  298. /* -- device connect -- */
  299. static int sd_probe(struct usb_interface *intf,
  300. const struct usb_device_id *id)
  301. {
  302. return gspca_dev_probe(intf, id,
  303. sd_desc[id->driver_info],
  304. sizeof(struct sd),
  305. THIS_MODULE);
  306. }
  307. static struct usb_driver sd_driver = {
  308. .name = MODULE_NAME,
  309. .id_table = device_table,
  310. .probe = sd_probe,
  311. .disconnect = gspca_disconnect,
  312. #ifdef CONFIG_PM
  313. .suspend = gspca_suspend,
  314. .resume = gspca_resume,
  315. #endif
  316. };
  317. /* -- module insert / remove -- */
  318. static int __init sd_mod_init(void)
  319. {
  320. return usb_register(&sd_driver);
  321. }
  322. static void __exit sd_mod_exit(void)
  323. {
  324. usb_deregister(&sd_driver);
  325. }
  326. module_init(sd_mod_init);
  327. module_exit(sd_mod_exit);