jeilinj.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. int blocks_left; /* 0x200-sized blocks remaining in current frame. */
  167. int size_in_blocks;
  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. PDEBUG(D_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. size_in_blocks = buffer[0x0a];
  193. blocks_left = buffer[0x0a] - 1;
  194. PDEBUG(D_STREAM, "blocks_left = 0x%x", blocks_left);
  195. /* Start a new frame, and add the JPEG header, first thing */
  196. gspca_frame_add(gspca_dev, FIRST_PACKET,
  197. dev->jpeg_hdr, JPEG_HDR_SZ);
  198. /* Toss line 0 of data block 0, keep the rest. */
  199. gspca_frame_add(gspca_dev, INTER_PACKET,
  200. buffer + FRAME_HEADER_LEN,
  201. JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
  202. while (blocks_left > 0) {
  203. if (!gspca_dev->present)
  204. goto quit_stream;
  205. ret = usb_bulk_msg(gspca_dev->dev,
  206. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  207. buffer, JEILINJ_MAX_TRANSFER, &act_len,
  208. JEILINJ_DATA_TIMEOUT);
  209. if (ret < 0 || act_len < JEILINJ_MAX_TRANSFER)
  210. goto quit_stream;
  211. PDEBUG(D_STREAM,
  212. "%d blocks remaining for frame", blocks_left);
  213. blocks_left -= 1;
  214. if (blocks_left == 0)
  215. packet_type = LAST_PACKET;
  216. else
  217. packet_type = INTER_PACKET;
  218. gspca_frame_add(gspca_dev, packet_type,
  219. buffer, JEILINJ_MAX_TRANSFER);
  220. }
  221. }
  222. quit_stream:
  223. mutex_lock(&gspca_dev->usb_lock);
  224. if (gspca_dev->present)
  225. jlj_stop(gspca_dev);
  226. mutex_unlock(&gspca_dev->usb_lock);
  227. kfree(buffer);
  228. }
  229. /* This function is called at probe time just before sd_init */
  230. static int sd_config(struct gspca_dev *gspca_dev,
  231. const struct usb_device_id *id)
  232. {
  233. struct cam *cam = &gspca_dev->cam;
  234. struct sd *dev = (struct sd *) gspca_dev;
  235. dev->quality = 85;
  236. dev->jpegqual = 85;
  237. PDEBUG(D_PROBE,
  238. "JEILINJ camera detected"
  239. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  240. cam->cam_mode = jlj_mode;
  241. cam->nmodes = 1;
  242. cam->bulk = 1;
  243. /* We don't use the buffer gspca allocates so make it small. */
  244. cam->bulk_size = 32;
  245. INIT_WORK(&dev->work_struct, jlj_dostream);
  246. return 0;
  247. }
  248. /* called on streamoff with alt==0 and on disconnect */
  249. /* the usb_lock is held at entry - restore on exit */
  250. static void sd_stop0(struct gspca_dev *gspca_dev)
  251. {
  252. struct sd *dev = (struct sd *) gspca_dev;
  253. /* wait for the work queue to terminate */
  254. mutex_unlock(&gspca_dev->usb_lock);
  255. /* This waits for jlj_dostream to finish */
  256. destroy_workqueue(dev->work_thread);
  257. dev->work_thread = NULL;
  258. mutex_lock(&gspca_dev->usb_lock);
  259. kfree(dev->jpeg_hdr);
  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. dev->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
  273. if (dev->jpeg_hdr == NULL)
  274. return -ENOMEM;
  275. jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  276. 0x21); /* JPEG 422 */
  277. jpeg_set_qual(dev->jpeg_hdr, dev->quality);
  278. PDEBUG(D_STREAM, "Start streaming at 320x240");
  279. ret = jlj_start(gspca_dev);
  280. if (ret < 0) {
  281. PDEBUG(D_ERR, "Start streaming command failed");
  282. return ret;
  283. }
  284. /* Start the workqueue function to do the streaming */
  285. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  286. queue_work(dev->work_thread, &dev->work_struct);
  287. return 0;
  288. }
  289. /* Table of supported USB devices */
  290. static const __devinitdata struct usb_device_id device_table[] = {
  291. {USB_DEVICE(0x0979, 0x0280)},
  292. {}
  293. };
  294. MODULE_DEVICE_TABLE(usb, device_table);
  295. /* sub-driver description */
  296. static const struct sd_desc sd_desc = {
  297. .name = MODULE_NAME,
  298. .config = sd_config,
  299. .init = sd_init,
  300. .start = sd_start,
  301. .stop0 = sd_stop0,
  302. };
  303. /* -- device connect -- */
  304. static int sd_probe(struct usb_interface *intf,
  305. const struct usb_device_id *id)
  306. {
  307. return gspca_dev_probe(intf, id,
  308. &sd_desc,
  309. sizeof(struct sd),
  310. THIS_MODULE);
  311. }
  312. static struct usb_driver sd_driver = {
  313. .name = MODULE_NAME,
  314. .id_table = device_table,
  315. .probe = sd_probe,
  316. .disconnect = gspca_disconnect,
  317. #ifdef CONFIG_PM
  318. .suspend = gspca_suspend,
  319. .resume = gspca_resume,
  320. #endif
  321. };
  322. /* -- module insert / remove -- */
  323. static int __init sd_mod_init(void)
  324. {
  325. int ret;
  326. ret = usb_register(&sd_driver);
  327. if (ret < 0)
  328. return ret;
  329. PDEBUG(D_PROBE, "registered");
  330. return 0;
  331. }
  332. static void __exit sd_mod_exit(void)
  333. {
  334. usb_deregister(&sd_driver);
  335. PDEBUG(D_PROBE, "deregistered");
  336. }
  337. module_init(sd_mod_init);
  338. module_exit(sd_mod_exit);