jl2005bcd.c 13 KB

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