mars.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #define QUALITY 50
  29. /* specific webcam descriptor */
  30. struct sd {
  31. struct gspca_dev gspca_dev; /* !! must be the first item */
  32. struct v4l2_ctrl *brightness;
  33. struct v4l2_ctrl *saturation;
  34. struct v4l2_ctrl *sharpness;
  35. struct v4l2_ctrl *gamma;
  36. struct { /* illuminator control cluster */
  37. struct v4l2_ctrl *illum_top;
  38. struct v4l2_ctrl *illum_bottom;
  39. };
  40. u8 jpeg_hdr[JPEG_HDR_SZ];
  41. };
  42. /* V4L2 controls supported by the driver */
  43. static void setbrightness(struct gspca_dev *gspca_dev, s32 val);
  44. static void setcolors(struct gspca_dev *gspca_dev, s32 val);
  45. static void setgamma(struct gspca_dev *gspca_dev, s32 val);
  46. static void setsharpness(struct gspca_dev *gspca_dev, s32 val);
  47. static const struct v4l2_pix_format vga_mode[] = {
  48. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  49. .bytesperline = 320,
  50. .sizeimage = 320 * 240 * 3 / 8 + 590,
  51. .colorspace = V4L2_COLORSPACE_JPEG,
  52. .priv = 2},
  53. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  54. .bytesperline = 640,
  55. .sizeimage = 640 * 480 * 3 / 8 + 590,
  56. .colorspace = V4L2_COLORSPACE_JPEG,
  57. .priv = 1},
  58. };
  59. static const __u8 mi_data[0x20] = {
  60. /* 01 02 03 04 05 06 07 08 */
  61. 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
  62. /* 09 0a 0b 0c 0d 0e 0f 10 */
  63. 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
  64. /* 11 12 13 14 15 16 17 18 */
  65. 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
  66. /* 19 1a 1b 1c 1d 1e 1f 20 */
  67. 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
  68. };
  69. /* write <len> bytes from gspca_dev->usb_buf */
  70. static void reg_w(struct gspca_dev *gspca_dev,
  71. int len)
  72. {
  73. int alen, ret;
  74. if (gspca_dev->usb_err < 0)
  75. return;
  76. ret = usb_bulk_msg(gspca_dev->dev,
  77. usb_sndbulkpipe(gspca_dev->dev, 4),
  78. gspca_dev->usb_buf,
  79. len,
  80. &alen,
  81. 500); /* timeout in milliseconds */
  82. if (ret < 0) {
  83. pr_err("reg write [%02x] error %d\n",
  84. gspca_dev->usb_buf[0], ret);
  85. gspca_dev->usb_err = ret;
  86. }
  87. }
  88. static void mi_w(struct gspca_dev *gspca_dev,
  89. u8 addr,
  90. u8 value)
  91. {
  92. gspca_dev->usb_buf[0] = 0x1f;
  93. gspca_dev->usb_buf[1] = 0; /* control byte */
  94. gspca_dev->usb_buf[2] = addr;
  95. gspca_dev->usb_buf[3] = value;
  96. reg_w(gspca_dev, 4);
  97. }
  98. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  99. {
  100. gspca_dev->usb_buf[0] = 0x61;
  101. gspca_dev->usb_buf[1] = val;
  102. reg_w(gspca_dev, 2);
  103. }
  104. static void setcolors(struct gspca_dev *gspca_dev, s32 val)
  105. {
  106. gspca_dev->usb_buf[0] = 0x5f;
  107. gspca_dev->usb_buf[1] = val << 3;
  108. gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
  109. reg_w(gspca_dev, 3);
  110. }
  111. static void setgamma(struct gspca_dev *gspca_dev, s32 val)
  112. {
  113. gspca_dev->usb_buf[0] = 0x06;
  114. gspca_dev->usb_buf[1] = val * 0x40;
  115. reg_w(gspca_dev, 2);
  116. }
  117. static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
  118. {
  119. gspca_dev->usb_buf[0] = 0x67;
  120. gspca_dev->usb_buf[1] = val * 4 + 3;
  121. reg_w(gspca_dev, 2);
  122. }
  123. static void setilluminators(struct gspca_dev *gspca_dev, bool top, bool bottom)
  124. {
  125. /* both are off if not streaming */
  126. gspca_dev->usb_buf[0] = 0x22;
  127. if (top)
  128. gspca_dev->usb_buf[1] = 0x76;
  129. else if (bottom)
  130. gspca_dev->usb_buf[1] = 0x7a;
  131. else
  132. gspca_dev->usb_buf[1] = 0x7e;
  133. reg_w(gspca_dev, 2);
  134. }
  135. static int mars_s_ctrl(struct v4l2_ctrl *ctrl)
  136. {
  137. struct gspca_dev *gspca_dev =
  138. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  139. struct sd *sd = (struct sd *)gspca_dev;
  140. gspca_dev->usb_err = 0;
  141. if (ctrl->id == V4L2_CID_ILLUMINATORS_1) {
  142. /* only one can be on at a time */
  143. if (ctrl->is_new && ctrl->val)
  144. sd->illum_bottom->val = 0;
  145. if (sd->illum_bottom->is_new && sd->illum_bottom->val)
  146. sd->illum_top->val = 0;
  147. }
  148. if (!gspca_dev->streaming)
  149. return 0;
  150. switch (ctrl->id) {
  151. case V4L2_CID_BRIGHTNESS:
  152. setbrightness(gspca_dev, ctrl->val);
  153. break;
  154. case V4L2_CID_SATURATION:
  155. setcolors(gspca_dev, ctrl->val);
  156. break;
  157. case V4L2_CID_GAMMA:
  158. setgamma(gspca_dev, ctrl->val);
  159. break;
  160. case V4L2_CID_ILLUMINATORS_1:
  161. setilluminators(gspca_dev, sd->illum_top->val,
  162. sd->illum_bottom->val);
  163. break;
  164. case V4L2_CID_SHARPNESS:
  165. setsharpness(gspca_dev, ctrl->val);
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. return gspca_dev->usb_err;
  171. }
  172. static const struct v4l2_ctrl_ops mars_ctrl_ops = {
  173. .s_ctrl = mars_s_ctrl,
  174. };
  175. /* this function is called at probe time */
  176. static int sd_init_controls(struct gspca_dev *gspca_dev)
  177. {
  178. struct sd *sd = (struct sd *) gspca_dev;
  179. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  180. gspca_dev->vdev.ctrl_handler = hdl;
  181. v4l2_ctrl_handler_init(hdl, 6);
  182. sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  183. V4L2_CID_BRIGHTNESS, 0, 30, 1, 15);
  184. sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  185. V4L2_CID_SATURATION, 0, 255, 1, 200);
  186. sd->gamma = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  187. V4L2_CID_GAMMA, 0, 3, 1, 1);
  188. sd->sharpness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  189. V4L2_CID_SHARPNESS, 0, 2, 1, 1);
  190. sd->illum_top = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  191. V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
  192. sd->illum_top->flags |= V4L2_CTRL_FLAG_UPDATE;
  193. sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
  194. V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
  195. sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE;
  196. if (hdl->error) {
  197. pr_err("Could not initialize controls\n");
  198. return hdl->error;
  199. }
  200. v4l2_ctrl_cluster(2, &sd->illum_top);
  201. return 0;
  202. }
  203. /* this function is called at probe time */
  204. static int sd_config(struct gspca_dev *gspca_dev,
  205. const struct usb_device_id *id)
  206. {
  207. struct cam *cam;
  208. cam = &gspca_dev->cam;
  209. cam->cam_mode = vga_mode;
  210. cam->nmodes = ARRAY_SIZE(vga_mode);
  211. return 0;
  212. }
  213. /* this function is called at probe and resume time */
  214. static int sd_init(struct gspca_dev *gspca_dev)
  215. {
  216. return 0;
  217. }
  218. static int sd_start(struct gspca_dev *gspca_dev)
  219. {
  220. struct sd *sd = (struct sd *) gspca_dev;
  221. u8 *data;
  222. int i;
  223. /* create the JPEG header */
  224. jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
  225. gspca_dev->pixfmt.width,
  226. 0x21); /* JPEG 422 */
  227. jpeg_set_qual(sd->jpeg_hdr, QUALITY);
  228. data = gspca_dev->usb_buf;
  229. data[0] = 0x01; /* address */
  230. data[1] = 0x01;
  231. reg_w(gspca_dev, 2);
  232. /*
  233. Initialize the MR97113 chip register
  234. */
  235. data[0] = 0x00; /* address */
  236. data[1] = 0x0c | 0x01; /* reg 0 */
  237. data[2] = 0x01; /* reg 1 */
  238. data[3] = gspca_dev->pixfmt.width / 8; /* h_size , reg 2 */
  239. data[4] = gspca_dev->pixfmt.height / 8; /* v_size , reg 3 */
  240. data[5] = 0x30; /* reg 4, MI, PAS5101 :
  241. * 0x30 for 24mhz , 0x28 for 12mhz */
  242. data[6] = 0x02; /* reg 5, H start - was 0x04 */
  243. data[7] = v4l2_ctrl_g_ctrl(sd->gamma) * 0x40; /* reg 0x06: gamma */
  244. data[8] = 0x01; /* reg 7, V start - was 0x03 */
  245. /* if (h_size == 320 ) */
  246. /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
  247. /* else */
  248. data[9] = 0x52; /* reg 8, 24MHz, no scale down */
  249. /*jfm: from win trace*/
  250. data[10] = 0x18;
  251. reg_w(gspca_dev, 11);
  252. data[0] = 0x23; /* address */
  253. data[1] = 0x09; /* reg 35, append frame header */
  254. reg_w(gspca_dev, 2);
  255. data[0] = 0x3c; /* address */
  256. /* if (gspca_dev->width == 1280) */
  257. /* data[1] = 200; * reg 60, pc-cam frame size
  258. * (unit: 4KB) 800KB */
  259. /* else */
  260. data[1] = 50; /* 50 reg 60, pc-cam frame size
  261. * (unit: 4KB) 200KB */
  262. reg_w(gspca_dev, 2);
  263. /* auto dark-gain */
  264. data[0] = 0x5e; /* address */
  265. data[1] = 0; /* reg 94, Y Gain (auto) */
  266. /*jfm: from win trace*/
  267. /* reg 0x5f/0x60 (LE) = saturation */
  268. /* h (60): xxxx x100
  269. * l (5f): xxxx x000 */
  270. data[2] = v4l2_ctrl_g_ctrl(sd->saturation) << 3;
  271. data[3] = ((v4l2_ctrl_g_ctrl(sd->saturation) >> 2) & 0xf8) | 0x04;
  272. data[4] = v4l2_ctrl_g_ctrl(sd->brightness); /* reg 0x61 = brightness */
  273. data[5] = 0x00;
  274. reg_w(gspca_dev, 6);
  275. data[0] = 0x67;
  276. /*jfm: from win trace*/
  277. data[1] = v4l2_ctrl_g_ctrl(sd->sharpness) * 4 + 3;
  278. data[2] = 0x14;
  279. reg_w(gspca_dev, 3);
  280. data[0] = 0x69;
  281. data[1] = 0x2f;
  282. data[2] = 0x28;
  283. data[3] = 0x42;
  284. reg_w(gspca_dev, 4);
  285. data[0] = 0x63;
  286. data[1] = 0x07;
  287. reg_w(gspca_dev, 2);
  288. /*jfm: win trace - many writes here to reg 0x64*/
  289. /* initialize the MI sensor */
  290. for (i = 0; i < sizeof mi_data; i++)
  291. mi_w(gspca_dev, i + 1, mi_data[i]);
  292. data[0] = 0x00;
  293. data[1] = 0x4d; /* ISOC transferring enable... */
  294. reg_w(gspca_dev, 2);
  295. setilluminators(gspca_dev, v4l2_ctrl_g_ctrl(sd->illum_top),
  296. v4l2_ctrl_g_ctrl(sd->illum_bottom));
  297. return gspca_dev->usb_err;
  298. }
  299. static void sd_stopN(struct gspca_dev *gspca_dev)
  300. {
  301. struct sd *sd = (struct sd *) gspca_dev;
  302. if (v4l2_ctrl_g_ctrl(sd->illum_top) ||
  303. v4l2_ctrl_g_ctrl(sd->illum_bottom)) {
  304. setilluminators(gspca_dev, false, false);
  305. msleep(20);
  306. }
  307. gspca_dev->usb_buf[0] = 1;
  308. gspca_dev->usb_buf[1] = 0;
  309. reg_w(gspca_dev, 2);
  310. }
  311. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  312. u8 *data, /* isoc packet */
  313. int len) /* iso packet length */
  314. {
  315. struct sd *sd = (struct sd *) gspca_dev;
  316. int p;
  317. if (len < 6) {
  318. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  319. return;
  320. }
  321. for (p = 0; p < len - 6; p++) {
  322. if (data[0 + p] == 0xff
  323. && data[1 + p] == 0xff
  324. && data[2 + p] == 0x00
  325. && data[3 + p] == 0xff
  326. && data[4 + p] == 0x96) {
  327. if (data[5 + p] == 0x64
  328. || data[5 + p] == 0x65
  329. || data[5 + p] == 0x66
  330. || data[5 + p] == 0x67) {
  331. PDEBUG(D_PACK, "sof offset: %d len: %d",
  332. p, len);
  333. gspca_frame_add(gspca_dev, LAST_PACKET,
  334. data, p);
  335. /* put the JPEG header */
  336. gspca_frame_add(gspca_dev, FIRST_PACKET,
  337. sd->jpeg_hdr, JPEG_HDR_SZ);
  338. data += p + 16;
  339. len -= p + 16;
  340. break;
  341. }
  342. }
  343. }
  344. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  345. }
  346. /* sub-driver description */
  347. static const struct sd_desc sd_desc = {
  348. .name = MODULE_NAME,
  349. .config = sd_config,
  350. .init = sd_init,
  351. .init_controls = sd_init_controls,
  352. .start = sd_start,
  353. .stopN = sd_stopN,
  354. .pkt_scan = sd_pkt_scan,
  355. };
  356. /* -- module initialisation -- */
  357. static const struct usb_device_id device_table[] = {
  358. {USB_DEVICE(0x093a, 0x050f)},
  359. {}
  360. };
  361. MODULE_DEVICE_TABLE(usb, device_table);
  362. /* -- device connect -- */
  363. static int sd_probe(struct usb_interface *intf,
  364. const struct usb_device_id *id)
  365. {
  366. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  367. THIS_MODULE);
  368. }
  369. static struct usb_driver sd_driver = {
  370. .name = MODULE_NAME,
  371. .id_table = device_table,
  372. .probe = sd_probe,
  373. .disconnect = gspca_disconnect,
  374. #ifdef CONFIG_PM
  375. .suspend = gspca_suspend,
  376. .resume = gspca_resume,
  377. .reset_resume = gspca_resume,
  378. #endif
  379. };
  380. module_usb_driver(sd_driver);