jeilinj.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. 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. 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 act_len;
  169. int packet_type;
  170. int ret;
  171. u8 *buffer;
  172. buffer = kmalloc(JEILINJ_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
  173. if (!buffer) {
  174. err("Couldn't allocate USB buffer");
  175. goto quit_stream;
  176. }
  177. while (gspca_dev->present && gspca_dev->streaming) {
  178. /*
  179. * Now request data block 0. Line 0 reports the size
  180. * to download, in blocks of size 0x200, and also tells the
  181. * "actual" data size, in bytes, which seems best to ignore.
  182. */
  183. ret = usb_bulk_msg(gspca_dev->dev,
  184. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  185. buffer, JEILINJ_MAX_TRANSFER, &act_len,
  186. JEILINJ_DATA_TIMEOUT);
  187. PDEBUG(D_STREAM,
  188. "Got %d bytes out of %d for Block 0",
  189. act_len, JEILINJ_MAX_TRANSFER);
  190. if (ret < 0 || act_len < FRAME_HEADER_LEN)
  191. goto quit_stream;
  192. blocks_left = buffer[0x0a] - 1;
  193. PDEBUG(D_STREAM, "blocks_left = 0x%x", blocks_left);
  194. /* Start a new frame, and add the JPEG header, first thing */
  195. gspca_frame_add(gspca_dev, FIRST_PACKET,
  196. dev->jpeg_hdr, JPEG_HDR_SZ);
  197. /* Toss line 0 of data block 0, keep the rest. */
  198. gspca_frame_add(gspca_dev, INTER_PACKET,
  199. buffer + FRAME_HEADER_LEN,
  200. JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
  201. while (blocks_left > 0) {
  202. if (!gspca_dev->present)
  203. goto quit_stream;
  204. ret = usb_bulk_msg(gspca_dev->dev,
  205. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  206. buffer, JEILINJ_MAX_TRANSFER, &act_len,
  207. JEILINJ_DATA_TIMEOUT);
  208. if (ret < 0 || act_len < JEILINJ_MAX_TRANSFER)
  209. goto quit_stream;
  210. PDEBUG(D_STREAM,
  211. "%d blocks remaining for frame", blocks_left);
  212. blocks_left -= 1;
  213. if (blocks_left == 0)
  214. packet_type = LAST_PACKET;
  215. else
  216. packet_type = INTER_PACKET;
  217. gspca_frame_add(gspca_dev, packet_type,
  218. buffer, JEILINJ_MAX_TRANSFER);
  219. }
  220. }
  221. quit_stream:
  222. mutex_lock(&gspca_dev->usb_lock);
  223. if (gspca_dev->present)
  224. jlj_stop(gspca_dev);
  225. mutex_unlock(&gspca_dev->usb_lock);
  226. kfree(buffer);
  227. }
  228. /* This function is called at probe time just before sd_init */
  229. static int sd_config(struct gspca_dev *gspca_dev,
  230. const struct usb_device_id *id)
  231. {
  232. struct cam *cam = &gspca_dev->cam;
  233. struct sd *dev = (struct sd *) gspca_dev;
  234. dev->quality = 85;
  235. dev->jpegqual = 85;
  236. PDEBUG(D_PROBE,
  237. "JEILINJ camera detected"
  238. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  239. cam->cam_mode = jlj_mode;
  240. cam->nmodes = 1;
  241. cam->bulk = 1;
  242. /* We don't use the buffer gspca allocates so make it small. */
  243. cam->bulk_size = 32;
  244. INIT_WORK(&dev->work_struct, jlj_dostream);
  245. return 0;
  246. }
  247. /* called on streamoff with alt==0 and on disconnect */
  248. /* the usb_lock is held at entry - restore on exit */
  249. static void sd_stop0(struct gspca_dev *gspca_dev)
  250. {
  251. struct sd *dev = (struct sd *) gspca_dev;
  252. /* wait for the work queue to terminate */
  253. mutex_unlock(&gspca_dev->usb_lock);
  254. /* This waits for jlj_dostream to finish */
  255. destroy_workqueue(dev->work_thread);
  256. dev->work_thread = NULL;
  257. mutex_lock(&gspca_dev->usb_lock);
  258. }
  259. /* this function is called at probe and resume time */
  260. static int sd_init(struct gspca_dev *gspca_dev)
  261. {
  262. return 0;
  263. }
  264. /* Set up for getting frames. */
  265. static int sd_start(struct gspca_dev *gspca_dev)
  266. {
  267. struct sd *dev = (struct sd *) gspca_dev;
  268. int ret;
  269. /* create the JPEG header */
  270. jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  271. 0x21); /* JPEG 422 */
  272. jpeg_set_qual(dev->jpeg_hdr, dev->quality);
  273. PDEBUG(D_STREAM, "Start streaming at 320x240");
  274. ret = jlj_start(gspca_dev);
  275. if (ret < 0) {
  276. PDEBUG(D_ERR, "Start streaming command failed");
  277. return ret;
  278. }
  279. /* Start the workqueue function to do the streaming */
  280. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  281. queue_work(dev->work_thread, &dev->work_struct);
  282. return 0;
  283. }
  284. /* Table of supported USB devices */
  285. static const struct usb_device_id device_table[] = {
  286. {USB_DEVICE(0x0979, 0x0280)},
  287. {}
  288. };
  289. MODULE_DEVICE_TABLE(usb, device_table);
  290. /* sub-driver description */
  291. static const struct sd_desc sd_desc = {
  292. .name = MODULE_NAME,
  293. .config = sd_config,
  294. .init = sd_init,
  295. .start = sd_start,
  296. .stop0 = sd_stop0,
  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,
  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);