jl2005bcd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Jeilin JL2005B/C/D library
  3. *
  4. * Copyright (C) 2011 Theodore Kilgore <kilgota@auburn.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define MODULE_NAME "jl2005bcd"
  21. #include <linux/workqueue.h>
  22. #include <linux/slab.h>
  23. #include "gspca.h"
  24. MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
  25. MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
  26. MODULE_LICENSE("GPL");
  27. /* Default timeouts, in ms */
  28. #define JL2005C_CMD_TIMEOUT 500
  29. #define JL2005C_DATA_TIMEOUT 1000
  30. /* Maximum transfer size to use. */
  31. #define JL2005C_MAX_TRANSFER 0x200
  32. #define FRAME_HEADER_LEN 16
  33. /* specific webcam descriptor */
  34. struct sd {
  35. struct gspca_dev gspca_dev; /* !! must be the first item */
  36. unsigned char firmware_id[6];
  37. const struct v4l2_pix_format *cap_mode;
  38. /* Driver stuff */
  39. struct work_struct work_struct;
  40. struct workqueue_struct *work_thread;
  41. u8 frame_brightness;
  42. int block_size; /* block size of camera */
  43. int vga; /* 1 if vga cam, 0 if cif cam */
  44. };
  45. /* Camera has two resolution settings. What they are depends on model. */
  46. static const struct v4l2_pix_format cif_mode[] = {
  47. {176, 144, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
  48. .bytesperline = 176,
  49. .sizeimage = 176 * 144,
  50. .colorspace = V4L2_COLORSPACE_SRGB,
  51. .priv = 0},
  52. {352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
  53. .bytesperline = 352,
  54. .sizeimage = 352 * 288,
  55. .colorspace = V4L2_COLORSPACE_SRGB,
  56. .priv = 0},
  57. };
  58. static const struct v4l2_pix_format vga_mode[] = {
  59. {320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
  60. .bytesperline = 320,
  61. .sizeimage = 320 * 240,
  62. .colorspace = V4L2_COLORSPACE_SRGB,
  63. .priv = 0},
  64. {640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
  65. .bytesperline = 640,
  66. .sizeimage = 640 * 480,
  67. .colorspace = V4L2_COLORSPACE_SRGB,
  68. .priv = 0},
  69. };
  70. /*
  71. * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
  72. * and 0x82 for bulk data transfer.
  73. */
  74. /* All commands are two bytes only */
  75. static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command)
  76. {
  77. int retval;
  78. memcpy(gspca_dev->usb_buf, command, 2);
  79. retval = usb_bulk_msg(gspca_dev->dev,
  80. usb_sndbulkpipe(gspca_dev->dev, 3),
  81. gspca_dev->usb_buf, 2, NULL, 500);
  82. if (retval < 0)
  83. pr_err("command write [%02x] error %d\n",
  84. gspca_dev->usb_buf[0], retval);
  85. return retval;
  86. }
  87. /* Response to a command is one byte in usb_buf[0], only if requested. */
  88. static int jl2005c_read1(struct gspca_dev *gspca_dev)
  89. {
  90. int retval;
  91. retval = usb_bulk_msg(gspca_dev->dev,
  92. usb_rcvbulkpipe(gspca_dev->dev, 0x84),
  93. gspca_dev->usb_buf, 1, NULL, 500);
  94. if (retval < 0)
  95. pr_err("read command [0x%02x] error %d\n",
  96. gspca_dev->usb_buf[0], retval);
  97. return retval;
  98. }
  99. /* Response appears in gspca_dev->usb_buf[0] */
  100. static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg)
  101. {
  102. int retval;
  103. static u8 instruction[2] = {0x95, 0x00};
  104. /* put register to read in byte 1 */
  105. instruction[1] = reg;
  106. /* Send the read request */
  107. retval = jl2005c_write2(gspca_dev, instruction);
  108. if (retval < 0)
  109. return retval;
  110. retval = jl2005c_read1(gspca_dev);
  111. return retval;
  112. }
  113. static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev)
  114. {
  115. int i;
  116. int retval;
  117. int frame_brightness = 0;
  118. static u8 instruction[2] = {0x7f, 0x01};
  119. retval = jl2005c_write2(gspca_dev, instruction);
  120. if (retval < 0)
  121. return retval;
  122. i = 0;
  123. while (i < 20 && !frame_brightness) {
  124. /* If we tried 20 times, give up. */
  125. retval = jl2005c_read_reg(gspca_dev, 0x7e);
  126. if (retval < 0)
  127. return retval;
  128. frame_brightness = gspca_dev->usb_buf[0];
  129. retval = jl2005c_read_reg(gspca_dev, 0x7d);
  130. if (retval < 0)
  131. return retval;
  132. i++;
  133. }
  134. PDEBUG(D_FRAM, "frame_brightness is 0x%02x", gspca_dev->usb_buf[0]);
  135. return retval;
  136. }
  137. static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
  138. unsigned char value)
  139. {
  140. int retval;
  141. u8 instruction[2];
  142. instruction[0] = reg;
  143. instruction[1] = value;
  144. retval = jl2005c_write2(gspca_dev, instruction);
  145. if (retval < 0)
  146. return retval;
  147. return retval;
  148. }
  149. static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
  150. {
  151. struct sd *sd = (struct sd *)gspca_dev;
  152. int i = 0;
  153. int retval = -1;
  154. unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
  155. PDEBUG(D_PROBE, "Running jl2005c_get_firmware_id");
  156. /* Read the first ID byte once for warmup */
  157. retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
  158. PDEBUG(D_PROBE, "response is %02x", gspca_dev->usb_buf[0]);
  159. if (retval < 0)
  160. return retval;
  161. /* Now actually get the ID string */
  162. for (i = 0; i < 6; i++) {
  163. retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
  164. if (retval < 0)
  165. return retval;
  166. sd->firmware_id[i] = gspca_dev->usb_buf[0];
  167. }
  168. PDEBUG(D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x",
  169. sd->firmware_id[0],
  170. sd->firmware_id[1],
  171. sd->firmware_id[2],
  172. sd->firmware_id[3],
  173. sd->firmware_id[4],
  174. sd->firmware_id[5]);
  175. return 0;
  176. }
  177. static int jl2005c_stream_start_vga_lg
  178. (struct gspca_dev *gspca_dev)
  179. {
  180. int i;
  181. int retval = -1;
  182. static u8 instruction[][2] = {
  183. {0x05, 0x00},
  184. {0x7c, 0x00},
  185. {0x7d, 0x18},
  186. {0x02, 0x00},
  187. {0x01, 0x00},
  188. {0x04, 0x52},
  189. };
  190. for (i = 0; i < ARRAY_SIZE(instruction); i++) {
  191. msleep(60);
  192. retval = jl2005c_write2(gspca_dev, instruction[i]);
  193. if (retval < 0)
  194. return retval;
  195. }
  196. msleep(60);
  197. return retval;
  198. }
  199. static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
  200. {
  201. int i;
  202. int retval = -1;
  203. static u8 instruction[][2] = {
  204. {0x06, 0x00},
  205. {0x7c, 0x00},
  206. {0x7d, 0x1a},
  207. {0x02, 0x00},
  208. {0x01, 0x00},
  209. {0x04, 0x52},
  210. };
  211. for (i = 0; i < ARRAY_SIZE(instruction); i++) {
  212. msleep(60);
  213. retval = jl2005c_write2(gspca_dev, instruction[i]);
  214. if (retval < 0)
  215. return retval;
  216. }
  217. msleep(60);
  218. return retval;
  219. }
  220. static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
  221. {
  222. int i;
  223. int retval = -1;
  224. static u8 instruction[][2] = {
  225. {0x05, 0x00},
  226. {0x7c, 0x00},
  227. {0x7d, 0x30},
  228. {0x02, 0x00},
  229. {0x01, 0x00},
  230. {0x04, 0x42},
  231. };
  232. for (i = 0; i < ARRAY_SIZE(instruction); i++) {
  233. msleep(60);
  234. retval = jl2005c_write2(gspca_dev, instruction[i]);
  235. if (retval < 0)
  236. return retval;
  237. }
  238. msleep(60);
  239. return retval;
  240. }
  241. static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
  242. {
  243. int i;
  244. int retval = -1;
  245. static u8 instruction[][2] = {
  246. {0x06, 0x00},
  247. {0x7c, 0x00},
  248. {0x7d, 0x32},
  249. {0x02, 0x00},
  250. {0x01, 0x00},
  251. {0x04, 0x42},
  252. };
  253. for (i = 0; i < ARRAY_SIZE(instruction); i++) {
  254. msleep(60);
  255. retval = jl2005c_write2(gspca_dev, instruction[i]);
  256. if (retval < 0)
  257. return retval;
  258. }
  259. msleep(60);
  260. return retval;
  261. }
  262. static int jl2005c_stop(struct gspca_dev *gspca_dev)
  263. {
  264. int retval;
  265. retval = jl2005c_write_reg(gspca_dev, 0x07, 0x00);
  266. return retval;
  267. }
  268. /*
  269. * This function is called as a workqueue function and runs whenever the camera
  270. * is streaming data. Because it is a workqueue function it is allowed to sleep
  271. * so we can use synchronous USB calls. To avoid possible collisions with other
  272. * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
  273. * performing USB operations using it. In practice we don't really need this
  274. * as the camera doesn't provide any controls.
  275. */
  276. static void jl2005c_dostream(struct work_struct *work)
  277. {
  278. struct sd *dev = container_of(work, struct sd, work_struct);
  279. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  280. int bytes_left = 0; /* bytes remaining in current frame. */
  281. int data_len; /* size to use for the next read. */
  282. int header_read = 0;
  283. unsigned char header_sig[2] = {0x4a, 0x4c};
  284. int act_len;
  285. int packet_type;
  286. int ret;
  287. u8 *buffer;
  288. buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
  289. if (!buffer) {
  290. pr_err("Couldn't allocate USB buffer\n");
  291. goto quit_stream;
  292. }
  293. while (gspca_dev->present && gspca_dev->streaming) {
  294. #ifdef CONFIG_PM
  295. if (gspca_dev->frozen)
  296. break;
  297. #endif
  298. /* Check if this is a new frame. If so, start the frame first */
  299. if (!header_read) {
  300. mutex_lock(&gspca_dev->usb_lock);
  301. ret = jl2005c_start_new_frame(gspca_dev);
  302. mutex_unlock(&gspca_dev->usb_lock);
  303. if (ret < 0)
  304. goto quit_stream;
  305. ret = usb_bulk_msg(gspca_dev->dev,
  306. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  307. buffer, JL2005C_MAX_TRANSFER, &act_len,
  308. JL2005C_DATA_TIMEOUT);
  309. PDEBUG(D_PACK,
  310. "Got %d bytes out of %d for header",
  311. act_len, JL2005C_MAX_TRANSFER);
  312. if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
  313. goto quit_stream;
  314. /* Check whether we actually got the first blodk */
  315. if (memcmp(header_sig, buffer, 2) != 0) {
  316. pr_err("First block is not the first block\n");
  317. goto quit_stream;
  318. }
  319. /* total size to fetch is byte 7, times blocksize
  320. * of which we already got act_len */
  321. bytes_left = buffer[0x07] * dev->block_size - act_len;
  322. PDEBUG(D_PACK, "bytes_left = 0x%x", bytes_left);
  323. /* We keep the header. It has other information, too.*/
  324. packet_type = FIRST_PACKET;
  325. gspca_frame_add(gspca_dev, packet_type,
  326. buffer, act_len);
  327. header_read = 1;
  328. }
  329. while (bytes_left > 0 && gspca_dev->present) {
  330. data_len = bytes_left > JL2005C_MAX_TRANSFER ?
  331. JL2005C_MAX_TRANSFER : bytes_left;
  332. ret = usb_bulk_msg(gspca_dev->dev,
  333. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  334. buffer, data_len, &act_len,
  335. JL2005C_DATA_TIMEOUT);
  336. if (ret < 0 || act_len < data_len)
  337. goto quit_stream;
  338. PDEBUG(D_PACK,
  339. "Got %d bytes out of %d for frame",
  340. data_len, bytes_left);
  341. bytes_left -= data_len;
  342. if (bytes_left == 0) {
  343. packet_type = LAST_PACKET;
  344. header_read = 0;
  345. } else
  346. packet_type = INTER_PACKET;
  347. gspca_frame_add(gspca_dev, packet_type,
  348. buffer, data_len);
  349. }
  350. }
  351. quit_stream:
  352. if (gspca_dev->present) {
  353. mutex_lock(&gspca_dev->usb_lock);
  354. jl2005c_stop(gspca_dev);
  355. mutex_unlock(&gspca_dev->usb_lock);
  356. }
  357. kfree(buffer);
  358. }
  359. /* This function is called at probe time */
  360. static int sd_config(struct gspca_dev *gspca_dev,
  361. const struct usb_device_id *id)
  362. {
  363. struct cam *cam;
  364. struct sd *sd = (struct sd *) gspca_dev;
  365. cam = &gspca_dev->cam;
  366. /* We don't use the buffer gspca allocates so make it small. */
  367. cam->bulk_size = 64;
  368. cam->bulk = 1;
  369. /* For the rest, the camera needs to be detected */
  370. jl2005c_get_firmware_id(gspca_dev);
  371. /* Here are some known firmware IDs
  372. * First some JL2005B cameras
  373. * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
  374. * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
  375. * JL2005C cameras
  376. * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
  377. * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
  378. * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
  379. *
  380. * Based upon this scanty evidence, we can detect a CIF camera by
  381. * testing byte 0 for 0x4x.
  382. */
  383. if ((sd->firmware_id[0] & 0xf0) == 0x40) {
  384. cam->cam_mode = cif_mode;
  385. cam->nmodes = ARRAY_SIZE(cif_mode);
  386. sd->block_size = 0x80;
  387. } else {
  388. cam->cam_mode = vga_mode;
  389. cam->nmodes = ARRAY_SIZE(vga_mode);
  390. sd->block_size = 0x200;
  391. }
  392. INIT_WORK(&sd->work_struct, jl2005c_dostream);
  393. return 0;
  394. }
  395. /* this function is called at probe and resume time */
  396. static int sd_init(struct gspca_dev *gspca_dev)
  397. {
  398. return 0;
  399. }
  400. static int sd_start(struct gspca_dev *gspca_dev)
  401. {
  402. struct sd *sd = (struct sd *) gspca_dev;
  403. sd->cap_mode = gspca_dev->cam.cam_mode;
  404. switch (gspca_dev->pixfmt.width) {
  405. case 640:
  406. PDEBUG(D_STREAM, "Start streaming at vga resolution");
  407. jl2005c_stream_start_vga_lg(gspca_dev);
  408. break;
  409. case 320:
  410. PDEBUG(D_STREAM, "Start streaming at qvga resolution");
  411. jl2005c_stream_start_vga_small(gspca_dev);
  412. break;
  413. case 352:
  414. PDEBUG(D_STREAM, "Start streaming at cif resolution");
  415. jl2005c_stream_start_cif_lg(gspca_dev);
  416. break;
  417. case 176:
  418. PDEBUG(D_STREAM, "Start streaming at qcif resolution");
  419. jl2005c_stream_start_cif_small(gspca_dev);
  420. break;
  421. default:
  422. pr_err("Unknown resolution specified\n");
  423. return -1;
  424. }
  425. /* Start the workqueue function to do the streaming */
  426. sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
  427. queue_work(sd->work_thread, &sd->work_struct);
  428. return 0;
  429. }
  430. /* called on streamoff with alt==0 and on disconnect */
  431. /* the usb_lock is held at entry - restore on exit */
  432. static void sd_stop0(struct gspca_dev *gspca_dev)
  433. {
  434. struct sd *dev = (struct sd *) gspca_dev;
  435. /* wait for the work queue to terminate */
  436. mutex_unlock(&gspca_dev->usb_lock);
  437. /* This waits for sq905c_dostream to finish */
  438. destroy_workqueue(dev->work_thread);
  439. dev->work_thread = NULL;
  440. mutex_lock(&gspca_dev->usb_lock);
  441. }
  442. /* sub-driver description */
  443. static const struct sd_desc sd_desc = {
  444. .name = MODULE_NAME,
  445. .config = sd_config,
  446. .init = sd_init,
  447. .start = sd_start,
  448. .stop0 = sd_stop0,
  449. };
  450. /* -- module initialisation -- */
  451. static const struct usb_device_id device_table[] = {
  452. {USB_DEVICE(0x0979, 0x0227)},
  453. {}
  454. };
  455. MODULE_DEVICE_TABLE(usb, device_table);
  456. /* -- device connect -- */
  457. static int sd_probe(struct usb_interface *intf,
  458. const struct usb_device_id *id)
  459. {
  460. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  461. THIS_MODULE);
  462. }
  463. static struct usb_driver sd_driver = {
  464. .name = MODULE_NAME,
  465. .id_table = device_table,
  466. .probe = sd_probe,
  467. .disconnect = gspca_disconnect,
  468. #ifdef CONFIG_PM
  469. .suspend = gspca_suspend,
  470. .resume = gspca_resume,
  471. .reset_resume = gspca_resume,
  472. #endif
  473. };
  474. module_usb_driver(sd_driver);