jl2005bcd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. int retval;
  264. retval = jl2005c_write_reg(gspca_dev, 0x07, 0x00);
  265. return retval;
  266. }
  267. /*
  268. * This function is called as a workqueue function and runs whenever the camera
  269. * is streaming data. Because it is a workqueue function it is allowed to sleep
  270. * so we can use synchronous USB calls. To avoid possible collisions with other
  271. * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
  272. * performing USB operations using it. In practice we don't really need this
  273. * as the camera doesn't provide any controls.
  274. */
  275. static void jl2005c_dostream(struct work_struct *work)
  276. {
  277. struct sd *dev = container_of(work, struct sd, work_struct);
  278. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  279. int bytes_left = 0; /* bytes remaining in current frame. */
  280. int data_len; /* size to use for the next read. */
  281. int header_read = 0;
  282. unsigned char header_sig[2] = {0x4a, 0x4c};
  283. int act_len;
  284. int packet_type;
  285. int ret;
  286. u8 *buffer;
  287. buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
  288. if (!buffer) {
  289. pr_err("Couldn't allocate USB buffer\n");
  290. goto quit_stream;
  291. }
  292. while (gspca_dev->present && gspca_dev->streaming) {
  293. #ifdef CONFIG_PM
  294. if (gspca_dev->frozen)
  295. break;
  296. #endif
  297. /* Check if this is a new frame. If so, start the frame first */
  298. if (!header_read) {
  299. mutex_lock(&gspca_dev->usb_lock);
  300. ret = jl2005c_start_new_frame(gspca_dev);
  301. mutex_unlock(&gspca_dev->usb_lock);
  302. if (ret < 0)
  303. goto quit_stream;
  304. ret = usb_bulk_msg(gspca_dev->dev,
  305. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  306. buffer, JL2005C_MAX_TRANSFER, &act_len,
  307. JL2005C_DATA_TIMEOUT);
  308. PDEBUG(D_PACK,
  309. "Got %d bytes out of %d for header",
  310. act_len, JL2005C_MAX_TRANSFER);
  311. if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
  312. goto quit_stream;
  313. /* Check whether we actually got the first blodk */
  314. if (memcmp(header_sig, buffer, 2) != 0) {
  315. pr_err("First block is not the first block\n");
  316. goto quit_stream;
  317. }
  318. /* total size to fetch is byte 7, times blocksize
  319. * of which we already got act_len */
  320. bytes_left = buffer[0x07] * dev->block_size - act_len;
  321. PDEBUG(D_PACK, "bytes_left = 0x%x", bytes_left);
  322. /* We keep the header. It has other information, too.*/
  323. packet_type = FIRST_PACKET;
  324. gspca_frame_add(gspca_dev, packet_type,
  325. buffer, act_len);
  326. header_read = 1;
  327. }
  328. while (bytes_left > 0 && gspca_dev->present) {
  329. data_len = bytes_left > JL2005C_MAX_TRANSFER ?
  330. JL2005C_MAX_TRANSFER : bytes_left;
  331. ret = usb_bulk_msg(gspca_dev->dev,
  332. usb_rcvbulkpipe(gspca_dev->dev, 0x82),
  333. buffer, data_len, &act_len,
  334. JL2005C_DATA_TIMEOUT);
  335. if (ret < 0 || act_len < data_len)
  336. goto quit_stream;
  337. PDEBUG(D_PACK,
  338. "Got %d bytes out of %d for frame",
  339. data_len, bytes_left);
  340. bytes_left -= data_len;
  341. if (bytes_left == 0) {
  342. packet_type = LAST_PACKET;
  343. header_read = 0;
  344. } else
  345. packet_type = INTER_PACKET;
  346. gspca_frame_add(gspca_dev, packet_type,
  347. buffer, data_len);
  348. }
  349. }
  350. quit_stream:
  351. if (gspca_dev->present) {
  352. mutex_lock(&gspca_dev->usb_lock);
  353. jl2005c_stop(gspca_dev);
  354. mutex_unlock(&gspca_dev->usb_lock);
  355. }
  356. kfree(buffer);
  357. }
  358. /* This function is called at probe time */
  359. static int sd_config(struct gspca_dev *gspca_dev,
  360. const struct usb_device_id *id)
  361. {
  362. struct cam *cam;
  363. struct sd *sd = (struct sd *) gspca_dev;
  364. cam = &gspca_dev->cam;
  365. /* We don't use the buffer gspca allocates so make it small. */
  366. cam->bulk_size = 64;
  367. cam->bulk = 1;
  368. /* For the rest, the camera needs to be detected */
  369. jl2005c_get_firmware_id(gspca_dev);
  370. /* Here are some known firmware IDs
  371. * First some JL2005B cameras
  372. * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
  373. * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
  374. * JL2005C cameras
  375. * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
  376. * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
  377. * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
  378. *
  379. * Based upon this scanty evidence, we can detect a CIF camera by
  380. * testing byte 0 for 0x4x.
  381. */
  382. if ((sd->firmware_id[0] & 0xf0) == 0x40) {
  383. cam->cam_mode = cif_mode;
  384. cam->nmodes = ARRAY_SIZE(cif_mode);
  385. sd->block_size = 0x80;
  386. } else {
  387. cam->cam_mode = vga_mode;
  388. cam->nmodes = ARRAY_SIZE(vga_mode);
  389. sd->block_size = 0x200;
  390. }
  391. INIT_WORK(&sd->work_struct, jl2005c_dostream);
  392. return 0;
  393. }
  394. /* this function is called at probe and resume time */
  395. static int sd_init(struct gspca_dev *gspca_dev)
  396. {
  397. return 0;
  398. }
  399. static int sd_start(struct gspca_dev *gspca_dev)
  400. {
  401. struct sd *sd = (struct sd *) gspca_dev;
  402. sd->cap_mode = gspca_dev->cam.cam_mode;
  403. switch (gspca_dev->pixfmt.width) {
  404. case 640:
  405. PDEBUG(D_STREAM, "Start streaming at vga resolution");
  406. jl2005c_stream_start_vga_lg(gspca_dev);
  407. break;
  408. case 320:
  409. PDEBUG(D_STREAM, "Start streaming at qvga resolution");
  410. jl2005c_stream_start_vga_small(gspca_dev);
  411. break;
  412. case 352:
  413. PDEBUG(D_STREAM, "Start streaming at cif resolution");
  414. jl2005c_stream_start_cif_lg(gspca_dev);
  415. break;
  416. case 176:
  417. PDEBUG(D_STREAM, "Start streaming at qcif resolution");
  418. jl2005c_stream_start_cif_small(gspca_dev);
  419. break;
  420. default:
  421. pr_err("Unknown resolution specified\n");
  422. return -1;
  423. }
  424. schedule_work(&sd->work_struct);
  425. return 0;
  426. }
  427. /* called on streamoff with alt==0 and on disconnect */
  428. /* the usb_lock is held at entry - restore on exit */
  429. static void sd_stop0(struct gspca_dev *gspca_dev)
  430. {
  431. struct sd *dev = (struct sd *) gspca_dev;
  432. /* wait for the work queue to terminate */
  433. mutex_unlock(&gspca_dev->usb_lock);
  434. /* This waits for sq905c_dostream to finish */
  435. flush_work(&dev->work_struct);
  436. mutex_lock(&gspca_dev->usb_lock);
  437. }
  438. /* sub-driver description */
  439. static const struct sd_desc sd_desc = {
  440. .name = MODULE_NAME,
  441. .config = sd_config,
  442. .init = sd_init,
  443. .start = sd_start,
  444. .stop0 = sd_stop0,
  445. };
  446. /* -- module initialisation -- */
  447. static const struct usb_device_id device_table[] = {
  448. {USB_DEVICE(0x0979, 0x0227)},
  449. {}
  450. };
  451. MODULE_DEVICE_TABLE(usb, device_table);
  452. /* -- device connect -- */
  453. static int sd_probe(struct usb_interface *intf,
  454. const struct usb_device_id *id)
  455. {
  456. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  457. THIS_MODULE);
  458. }
  459. static struct usb_driver sd_driver = {
  460. .name = MODULE_NAME,
  461. .id_table = device_table,
  462. .probe = sd_probe,
  463. .disconnect = gspca_disconnect,
  464. #ifdef CONFIG_PM
  465. .suspend = gspca_suspend,
  466. .resume = gspca_resume,
  467. .reset_resume = gspca_resume,
  468. #endif
  469. };
  470. module_usb_driver(sd_driver);