mr97310a.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Mars MR97310A library
  3. *
  4. * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
  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 "mr97310a"
  21. #include "gspca.h"
  22. MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>");
  23. MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
  24. MODULE_LICENSE("GPL");
  25. /* specific webcam descriptor */
  26. struct sd {
  27. struct gspca_dev gspca_dev; /* !! must be the first item */
  28. u8 sof_read;
  29. u8 header_read;
  30. };
  31. /* V4L2 controls supported by the driver */
  32. static struct ctrl sd_ctrls[] = {
  33. };
  34. static const struct v4l2_pix_format vga_mode[] = {
  35. {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  36. .bytesperline = 160,
  37. .sizeimage = 160 * 120,
  38. .colorspace = V4L2_COLORSPACE_SRGB,
  39. .priv = 4},
  40. {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  41. .bytesperline = 176,
  42. .sizeimage = 176 * 144,
  43. .colorspace = V4L2_COLORSPACE_SRGB,
  44. .priv = 3},
  45. {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  46. .bytesperline = 320,
  47. .sizeimage = 320 * 240,
  48. .colorspace = V4L2_COLORSPACE_SRGB,
  49. .priv = 2},
  50. {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  51. .bytesperline = 352,
  52. .sizeimage = 352 * 288,
  53. .colorspace = V4L2_COLORSPACE_SRGB,
  54. .priv = 1},
  55. {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  56. .bytesperline = 640,
  57. .sizeimage = 640 * 480,
  58. .colorspace = V4L2_COLORSPACE_SRGB,
  59. .priv = 0},
  60. };
  61. /* the bytes to write are in gspca_dev->usb_buf */
  62. static int reg_w(struct gspca_dev *gspca_dev, int len)
  63. {
  64. int rc;
  65. rc = usb_bulk_msg(gspca_dev->dev,
  66. usb_sndbulkpipe(gspca_dev->dev, 4),
  67. gspca_dev->usb_buf, len, 0, 500);
  68. if (rc < 0)
  69. PDEBUG(D_ERR, "reg write [%02x] error %d",
  70. gspca_dev->usb_buf[0], rc);
  71. return rc;
  72. }
  73. /* this function is called at probe time */
  74. static int sd_config(struct gspca_dev *gspca_dev,
  75. const struct usb_device_id *id)
  76. {
  77. struct cam *cam;
  78. cam = &gspca_dev->cam;
  79. cam->cam_mode = vga_mode;
  80. cam->nmodes = ARRAY_SIZE(vga_mode);
  81. return 0;
  82. }
  83. /* this function is called at probe and resume time */
  84. static int sd_init(struct gspca_dev *gspca_dev)
  85. {
  86. return 0;
  87. }
  88. static int sd_start(struct gspca_dev *gspca_dev)
  89. {
  90. struct sd *sd = (struct sd *) gspca_dev;
  91. __u8 *data = gspca_dev->usb_buf;
  92. int err_code;
  93. sd->sof_read = 0;
  94. /* Note: register descriptions guessed from MR97113A driver */
  95. data[0] = 0x01;
  96. data[1] = 0x01;
  97. err_code = reg_w(gspca_dev, 2);
  98. if (err_code < 0)
  99. return err_code;
  100. data[0] = 0x00;
  101. data[1] = 0x0d;
  102. data[2] = 0x01;
  103. data[5] = 0x2b;
  104. data[7] = 0x00;
  105. data[9] = 0x50; /* reg 8, no scale down */
  106. data[10] = 0xc0;
  107. switch (gspca_dev->width) {
  108. case 160:
  109. data[9] |= 0x0c; /* reg 8, 4:1 scale down */
  110. /* fall thru */
  111. case 320:
  112. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  113. /* fall thru */
  114. case 640:
  115. default:
  116. data[3] = 0x50; /* reg 2, H size */
  117. data[4] = 0x78; /* reg 3, V size */
  118. data[6] = 0x04; /* reg 5, H start */
  119. data[8] = 0x03; /* reg 7, V start */
  120. break;
  121. case 176:
  122. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  123. /* fall thru */
  124. case 352:
  125. data[3] = 0x2c; /* reg 2, H size */
  126. data[4] = 0x48; /* reg 3, V size */
  127. data[6] = 0x94; /* reg 5, H start */
  128. data[8] = 0x63; /* reg 7, V start */
  129. break;
  130. }
  131. err_code = reg_w(gspca_dev, 11);
  132. if (err_code < 0)
  133. return err_code;
  134. data[0] = 0x0a;
  135. data[1] = 0x80;
  136. err_code = reg_w(gspca_dev, 2);
  137. if (err_code < 0)
  138. return err_code;
  139. data[0] = 0x14;
  140. data[1] = 0x0a;
  141. err_code = reg_w(gspca_dev, 2);
  142. if (err_code < 0)
  143. return err_code;
  144. data[0] = 0x1b;
  145. data[1] = 0x00;
  146. err_code = reg_w(gspca_dev, 2);
  147. if (err_code < 0)
  148. return err_code;
  149. data[0] = 0x15;
  150. data[1] = 0x16;
  151. err_code = reg_w(gspca_dev, 2);
  152. if (err_code < 0)
  153. return err_code;
  154. data[0] = 0x16;
  155. data[1] = 0x10;
  156. err_code = reg_w(gspca_dev, 2);
  157. if (err_code < 0)
  158. return err_code;
  159. data[0] = 0x17;
  160. data[1] = 0x3a;
  161. err_code = reg_w(gspca_dev, 2);
  162. if (err_code < 0)
  163. return err_code;
  164. data[0] = 0x18;
  165. data[1] = 0x68;
  166. err_code = reg_w(gspca_dev, 2);
  167. if (err_code < 0)
  168. return err_code;
  169. data[0] = 0x1f;
  170. data[1] = 0x00;
  171. data[2] = 0x02;
  172. data[3] = 0x06;
  173. data[4] = 0x59;
  174. data[5] = 0x0c;
  175. data[6] = 0x16;
  176. data[7] = 0x00;
  177. data[8] = 0x07;
  178. data[9] = 0x00;
  179. data[10] = 0x01;
  180. err_code = reg_w(gspca_dev, 11);
  181. if (err_code < 0)
  182. return err_code;
  183. data[0] = 0x1f;
  184. data[1] = 0x04;
  185. data[2] = 0x11;
  186. data[3] = 0x01;
  187. err_code = reg_w(gspca_dev, 4);
  188. if (err_code < 0)
  189. return err_code;
  190. data[0] = 0x1f;
  191. data[1] = 0x00;
  192. data[2] = 0x0a;
  193. data[3] = 0x00;
  194. data[4] = 0x01;
  195. data[5] = 0x00;
  196. data[6] = 0x00;
  197. data[7] = 0x01;
  198. data[8] = 0x00;
  199. data[9] = 0x0a;
  200. err_code = reg_w(gspca_dev, 10);
  201. if (err_code < 0)
  202. return err_code;
  203. data[0] = 0x1f;
  204. data[1] = 0x04;
  205. data[2] = 0x11;
  206. data[3] = 0x01;
  207. err_code = reg_w(gspca_dev, 4);
  208. if (err_code < 0)
  209. return err_code;
  210. data[0] = 0x1f;
  211. data[1] = 0x00;
  212. data[2] = 0x12;
  213. data[3] = 0x00;
  214. data[4] = 0x63;
  215. data[5] = 0x00;
  216. data[6] = 0x70;
  217. data[7] = 0x00;
  218. data[8] = 0x00;
  219. err_code = reg_w(gspca_dev, 9);
  220. if (err_code < 0)
  221. return err_code;
  222. data[0] = 0x1f;
  223. data[1] = 0x04;
  224. data[2] = 0x11;
  225. data[3] = 0x01;
  226. err_code = reg_w(gspca_dev, 4);
  227. if (err_code < 0)
  228. return err_code;
  229. data[0] = 0x00;
  230. data[1] = 0x4d; /* ISOC transfering enable... */
  231. err_code = reg_w(gspca_dev, 2);
  232. return err_code;
  233. }
  234. static void sd_stopN(struct gspca_dev *gspca_dev)
  235. {
  236. int result;
  237. gspca_dev->usb_buf[0] = 1;
  238. gspca_dev->usb_buf[1] = 0;
  239. result = reg_w(gspca_dev, 2);
  240. if (result < 0)
  241. PDEBUG(D_ERR, "Camera Stop failed");
  242. }
  243. /* Include pac common sof detection functions */
  244. #include "pac_common.h"
  245. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  246. struct gspca_frame *frame, /* target */
  247. __u8 *data, /* isoc packet */
  248. int len) /* iso packet length */
  249. {
  250. struct sd *sd = (struct sd *) gspca_dev;
  251. unsigned char *sof;
  252. sof = pac_find_sof(gspca_dev, data, len);
  253. if (sof) {
  254. int n;
  255. /* finish decoding current frame */
  256. n = sof - data;
  257. if (n > sizeof pac_sof_marker)
  258. n -= sizeof pac_sof_marker;
  259. else
  260. n = 0;
  261. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  262. data, n);
  263. sd->header_read = 0;
  264. gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
  265. len -= sof - data;
  266. data = sof;
  267. }
  268. if (sd->header_read < 7) {
  269. int needed;
  270. /* skip the rest of the header */
  271. needed = 7 - sd->header_read;
  272. if (len <= needed) {
  273. sd->header_read += len;
  274. return;
  275. }
  276. data += needed;
  277. len -= needed;
  278. sd->header_read = 7;
  279. }
  280. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  281. }
  282. /* sub-driver description */
  283. static const struct sd_desc sd_desc = {
  284. .name = MODULE_NAME,
  285. .ctrls = sd_ctrls,
  286. .nctrls = ARRAY_SIZE(sd_ctrls),
  287. .config = sd_config,
  288. .init = sd_init,
  289. .start = sd_start,
  290. .stopN = sd_stopN,
  291. .pkt_scan = sd_pkt_scan,
  292. };
  293. /* -- module initialisation -- */
  294. static const __devinitdata struct usb_device_id device_table[] = {
  295. {USB_DEVICE(0x08ca, 0x0111)},
  296. {}
  297. };
  298. MODULE_DEVICE_TABLE(usb, device_table);
  299. /* -- device connect -- */
  300. static int sd_probe(struct usb_interface *intf,
  301. const struct usb_device_id *id)
  302. {
  303. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  304. THIS_MODULE);
  305. }
  306. static struct usb_driver sd_driver = {
  307. .name = MODULE_NAME,
  308. .id_table = device_table,
  309. .probe = sd_probe,
  310. .disconnect = gspca_disconnect,
  311. #ifdef CONFIG_PM
  312. .suspend = gspca_suspend,
  313. .resume = gspca_resume,
  314. #endif
  315. };
  316. /* -- module insert / remove -- */
  317. static int __init sd_mod_init(void)
  318. {
  319. if (usb_register(&sd_driver) < 0)
  320. return -1;
  321. PDEBUG(D_PROBE, "registered");
  322. return 0;
  323. }
  324. static void __exit sd_mod_exit(void)
  325. {
  326. usb_deregister(&sd_driver);
  327. PDEBUG(D_PROBE, "deregistered");
  328. }
  329. module_init(sd_mod_init);
  330. module_exit(sd_mod_exit);