jeilinj.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Jeilinj subdriver
  3. *
  4. * Supports some Jeilin dual-mode cameras which use bulk transport and
  5. * download raw JPEG data.
  6. *
  7. * Copyright (C) 2009 Theodore Kilgore
  8. *
  9. * Sportscam DV15 support and control settings are
  10. * Copyright (C) 2011 Patrice Chotard
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #define MODULE_NAME "jeilinj"
  28. #include <linux/slab.h>
  29. #include "gspca.h"
  30. #include "jpeg.h"
  31. MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
  32. MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
  33. MODULE_LICENSE("GPL");
  34. /* Default timeouts, in ms */
  35. #define JEILINJ_CMD_TIMEOUT 500
  36. #define JEILINJ_CMD_DELAY 160
  37. #define JEILINJ_DATA_TIMEOUT 1000
  38. /* Maximum transfer size to use. */
  39. #define JEILINJ_MAX_TRANSFER 0x200
  40. #define FRAME_HEADER_LEN 0x10
  41. #define FRAME_START 0xFFFFFFFF
  42. enum {
  43. SAKAR_57379,
  44. SPORTSCAM_DV15,
  45. };
  46. #define CAMQUALITY_MIN 0 /* highest cam quality */
  47. #define CAMQUALITY_MAX 97 /* lowest cam quality */
  48. /* Structure to hold all of our device specific stuff */
  49. struct sd {
  50. struct gspca_dev gspca_dev; /* !! must be the first item */
  51. int blocks_left;
  52. const struct v4l2_pix_format *cap_mode;
  53. struct v4l2_ctrl *freq;
  54. struct v4l2_ctrl *jpegqual;
  55. /* Driver stuff */
  56. u8 type;
  57. u8 quality; /* image quality */
  58. #define QUALITY_MIN 35
  59. #define QUALITY_MAX 85
  60. #define QUALITY_DEF 85
  61. u8 jpeg_hdr[JPEG_HDR_SZ];
  62. };
  63. struct jlj_command {
  64. unsigned char instruction[2];
  65. unsigned char ack_wanted;
  66. unsigned char delay;
  67. };
  68. /* AFAICT these cameras will only do 320x240. */
  69. static struct v4l2_pix_format jlj_mode[] = {
  70. { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  71. .bytesperline = 320,
  72. .sizeimage = 320 * 240,
  73. .colorspace = V4L2_COLORSPACE_JPEG,
  74. .priv = 0},
  75. { 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  76. .bytesperline = 640,
  77. .sizeimage = 640 * 480,
  78. .colorspace = V4L2_COLORSPACE_JPEG,
  79. .priv = 0}
  80. };
  81. /*
  82. * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
  83. * and 0x82 for bulk transfer.
  84. */
  85. /* All commands are two bytes only */
  86. static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
  87. {
  88. int retval;
  89. if (gspca_dev->usb_err < 0)
  90. return;
  91. memcpy(gspca_dev->usb_buf, command, 2);
  92. retval = usb_bulk_msg(gspca_dev->dev,
  93. usb_sndbulkpipe(gspca_dev->dev, 3),
  94. gspca_dev->usb_buf, 2, NULL, 500);
  95. if (retval < 0) {
  96. pr_err("command write [%02x] error %d\n",
  97. gspca_dev->usb_buf[0], retval);
  98. gspca_dev->usb_err = retval;
  99. }
  100. }
  101. /* Responses are one byte only */
  102. static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char *response)
  103. {
  104. int retval;
  105. if (gspca_dev->usb_err < 0)
  106. return;
  107. retval = usb_bulk_msg(gspca_dev->dev,
  108. usb_rcvbulkpipe(gspca_dev->dev, 0x84),
  109. gspca_dev->usb_buf, 1, NULL, 500);
  110. *response = gspca_dev->usb_buf[0];
  111. if (retval < 0) {
  112. pr_err("read command [%02x] error %d\n",
  113. gspca_dev->usb_buf[0], retval);
  114. gspca_dev->usb_err = retval;
  115. }
  116. }
  117. static void setfreq(struct gspca_dev *gspca_dev, s32 val)
  118. {
  119. u8 freq_commands[][2] = {
  120. {0x71, 0x80},
  121. {0x70, 0x07}
  122. };
  123. freq_commands[0][1] |= val >> 1;
  124. jlj_write2(gspca_dev, freq_commands[0]);
  125. jlj_write2(gspca_dev, freq_commands[1]);
  126. }
  127. static void setcamquality(struct gspca_dev *gspca_dev, s32 val)
  128. {
  129. u8 quality_commands[][2] = {
  130. {0x71, 0x1E},
  131. {0x70, 0x06}
  132. };
  133. u8 camquality;
  134. /* adapt camera quality from jpeg quality */
  135. camquality = ((QUALITY_MAX - val) * CAMQUALITY_MAX)
  136. / (QUALITY_MAX - QUALITY_MIN);
  137. quality_commands[0][1] += camquality;
  138. jlj_write2(gspca_dev, quality_commands[0]);
  139. jlj_write2(gspca_dev, quality_commands[1]);
  140. }
  141. static void setautogain(struct gspca_dev *gspca_dev, s32 val)
  142. {
  143. u8 autogain_commands[][2] = {
  144. {0x94, 0x02},
  145. {0xcf, 0x00}
  146. };
  147. autogain_commands[1][1] = val << 4;
  148. jlj_write2(gspca_dev, autogain_commands[0]);
  149. jlj_write2(gspca_dev, autogain_commands[1]);
  150. }
  151. static void setred(struct gspca_dev *gspca_dev, s32 val)
  152. {
  153. u8 setred_commands[][2] = {
  154. {0x94, 0x02},
  155. {0xe6, 0x00}
  156. };
  157. setred_commands[1][1] = val;
  158. jlj_write2(gspca_dev, setred_commands[0]);
  159. jlj_write2(gspca_dev, setred_commands[1]);
  160. }
  161. static void setgreen(struct gspca_dev *gspca_dev, s32 val)
  162. {
  163. u8 setgreen_commands[][2] = {
  164. {0x94, 0x02},
  165. {0xe7, 0x00}
  166. };
  167. setgreen_commands[1][1] = val;
  168. jlj_write2(gspca_dev, setgreen_commands[0]);
  169. jlj_write2(gspca_dev, setgreen_commands[1]);
  170. }
  171. static void setblue(struct gspca_dev *gspca_dev, s32 val)
  172. {
  173. u8 setblue_commands[][2] = {
  174. {0x94, 0x02},
  175. {0xe9, 0x00}
  176. };
  177. setblue_commands[1][1] = val;
  178. jlj_write2(gspca_dev, setblue_commands[0]);
  179. jlj_write2(gspca_dev, setblue_commands[1]);
  180. }
  181. static int jlj_start(struct gspca_dev *gspca_dev)
  182. {
  183. int i;
  184. int start_commands_size;
  185. u8 response = 0xff;
  186. struct sd *sd = (struct sd *) gspca_dev;
  187. struct jlj_command start_commands[] = {
  188. {{0x71, 0x81}, 0, 0},
  189. {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
  190. {{0x95, 0x70}, 1, 0},
  191. {{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
  192. {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
  193. {{0x95, 0x70}, 1, 0},
  194. {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
  195. {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
  196. {{0x95, 0x70}, 1, 0},
  197. #define SPORTSCAM_DV15_CMD_SIZE 9
  198. {{0x94, 0x02}, 0, 0},
  199. {{0xde, 0x24}, 0, 0},
  200. {{0x94, 0x02}, 0, 0},
  201. {{0xdd, 0xf0}, 0, 0},
  202. {{0x94, 0x02}, 0, 0},
  203. {{0xe3, 0x2c}, 0, 0},
  204. {{0x94, 0x02}, 0, 0},
  205. {{0xe4, 0x00}, 0, 0},
  206. {{0x94, 0x02}, 0, 0},
  207. {{0xe5, 0x00}, 0, 0},
  208. {{0x94, 0x02}, 0, 0},
  209. {{0xe6, 0x2c}, 0, 0},
  210. {{0x94, 0x03}, 0, 0},
  211. {{0xaa, 0x00}, 0, 0}
  212. };
  213. sd->blocks_left = 0;
  214. /* Under Windows, USB spy shows that only the 9 first start
  215. * commands are used for SPORTSCAM_DV15 webcam
  216. */
  217. if (sd->type == SPORTSCAM_DV15)
  218. start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
  219. else
  220. start_commands_size = ARRAY_SIZE(start_commands);
  221. for (i = 0; i < start_commands_size; i++) {
  222. jlj_write2(gspca_dev, start_commands[i].instruction);
  223. if (start_commands[i].delay)
  224. msleep(start_commands[i].delay);
  225. if (start_commands[i].ack_wanted)
  226. jlj_read1(gspca_dev, &response);
  227. }
  228. setcamquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
  229. msleep(2);
  230. setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
  231. if (gspca_dev->usb_err < 0)
  232. PERR("Start streaming command failed");
  233. return gspca_dev->usb_err;
  234. }
  235. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  236. u8 *data, int len)
  237. {
  238. struct sd *sd = (struct sd *) gspca_dev;
  239. int packet_type;
  240. u32 header_marker;
  241. PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
  242. len, JEILINJ_MAX_TRANSFER);
  243. if (len != JEILINJ_MAX_TRANSFER) {
  244. PDEBUG(D_PACK, "bad length");
  245. goto discard;
  246. }
  247. /* check if it's start of frame */
  248. header_marker = ((u32 *)data)[0];
  249. if (header_marker == FRAME_START) {
  250. sd->blocks_left = data[0x0a] - 1;
  251. PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
  252. /* Start a new frame, and add the JPEG header, first thing */
  253. gspca_frame_add(gspca_dev, FIRST_PACKET,
  254. sd->jpeg_hdr, JPEG_HDR_SZ);
  255. /* Toss line 0 of data block 0, keep the rest. */
  256. gspca_frame_add(gspca_dev, INTER_PACKET,
  257. data + FRAME_HEADER_LEN,
  258. JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
  259. } else if (sd->blocks_left > 0) {
  260. PDEBUG(D_STREAM, "%d blocks remaining for frame",
  261. sd->blocks_left);
  262. sd->blocks_left -= 1;
  263. if (sd->blocks_left == 0)
  264. packet_type = LAST_PACKET;
  265. else
  266. packet_type = INTER_PACKET;
  267. gspca_frame_add(gspca_dev, packet_type,
  268. data, JEILINJ_MAX_TRANSFER);
  269. } else
  270. goto discard;
  271. return;
  272. discard:
  273. /* Discard data until a new frame starts. */
  274. gspca_dev->last_packet_type = DISCARD_PACKET;
  275. }
  276. /* This function is called at probe time just before sd_init */
  277. static int sd_config(struct gspca_dev *gspca_dev,
  278. const struct usb_device_id *id)
  279. {
  280. struct cam *cam = &gspca_dev->cam;
  281. struct sd *dev = (struct sd *) gspca_dev;
  282. dev->type = id->driver_info;
  283. dev->quality = QUALITY_DEF;
  284. cam->cam_mode = jlj_mode;
  285. cam->nmodes = ARRAY_SIZE(jlj_mode);
  286. cam->bulk = 1;
  287. cam->bulk_nurbs = 1;
  288. cam->bulk_size = JEILINJ_MAX_TRANSFER;
  289. return 0;
  290. }
  291. static void sd_stopN(struct gspca_dev *gspca_dev)
  292. {
  293. int i;
  294. u8 *buf;
  295. static u8 stop_commands[][2] = {
  296. {0x71, 0x00},
  297. {0x70, 0x09},
  298. {0x71, 0x80},
  299. {0x70, 0x05}
  300. };
  301. for (;;) {
  302. /* get the image remaining blocks */
  303. usb_bulk_msg(gspca_dev->dev,
  304. gspca_dev->urb[0]->pipe,
  305. gspca_dev->urb[0]->transfer_buffer,
  306. JEILINJ_MAX_TRANSFER, NULL,
  307. JEILINJ_DATA_TIMEOUT);
  308. /* search for 0xff 0xd9 (EOF for JPEG) */
  309. i = 0;
  310. buf = gspca_dev->urb[0]->transfer_buffer;
  311. while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
  312. ((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
  313. i++;
  314. if (i != (JEILINJ_MAX_TRANSFER - 1))
  315. /* last remaining block found */
  316. break;
  317. }
  318. for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
  319. jlj_write2(gspca_dev, stop_commands[i]);
  320. }
  321. /* this function is called at probe and resume time */
  322. static int sd_init(struct gspca_dev *gspca_dev)
  323. {
  324. return gspca_dev->usb_err;
  325. }
  326. /* Set up for getting frames. */
  327. static int sd_start(struct gspca_dev *gspca_dev)
  328. {
  329. struct sd *dev = (struct sd *) gspca_dev;
  330. /* create the JPEG header */
  331. jpeg_define(dev->jpeg_hdr, gspca_dev->pixfmt.height,
  332. gspca_dev->pixfmt.width,
  333. 0x21); /* JPEG 422 */
  334. jpeg_set_qual(dev->jpeg_hdr, dev->quality);
  335. PDEBUG(D_STREAM, "Start streaming at %dx%d",
  336. gspca_dev->pixfmt.height, gspca_dev->pixfmt.width);
  337. jlj_start(gspca_dev);
  338. return gspca_dev->usb_err;
  339. }
  340. /* Table of supported USB devices */
  341. static const struct usb_device_id device_table[] = {
  342. {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
  343. {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
  344. {}
  345. };
  346. MODULE_DEVICE_TABLE(usb, device_table);
  347. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  348. {
  349. struct gspca_dev *gspca_dev =
  350. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  351. struct sd *sd = (struct sd *)gspca_dev;
  352. gspca_dev->usb_err = 0;
  353. if (!gspca_dev->streaming)
  354. return 0;
  355. switch (ctrl->id) {
  356. case V4L2_CID_POWER_LINE_FREQUENCY:
  357. setfreq(gspca_dev, ctrl->val);
  358. break;
  359. case V4L2_CID_RED_BALANCE:
  360. setred(gspca_dev, ctrl->val);
  361. break;
  362. case V4L2_CID_GAIN:
  363. setgreen(gspca_dev, ctrl->val);
  364. break;
  365. case V4L2_CID_BLUE_BALANCE:
  366. setblue(gspca_dev, ctrl->val);
  367. break;
  368. case V4L2_CID_AUTOGAIN:
  369. setautogain(gspca_dev, ctrl->val);
  370. break;
  371. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  372. jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
  373. setcamquality(gspca_dev, ctrl->val);
  374. break;
  375. }
  376. return gspca_dev->usb_err;
  377. }
  378. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  379. .s_ctrl = sd_s_ctrl,
  380. };
  381. static int sd_init_controls(struct gspca_dev *gspca_dev)
  382. {
  383. struct sd *sd = (struct sd *)gspca_dev;
  384. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  385. static const struct v4l2_ctrl_config custom_autogain = {
  386. .ops = &sd_ctrl_ops,
  387. .id = V4L2_CID_AUTOGAIN,
  388. .type = V4L2_CTRL_TYPE_INTEGER,
  389. .name = "Automatic Gain (and Exposure)",
  390. .max = 3,
  391. .step = 1,
  392. .def = 0,
  393. };
  394. gspca_dev->vdev.ctrl_handler = hdl;
  395. v4l2_ctrl_handler_init(hdl, 6);
  396. sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
  397. V4L2_CID_POWER_LINE_FREQUENCY,
  398. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
  399. V4L2_CID_POWER_LINE_FREQUENCY_60HZ);
  400. v4l2_ctrl_new_custom(hdl, &custom_autogain, NULL);
  401. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  402. V4L2_CID_RED_BALANCE, 0, 3, 1, 2);
  403. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  404. V4L2_CID_GAIN, 0, 3, 1, 2);
  405. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  406. V4L2_CID_BLUE_BALANCE, 0, 3, 1, 2);
  407. sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  408. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  409. QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
  410. if (hdl->error) {
  411. pr_err("Could not initialize controls\n");
  412. return hdl->error;
  413. }
  414. return 0;
  415. }
  416. static int sd_set_jcomp(struct gspca_dev *gspca_dev,
  417. const struct v4l2_jpegcompression *jcomp)
  418. {
  419. struct sd *sd = (struct sd *) gspca_dev;
  420. v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
  421. return 0;
  422. }
  423. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  424. struct v4l2_jpegcompression *jcomp)
  425. {
  426. struct sd *sd = (struct sd *) gspca_dev;
  427. memset(jcomp, 0, sizeof *jcomp);
  428. jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
  429. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  430. | V4L2_JPEG_MARKER_DQT;
  431. return 0;
  432. }
  433. /* sub-driver description */
  434. static const struct sd_desc sd_desc_sakar_57379 = {
  435. .name = MODULE_NAME,
  436. .config = sd_config,
  437. .init = sd_init,
  438. .start = sd_start,
  439. .stopN = sd_stopN,
  440. .pkt_scan = sd_pkt_scan,
  441. };
  442. /* sub-driver description */
  443. static const struct sd_desc sd_desc_sportscam_dv15 = {
  444. .name = MODULE_NAME,
  445. .config = sd_config,
  446. .init = sd_init,
  447. .init_controls = sd_init_controls,
  448. .start = sd_start,
  449. .stopN = sd_stopN,
  450. .pkt_scan = sd_pkt_scan,
  451. .get_jcomp = sd_get_jcomp,
  452. .set_jcomp = sd_set_jcomp,
  453. };
  454. static const struct sd_desc *sd_desc[2] = {
  455. &sd_desc_sakar_57379,
  456. &sd_desc_sportscam_dv15
  457. };
  458. /* -- device connect -- */
  459. static int sd_probe(struct usb_interface *intf,
  460. const struct usb_device_id *id)
  461. {
  462. return gspca_dev_probe(intf, id,
  463. sd_desc[id->driver_info],
  464. sizeof(struct sd),
  465. THIS_MODULE);
  466. }
  467. static struct usb_driver sd_driver = {
  468. .name = MODULE_NAME,
  469. .id_table = device_table,
  470. .probe = sd_probe,
  471. .disconnect = gspca_disconnect,
  472. #ifdef CONFIG_PM
  473. .suspend = gspca_suspend,
  474. .resume = gspca_resume,
  475. .reset_resume = gspca_resume,
  476. #endif
  477. };
  478. module_usb_driver(sd_driver);