mars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Mars-Semi MR97311A library
  3. * Copyright (C) 2005 <bradlch@hotmail.com>
  4. *
  5. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #define MODULE_NAME "mars"
  23. #include "gspca.h"
  24. #include "jpeg.h"
  25. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  26. MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
  27. MODULE_LICENSE("GPL");
  28. /* controls */
  29. enum e_ctrl {
  30. BRIGHTNESS,
  31. COLORS,
  32. GAMMA,
  33. SHARPNESS,
  34. ILLUM_TOP,
  35. ILLUM_BOT,
  36. NCTRLS /* number of controls */
  37. };
  38. /* specific webcam descriptor */
  39. struct sd {
  40. struct gspca_dev gspca_dev; /* !! must be the first item */
  41. struct gspca_ctrl ctrls[NCTRLS];
  42. u8 quality;
  43. #define QUALITY_MIN 40
  44. #define QUALITY_MAX 70
  45. #define QUALITY_DEF 50
  46. u8 jpeg_hdr[JPEG_HDR_SZ];
  47. };
  48. /* V4L2 controls supported by the driver */
  49. static void setbrightness(struct gspca_dev *gspca_dev);
  50. static void setcolors(struct gspca_dev *gspca_dev);
  51. static void setgamma(struct gspca_dev *gspca_dev);
  52. static void setsharpness(struct gspca_dev *gspca_dev);
  53. static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
  54. static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
  55. static const struct ctrl sd_ctrls[NCTRLS] = {
  56. [BRIGHTNESS] = {
  57. {
  58. .id = V4L2_CID_BRIGHTNESS,
  59. .type = V4L2_CTRL_TYPE_INTEGER,
  60. .name = "Brightness",
  61. .minimum = 0,
  62. .maximum = 30,
  63. .step = 1,
  64. .default_value = 15,
  65. },
  66. .set_control = setbrightness
  67. },
  68. [COLORS] = {
  69. {
  70. .id = V4L2_CID_SATURATION,
  71. .type = V4L2_CTRL_TYPE_INTEGER,
  72. .name = "Color",
  73. .minimum = 1,
  74. .maximum = 255,
  75. .step = 1,
  76. .default_value = 200,
  77. },
  78. .set_control = setcolors
  79. },
  80. [GAMMA] = {
  81. {
  82. .id = V4L2_CID_GAMMA,
  83. .type = V4L2_CTRL_TYPE_INTEGER,
  84. .name = "Gamma",
  85. .minimum = 0,
  86. .maximum = 3,
  87. .step = 1,
  88. .default_value = 1,
  89. },
  90. .set_control = setgamma
  91. },
  92. [SHARPNESS] = {
  93. {
  94. .id = V4L2_CID_SHARPNESS,
  95. .type = V4L2_CTRL_TYPE_INTEGER,
  96. .name = "Sharpness",
  97. .minimum = 0,
  98. .maximum = 2,
  99. .step = 1,
  100. .default_value = 1,
  101. },
  102. .set_control = setsharpness
  103. },
  104. [ILLUM_TOP] = {
  105. {
  106. .id = V4L2_CID_ILLUMINATORS_1,
  107. .type = V4L2_CTRL_TYPE_BOOLEAN,
  108. .name = "Top illuminator",
  109. .minimum = 0,
  110. .maximum = 1,
  111. .step = 1,
  112. .default_value = 0,
  113. .flags = V4L2_CTRL_FLAG_UPDATE,
  114. },
  115. .set = sd_setilluminator1
  116. },
  117. [ILLUM_BOT] = {
  118. {
  119. .id = V4L2_CID_ILLUMINATORS_2,
  120. .type = V4L2_CTRL_TYPE_BOOLEAN,
  121. .name = "Bottom illuminator",
  122. .minimum = 0,
  123. .maximum = 1,
  124. .step = 1,
  125. .default_value = 0,
  126. .flags = V4L2_CTRL_FLAG_UPDATE,
  127. },
  128. .set = sd_setilluminator2
  129. },
  130. };
  131. static const struct v4l2_pix_format vga_mode[] = {
  132. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  133. .bytesperline = 320,
  134. .sizeimage = 320 * 240 * 3 / 8 + 590,
  135. .colorspace = V4L2_COLORSPACE_JPEG,
  136. .priv = 2},
  137. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  138. .bytesperline = 640,
  139. .sizeimage = 640 * 480 * 3 / 8 + 590,
  140. .colorspace = V4L2_COLORSPACE_JPEG,
  141. .priv = 1},
  142. };
  143. static const __u8 mi_data[0x20] = {
  144. /* 01 02 03 04 05 06 07 08 */
  145. 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
  146. /* 09 0a 0b 0c 0d 0e 0f 10 */
  147. 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
  148. /* 11 12 13 14 15 16 17 18 */
  149. 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
  150. /* 19 1a 1b 1c 1d 1e 1f 20 */
  151. 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
  152. };
  153. /* write <len> bytes from gspca_dev->usb_buf */
  154. static void reg_w(struct gspca_dev *gspca_dev,
  155. int len)
  156. {
  157. int alen, ret;
  158. if (gspca_dev->usb_err < 0)
  159. return;
  160. ret = usb_bulk_msg(gspca_dev->dev,
  161. usb_sndbulkpipe(gspca_dev->dev, 4),
  162. gspca_dev->usb_buf,
  163. len,
  164. &alen,
  165. 500); /* timeout in milliseconds */
  166. if (ret < 0) {
  167. pr_err("reg write [%02x] error %d\n",
  168. gspca_dev->usb_buf[0], ret);
  169. gspca_dev->usb_err = ret;
  170. }
  171. }
  172. static void mi_w(struct gspca_dev *gspca_dev,
  173. u8 addr,
  174. u8 value)
  175. {
  176. gspca_dev->usb_buf[0] = 0x1f;
  177. gspca_dev->usb_buf[1] = 0; /* control byte */
  178. gspca_dev->usb_buf[2] = addr;
  179. gspca_dev->usb_buf[3] = value;
  180. reg_w(gspca_dev, 4);
  181. }
  182. static void setbrightness(struct gspca_dev *gspca_dev)
  183. {
  184. struct sd *sd = (struct sd *) gspca_dev;
  185. gspca_dev->usb_buf[0] = 0x61;
  186. gspca_dev->usb_buf[1] = sd->ctrls[BRIGHTNESS].val;
  187. reg_w(gspca_dev, 2);
  188. }
  189. static void setcolors(struct gspca_dev *gspca_dev)
  190. {
  191. struct sd *sd = (struct sd *) gspca_dev;
  192. s16 val;
  193. val = sd->ctrls[COLORS].val;
  194. gspca_dev->usb_buf[0] = 0x5f;
  195. gspca_dev->usb_buf[1] = val << 3;
  196. gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
  197. reg_w(gspca_dev, 3);
  198. }
  199. static void setgamma(struct gspca_dev *gspca_dev)
  200. {
  201. struct sd *sd = (struct sd *) gspca_dev;
  202. gspca_dev->usb_buf[0] = 0x06;
  203. gspca_dev->usb_buf[1] = sd->ctrls[GAMMA].val * 0x40;
  204. reg_w(gspca_dev, 2);
  205. }
  206. static void setsharpness(struct gspca_dev *gspca_dev)
  207. {
  208. struct sd *sd = (struct sd *) gspca_dev;
  209. gspca_dev->usb_buf[0] = 0x67;
  210. gspca_dev->usb_buf[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
  211. reg_w(gspca_dev, 2);
  212. }
  213. static void setilluminators(struct gspca_dev *gspca_dev)
  214. {
  215. struct sd *sd = (struct sd *) gspca_dev;
  216. gspca_dev->usb_buf[0] = 0x22;
  217. if (sd->ctrls[ILLUM_TOP].val)
  218. gspca_dev->usb_buf[1] = 0x76;
  219. else if (sd->ctrls[ILLUM_BOT].val)
  220. gspca_dev->usb_buf[1] = 0x7a;
  221. else
  222. gspca_dev->usb_buf[1] = 0x7e;
  223. reg_w(gspca_dev, 2);
  224. }
  225. /* this function is called at probe time */
  226. static int sd_config(struct gspca_dev *gspca_dev,
  227. const struct usb_device_id *id)
  228. {
  229. struct sd *sd = (struct sd *) gspca_dev;
  230. struct cam *cam;
  231. cam = &gspca_dev->cam;
  232. cam->cam_mode = vga_mode;
  233. cam->nmodes = ARRAY_SIZE(vga_mode);
  234. cam->ctrls = sd->ctrls;
  235. sd->quality = QUALITY_DEF;
  236. return 0;
  237. }
  238. /* this function is called at probe and resume time */
  239. static int sd_init(struct gspca_dev *gspca_dev)
  240. {
  241. gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
  242. return 0;
  243. }
  244. static int sd_start(struct gspca_dev *gspca_dev)
  245. {
  246. struct sd *sd = (struct sd *) gspca_dev;
  247. u8 *data;
  248. int i;
  249. /* create the JPEG header */
  250. jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  251. 0x21); /* JPEG 422 */
  252. jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  253. data = gspca_dev->usb_buf;
  254. data[0] = 0x01; /* address */
  255. data[1] = 0x01;
  256. reg_w(gspca_dev, 2);
  257. /*
  258. Initialize the MR97113 chip register
  259. */
  260. data[0] = 0x00; /* address */
  261. data[1] = 0x0c | 0x01; /* reg 0 */
  262. data[2] = 0x01; /* reg 1 */
  263. data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
  264. data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
  265. data[5] = 0x30; /* reg 4, MI, PAS5101 :
  266. * 0x30 for 24mhz , 0x28 for 12mhz */
  267. data[6] = 0x02; /* reg 5, H start - was 0x04 */
  268. data[7] = sd->ctrls[GAMMA].val * 0x40; /* reg 0x06: gamma */
  269. data[8] = 0x01; /* reg 7, V start - was 0x03 */
  270. /* if (h_size == 320 ) */
  271. /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
  272. /* else */
  273. data[9] = 0x52; /* reg 8, 24MHz, no scale down */
  274. /*jfm: from win trace*/
  275. data[10] = 0x18;
  276. reg_w(gspca_dev, 11);
  277. data[0] = 0x23; /* address */
  278. data[1] = 0x09; /* reg 35, append frame header */
  279. reg_w(gspca_dev, 2);
  280. data[0] = 0x3c; /* address */
  281. /* if (gspca_dev->width == 1280) */
  282. /* data[1] = 200; * reg 60, pc-cam frame size
  283. * (unit: 4KB) 800KB */
  284. /* else */
  285. data[1] = 50; /* 50 reg 60, pc-cam frame size
  286. * (unit: 4KB) 200KB */
  287. reg_w(gspca_dev, 2);
  288. /* auto dark-gain */
  289. data[0] = 0x5e; /* address */
  290. data[1] = 0; /* reg 94, Y Gain (auto) */
  291. /*jfm: from win trace*/
  292. /* reg 0x5f/0x60 (LE) = saturation */
  293. /* h (60): xxxx x100
  294. * l (5f): xxxx x000 */
  295. data[2] = sd->ctrls[COLORS].val << 3;
  296. data[3] = ((sd->ctrls[COLORS].val >> 2) & 0xf8) | 0x04;
  297. data[4] = sd->ctrls[BRIGHTNESS].val; /* reg 0x61 = brightness */
  298. data[5] = 0x00;
  299. reg_w(gspca_dev, 6);
  300. data[0] = 0x67;
  301. /*jfm: from win trace*/
  302. data[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
  303. data[2] = 0x14;
  304. reg_w(gspca_dev, 3);
  305. data[0] = 0x69;
  306. data[1] = 0x2f;
  307. data[2] = 0x28;
  308. data[3] = 0x42;
  309. reg_w(gspca_dev, 4);
  310. data[0] = 0x63;
  311. data[1] = 0x07;
  312. reg_w(gspca_dev, 2);
  313. /*jfm: win trace - many writes here to reg 0x64*/
  314. /* initialize the MI sensor */
  315. for (i = 0; i < sizeof mi_data; i++)
  316. mi_w(gspca_dev, i + 1, mi_data[i]);
  317. data[0] = 0x00;
  318. data[1] = 0x4d; /* ISOC transferring enable... */
  319. reg_w(gspca_dev, 2);
  320. gspca_dev->ctrl_inac = 0; /* activate the illuminator controls */
  321. return gspca_dev->usb_err;
  322. }
  323. static void sd_stopN(struct gspca_dev *gspca_dev)
  324. {
  325. struct sd *sd = (struct sd *) gspca_dev;
  326. gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
  327. if (sd->ctrls[ILLUM_TOP].val || sd->ctrls[ILLUM_BOT].val) {
  328. sd->ctrls[ILLUM_TOP].val = 0;
  329. sd->ctrls[ILLUM_BOT].val = 0;
  330. setilluminators(gspca_dev);
  331. msleep(20);
  332. }
  333. gspca_dev->usb_buf[0] = 1;
  334. gspca_dev->usb_buf[1] = 0;
  335. reg_w(gspca_dev, 2);
  336. }
  337. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  338. u8 *data, /* isoc packet */
  339. int len) /* iso packet length */
  340. {
  341. struct sd *sd = (struct sd *) gspca_dev;
  342. int p;
  343. if (len < 6) {
  344. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  345. return;
  346. }
  347. for (p = 0; p < len - 6; p++) {
  348. if (data[0 + p] == 0xff
  349. && data[1 + p] == 0xff
  350. && data[2 + p] == 0x00
  351. && data[3 + p] == 0xff
  352. && data[4 + p] == 0x96) {
  353. if (data[5 + p] == 0x64
  354. || data[5 + p] == 0x65
  355. || data[5 + p] == 0x66
  356. || data[5 + p] == 0x67) {
  357. PDEBUG(D_PACK, "sof offset: %d len: %d",
  358. p, len);
  359. gspca_frame_add(gspca_dev, LAST_PACKET,
  360. data, p);
  361. /* put the JPEG header */
  362. gspca_frame_add(gspca_dev, FIRST_PACKET,
  363. sd->jpeg_hdr, JPEG_HDR_SZ);
  364. data += p + 16;
  365. len -= p + 16;
  366. break;
  367. }
  368. }
  369. }
  370. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  371. }
  372. static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
  373. {
  374. struct sd *sd = (struct sd *) gspca_dev;
  375. /* only one illuminator may be on */
  376. sd->ctrls[ILLUM_TOP].val = val;
  377. if (val)
  378. sd->ctrls[ILLUM_BOT].val = 0;
  379. setilluminators(gspca_dev);
  380. return gspca_dev->usb_err;
  381. }
  382. static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
  383. {
  384. struct sd *sd = (struct sd *) gspca_dev;
  385. /* only one illuminator may be on */
  386. sd->ctrls[ILLUM_BOT].val = val;
  387. if (val)
  388. sd->ctrls[ILLUM_TOP].val = 0;
  389. setilluminators(gspca_dev);
  390. return gspca_dev->usb_err;
  391. }
  392. static int sd_set_jcomp(struct gspca_dev *gspca_dev,
  393. struct v4l2_jpegcompression *jcomp)
  394. {
  395. struct sd *sd = (struct sd *) gspca_dev;
  396. if (jcomp->quality < QUALITY_MIN)
  397. sd->quality = QUALITY_MIN;
  398. else if (jcomp->quality > QUALITY_MAX)
  399. sd->quality = QUALITY_MAX;
  400. else
  401. sd->quality = jcomp->quality;
  402. if (gspca_dev->streaming)
  403. jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  404. return 0;
  405. }
  406. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  407. struct v4l2_jpegcompression *jcomp)
  408. {
  409. struct sd *sd = (struct sd *) gspca_dev;
  410. memset(jcomp, 0, sizeof *jcomp);
  411. jcomp->quality = sd->quality;
  412. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  413. | V4L2_JPEG_MARKER_DQT;
  414. return 0;
  415. }
  416. /* sub-driver description */
  417. static const struct sd_desc sd_desc = {
  418. .name = MODULE_NAME,
  419. .ctrls = sd_ctrls,
  420. .nctrls = NCTRLS,
  421. .config = sd_config,
  422. .init = sd_init,
  423. .start = sd_start,
  424. .stopN = sd_stopN,
  425. .pkt_scan = sd_pkt_scan,
  426. .get_jcomp = sd_get_jcomp,
  427. .set_jcomp = sd_set_jcomp,
  428. };
  429. /* -- module initialisation -- */
  430. static const struct usb_device_id device_table[] = {
  431. {USB_DEVICE(0x093a, 0x050f)},
  432. {}
  433. };
  434. MODULE_DEVICE_TABLE(usb, device_table);
  435. /* -- device connect -- */
  436. static int sd_probe(struct usb_interface *intf,
  437. const struct usb_device_id *id)
  438. {
  439. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  440. THIS_MODULE);
  441. }
  442. static struct usb_driver sd_driver = {
  443. .name = MODULE_NAME,
  444. .id_table = device_table,
  445. .probe = sd_probe,
  446. .disconnect = gspca_disconnect,
  447. #ifdef CONFIG_PM
  448. .suspend = gspca_suspend,
  449. .resume = gspca_resume,
  450. #endif
  451. };
  452. module_usb_driver(sd_driver);