jeilinj.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #define MODULE_NAME "jeilinj"
  24. #include <linux/workqueue.h>
  25. #include "gspca.h"
  26. #include "jpeg.h"
  27. MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
  28. MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
  29. MODULE_LICENSE("GPL");
  30. /* Default timeouts, in ms */
  31. #define JEILINJ_CMD_TIMEOUT 500
  32. #define JEILINJ_DATA_TIMEOUT 1000
  33. /* Maximum transfer size to use. */
  34. #define JEILINJ_MAX_TRANSFER 0x200
  35. #define FRAME_HEADER_LEN 0x10
  36. /* Structure to hold all of our device specific stuff */
  37. struct sd {
  38. struct gspca_dev gspca_dev; /* !! must be the first item */
  39. const struct v4l2_pix_format *cap_mode;
  40. /* Driver stuff */
  41. struct work_struct work_struct;
  42. struct workqueue_struct *work_thread;
  43. u8 quality; /* image quality */
  44. u8 jpegqual; /* webcam quality */
  45. u8 *jpeg_hdr;
  46. };
  47. struct jlj_command {
  48. unsigned char instruction[2];
  49. unsigned char ack_wanted;
  50. };
  51. /* AFAICT these cameras will only do 320x240. */
  52. static struct v4l2_pix_format jlj_mode[] = {
  53. { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  54. .bytesperline = 320,
  55. .sizeimage = 320 * 240,
  56. .colorspace = V4L2_COLORSPACE_JPEG,
  57. .priv = 0}
  58. };
  59. /*
  60. * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
  61. * and 0x82 for bulk transfer.
  62. */
  63. /* All commands are two bytes only */
  64. static int jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
  65. {
  66. int retval;
  67. memcpy(gspca_dev->usb_buf, command, 2);
  68. retval = usb_bulk_msg(gspca_dev->dev,
  69. usb_sndbulkpipe(gspca_dev->dev, 3),
  70. gspca_dev->usb_buf, 2, NULL, 500);
  71. if (retval < 0)
  72. PDEBUG(D_ERR, "command write [%02x] error %d",
  73. gspca_dev->usb_buf[0], retval);
  74. return retval;
  75. }
  76. /* Responses are one byte only */
  77. static int jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
  78. {
  79. int retval;
  80. retval = usb_bulk_msg(gspca_dev->dev,
  81. usb_rcvbulkpipe(gspca_dev->dev, 0x84),
  82. gspca_dev->usb_buf, 1, NULL, 500);
  83. response = gspca_dev->usb_buf[0];
  84. if (retval < 0)
  85. PDEBUG(D_ERR, "read command [%02x] error %d",
  86. gspca_dev->usb_buf[0], retval);
  87. return retval;
  88. }
  89. static int jlj_start(struct gspca_dev *gspca_dev)
  90. {
  91. int i;
  92. int retval = -1;
  93. u8 response = 0xff;
  94. struct jlj_command start_commands[] = {
  95. {{0x71, 0x81}, 0},
  96. {{0x70, 0x05}, 0},
  97. {{0x95, 0x70}, 1},
  98. {{0x71, 0x81}, 0},
  99. {{0x70, 0x04}, 0},
  100. {{0x95, 0x70}, 1},
  101. {{0x71, 0x00}, 0},
  102. {{0x70, 0x08}, 0},
  103. {{0x95, 0x70}, 1},
  104. {{0x94, 0x02}, 0},
  105. {{0xde, 0x24}, 0},
  106. {{0x94, 0x02}, 0},
  107. {{0xdd, 0xf0}, 0},
  108. {{0x94, 0x02}, 0},
  109. {{0xe3, 0x2c}, 0},
  110. {{0x94, 0x02}, 0},
  111. {{0xe4, 0x00}, 0},
  112. {{0x94, 0x02}, 0},
  113. {{0xe5, 0x00}, 0},
  114. {{0x94, 0x02}, 0},
  115. {{0xe6, 0x2c}, 0},
  116. {{0x94, 0x03}, 0},
  117. {{0xaa, 0x00}, 0},
  118. {{0x71, 0x1e}, 0},
  119. {{0x70, 0x06}, 0},
  120. {{0x71, 0x80}, 0},
  121. {{0x70, 0x07}, 0}
  122. };
  123. for (i = 0; i < ARRAY_SIZE(start_commands); i++) {
  124. retval = jlj_write2(gspca_dev, start_commands[i].instruction);
  125. if (retval < 0)
  126. return retval;
  127. if (start_commands[i].ack_wanted)
  128. retval = jlj_read1(gspca_dev, response);
  129. if (retval < 0)
  130. return retval;
  131. }
  132. PDEBUG(D_ERR, "jlj_start retval is %d", retval);
  133. return retval;
  134. }
  135. static int jlj_stop(struct gspca_dev *gspca_dev)
  136. {
  137. int i;
  138. int retval;
  139. struct jlj_command stop_commands[] = {
  140. {{0x71, 0x00}, 0},
  141. {{0x70, 0x09}, 0},
  142. {{0x71, 0x80}, 0},
  143. {{0x70, 0x05}, 0}
  144. };
  145. for (i = 0; i < ARRAY_SIZE(stop_commands); i++) {
  146. retval = jlj_write2(gspca_dev, stop_commands[i].instruction);
  147. if (retval < 0)
  148. return retval;
  149. }
  150. return retval;
  151. }
  152. /* This function is called as a workqueue function and runs whenever the camera
  153. * is streaming data. Because it is a workqueue function it is allowed to sleep
  154. * so we can use synchronous USB calls. To avoid possible collisions with other
  155. * threads attempting to use the camera's USB interface the gspca usb_lock is
  156. * used when performing the one USB control operation inside the workqueue,
  157. * which tells the camera to close the stream. In practice the only thing
  158. * which needs to be protected against is the usb_set_interface call that
  159. * gspca makes during stream_off. Otherwise the camera doesn't provide any
  160. * controls that the user could try to change.
  161. */
  162. static void jlj_dostream(struct work_struct *work)
  163. {
  164. struct sd *dev = container_of(work, struct sd, work_struct);
  165. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  166. struct gspca_frame *frame;
  167. int blocks_left; /* 0x200-sized blocks remaining in current frame. */
  168. int size_in_blocks;
  169. int act_len;
  170. int discarding = 0; /* true if we failed to get space for frame. */
  171. int packet_type;
  172. int ret;
  173. u8 *buffer;
  174. buffer = kmalloc(JEILINJ_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
  175. if (!buffer) {
  176. PDEBUG(D_ERR, "Couldn't allocate USB buffer");
  177. goto quit_stream;
  178. }
  179. while (gspca_dev->present && gspca_dev->streaming) {
  180. if (!gspca_dev->present)
  181. goto quit_stream;
  182. /* Start a new frame, and add the JPEG header, first thing */
  183. frame = gspca_get_i_frame(gspca_dev);
  184. if (frame && !discarding)
  185. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  186. dev->jpeg_hdr, JPEG_HDR_SZ);
  187. else
  188. discarding = 1;
  189. /*
  190. * Now request data block 0. Line 0 reports the size
  191. * to download, in blocks of size 0x200, and also tells the
  192. * "actual" data size, in bytes, which seems best to ignore.
  193. */
  194. ret = usb_bulk_msg(gspca_dev->dev,
  195. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  196. buffer, JEILINJ_MAX_TRANSFER, &act_len,
  197. JEILINJ_DATA_TIMEOUT);
  198. PDEBUG(D_STREAM,
  199. "Got %d bytes out of %d for Block 0",
  200. act_len, JEILINJ_MAX_TRANSFER);
  201. if (ret < 0 || act_len < FRAME_HEADER_LEN)
  202. goto quit_stream;
  203. size_in_blocks = buffer[0x0a];
  204. blocks_left = buffer[0x0a] - 1;
  205. PDEBUG(D_STREAM, "blocks_left = 0x%x", blocks_left);
  206. packet_type = INTER_PACKET;
  207. if (frame && !discarding)
  208. /* Toss line 0 of data block 0, keep the rest. */
  209. gspca_frame_add(gspca_dev, packet_type,
  210. frame, buffer + FRAME_HEADER_LEN,
  211. JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
  212. else
  213. discarding = 1;
  214. while (blocks_left > 0) {
  215. if (!gspca_dev->present)
  216. goto quit_stream;
  217. ret = usb_bulk_msg(gspca_dev->dev,
  218. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  219. buffer, JEILINJ_MAX_TRANSFER, &act_len,
  220. JEILINJ_DATA_TIMEOUT);
  221. if (ret < 0 || act_len < JEILINJ_MAX_TRANSFER)
  222. goto quit_stream;
  223. PDEBUG(D_STREAM,
  224. "%d blocks remaining for frame", blocks_left);
  225. blocks_left -= 1;
  226. if (blocks_left == 0)
  227. packet_type = LAST_PACKET;
  228. else
  229. packet_type = INTER_PACKET;
  230. if (frame && !discarding)
  231. gspca_frame_add(gspca_dev, packet_type,
  232. frame, buffer,
  233. JEILINJ_MAX_TRANSFER);
  234. else
  235. discarding = 1;
  236. }
  237. }
  238. quit_stream:
  239. mutex_lock(&gspca_dev->usb_lock);
  240. if (gspca_dev->present)
  241. jlj_stop(gspca_dev);
  242. mutex_unlock(&gspca_dev->usb_lock);
  243. kfree(buffer);
  244. }
  245. /* This function is called at probe time just before sd_init */
  246. static int sd_config(struct gspca_dev *gspca_dev,
  247. const struct usb_device_id *id)
  248. {
  249. struct cam *cam = &gspca_dev->cam;
  250. struct sd *dev = (struct sd *) gspca_dev;
  251. dev->quality = 85;
  252. dev->jpegqual = 85;
  253. PDEBUG(D_PROBE,
  254. "JEILINJ camera detected"
  255. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  256. cam->cam_mode = jlj_mode;
  257. cam->nmodes = 1;
  258. cam->bulk = 1;
  259. /* We don't use the buffer gspca allocates so make it small. */
  260. cam->bulk_size = 32;
  261. INIT_WORK(&dev->work_struct, jlj_dostream);
  262. return 0;
  263. }
  264. /* called on streamoff with alt==0 and on disconnect */
  265. /* the usb_lock is held at entry - restore on exit */
  266. static void sd_stop0(struct gspca_dev *gspca_dev)
  267. {
  268. struct sd *dev = (struct sd *) gspca_dev;
  269. /* wait for the work queue to terminate */
  270. mutex_unlock(&gspca_dev->usb_lock);
  271. /* This waits for jlj_dostream to finish */
  272. destroy_workqueue(dev->work_thread);
  273. dev->work_thread = NULL;
  274. mutex_lock(&gspca_dev->usb_lock);
  275. kfree(dev->jpeg_hdr);
  276. }
  277. /* this function is called at probe and resume time */
  278. static int sd_init(struct gspca_dev *gspca_dev)
  279. {
  280. return 0;
  281. }
  282. /* Set up for getting frames. */
  283. static int sd_start(struct gspca_dev *gspca_dev)
  284. {
  285. struct sd *dev = (struct sd *) gspca_dev;
  286. int ret;
  287. /* create the JPEG header */
  288. dev->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
  289. if (dev->jpeg_hdr == NULL)
  290. return -ENOMEM;
  291. jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  292. 0x21); /* JPEG 422 */
  293. jpeg_set_qual(dev->jpeg_hdr, dev->quality);
  294. PDEBUG(D_STREAM, "Start streaming at 320x240");
  295. ret = jlj_start(gspca_dev);
  296. if (ret < 0) {
  297. PDEBUG(D_ERR, "Start streaming command failed");
  298. return ret;
  299. }
  300. /* Start the workqueue function to do the streaming */
  301. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  302. queue_work(dev->work_thread, &dev->work_struct);
  303. return 0;
  304. }
  305. /* Table of supported USB devices */
  306. static const __devinitdata struct usb_device_id device_table[] = {
  307. {USB_DEVICE(0x0979, 0x0280)},
  308. {}
  309. };
  310. MODULE_DEVICE_TABLE(usb, device_table);
  311. /* sub-driver description */
  312. static const struct sd_desc sd_desc = {
  313. .name = MODULE_NAME,
  314. .config = sd_config,
  315. .init = sd_init,
  316. .start = sd_start,
  317. .stop0 = sd_stop0,
  318. };
  319. /* -- device connect -- */
  320. static int sd_probe(struct usb_interface *intf,
  321. const struct usb_device_id *id)
  322. {
  323. return gspca_dev_probe(intf, id,
  324. &sd_desc,
  325. sizeof(struct sd),
  326. THIS_MODULE);
  327. }
  328. static struct usb_driver sd_driver = {
  329. .name = MODULE_NAME,
  330. .id_table = device_table,
  331. .probe = sd_probe,
  332. .disconnect = gspca_disconnect,
  333. #ifdef CONFIG_PM
  334. .suspend = gspca_suspend,
  335. .resume = gspca_resume,
  336. #endif
  337. };
  338. /* -- module insert / remove -- */
  339. static int __init sd_mod_init(void)
  340. {
  341. int ret;
  342. ret = usb_register(&sd_driver);
  343. if (ret < 0)
  344. return ret;
  345. PDEBUG(D_PROBE, "registered");
  346. return 0;
  347. }
  348. static void __exit sd_mod_exit(void)
  349. {
  350. usb_deregister(&sd_driver);
  351. PDEBUG(D_PROBE, "deregistered");
  352. }
  353. module_init(sd_mod_init);
  354. module_exit(sd_mod_exit);