jl2005bcd.c 13 KB

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