PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/usb/gspca/jl2005bcd.c

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