jeilinj.c 9.9 KB

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