mars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 MODULE_NAME "mars"
  22. #include "gspca.h"
  23. #include "jpeg.h"
  24. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 0)
  25. static const char version[] = "2.1.0";
  26. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  27. MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
  28. MODULE_LICENSE("GPL");
  29. /* specific webcam descriptor */
  30. struct sd {
  31. struct gspca_dev gspca_dev; /* !! must be the first item */
  32. char qindex;
  33. };
  34. /* V4L2 controls supported by the driver */
  35. static struct ctrl sd_ctrls[] = {
  36. };
  37. static struct cam_mode vga_mode[] = {
  38. {V4L2_PIX_FMT_JPEG, 320, 240, 2},
  39. {V4L2_PIX_FMT_JPEG, 640, 480, 1},
  40. };
  41. /* MI Register table //elvis */
  42. enum {
  43. REG_HW_MI_0,
  44. REG_HW_MI_1,
  45. REG_HW_MI_2,
  46. REG_HW_MI_3,
  47. REG_HW_MI_4,
  48. REG_HW_MI_5,
  49. REG_HW_MI_6,
  50. REG_HW_MI_7,
  51. REG_HW_MI_9 = 0x09,
  52. REG_HW_MI_B = 0x0B,
  53. REG_HW_MI_C,
  54. REG_HW_MI_D,
  55. REG_HW_MI_1E = 0x1E,
  56. REG_HW_MI_20 = 0x20,
  57. REG_HW_MI_2B = 0x2B,
  58. REG_HW_MI_2C,
  59. REG_HW_MI_2D,
  60. REG_HW_MI_2E,
  61. REG_HW_MI_35 = 0x35,
  62. REG_HW_MI_5F = 0x5f,
  63. REG_HW_MI_60,
  64. REG_HW_MI_61,
  65. REG_HW_MI_62,
  66. REG_HW_MI_63,
  67. REG_HW_MI_64,
  68. REG_HW_MI_F1 = 0xf1,
  69. ATTR_TOTAL_MI_REG = 242
  70. };
  71. static int pcam_reg_write(struct usb_device *dev,
  72. __u16 index, unsigned char *value, int length)
  73. {
  74. int rc;
  75. rc = usb_control_msg(dev,
  76. usb_sndbulkpipe(dev, 4),
  77. 0x12,
  78. /* ?? 0xc8 = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_??? !? */
  79. 0xc8,
  80. 0, /* value */
  81. index, value, length, 500);
  82. PDEBUG(D_USBO, "reg write: 0x%02X , result = 0x%x", index, rc);
  83. if (rc < 0)
  84. PDEBUG(D_ERR, "reg write: error %d", rc);
  85. return rc;
  86. }
  87. static void MISensor_BulkWrite(struct usb_device *dev, unsigned short *pch,
  88. char Address)
  89. {
  90. int result;
  91. unsigned char data[6];
  92. data[0] = 0x1f;
  93. data[1] = 0;
  94. data[2] = Address;
  95. data[3] = *pch >> 8; /* high byte */
  96. data[4] = *pch; /* low byte */
  97. data[5] = 0;
  98. result = usb_control_msg(dev,
  99. usb_sndbulkpipe(dev, 4),
  100. 0x12,
  101. /* ?? 0xc8 = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_??? !? */
  102. 0xc8,
  103. 0, /* value */
  104. Address, /* index */
  105. data, 5, 500);
  106. PDEBUG(D_USBO, "bulk write 0x%02x = 0x%04x", Address, *pch);
  107. if (result < 0)
  108. PDEBUG(D_ERR, "reg write: error %d", result);
  109. }
  110. /* this function is called at probe time */
  111. static int sd_config(struct gspca_dev *gspca_dev,
  112. const struct usb_device_id *id)
  113. {
  114. struct sd *sd = (struct sd *) gspca_dev;
  115. struct cam *cam;
  116. cam = &gspca_dev->cam;
  117. cam->dev_name = (char *) id->driver_info;
  118. cam->epaddr = 0x01;
  119. cam->cam_mode = vga_mode;
  120. cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
  121. sd->qindex = 1; /* set the quantization table */
  122. return 0;
  123. }
  124. /* this function is called at open time */
  125. static int sd_open(struct gspca_dev *gspca_dev)
  126. {
  127. return 0;
  128. }
  129. static void sd_start(struct gspca_dev *gspca_dev)
  130. {
  131. struct usb_device *dev = gspca_dev->dev;
  132. int err_code;
  133. __u8 data[12];
  134. __u16 MI_buf[242];
  135. int h_size, v_size;
  136. int intpipe;
  137. /* struct usb_device *dev = pcam->dev; */
  138. memset(data, 0, sizeof data);
  139. memset(MI_buf, 0, sizeof MI_buf);
  140. PDEBUG(D_STREAM, "camera start, iface %d, alt 8", gspca_dev->iface);
  141. if (usb_set_interface(dev, gspca_dev->iface, 8) < 0) {
  142. PDEBUG(D_ERR|D_STREAM, "Set packet size: set interface error");
  143. return;
  144. }
  145. data[0] = 0x01; /* address */
  146. data[1] = 0x01;
  147. err_code = pcam_reg_write(dev, data[0], data, 0x02);
  148. if (err_code < 0)
  149. return;
  150. /*
  151. Initialize the MR97113 chip register
  152. */
  153. data[0] = 0x00; /* address */
  154. data[1] = 0x0c | 0x01; /* reg 0 */
  155. data[2] = 0x01; /* reg 1 */
  156. h_size = gspca_dev->width;
  157. v_size = gspca_dev->height;
  158. data[3] = h_size / 8; /* h_size , reg 2 */
  159. data[4] = v_size / 8; /* v_size , reg 3 */
  160. data[5] = 0x30; /* reg 4, MI, PAS5101 :
  161. * 0x30 for 24mhz , 0x28 for 12mhz */
  162. data[6] = 4; /* reg 5, H start */
  163. data[7] = 0xc0; /* reg 6, gamma 1.5 */
  164. data[8] = 3; /* reg 7, V start */
  165. /* if(h_size == 320 ) */
  166. /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
  167. /* else */
  168. data[9] = 0x52; /* reg 8, 24MHz, no scale down */
  169. data[10] = 0x5d; /* reg 9, I2C device address
  170. * [for PAS5101 (0x40)] [for MI (0x5d)] */
  171. err_code = pcam_reg_write(dev, data[0], data, 0x0b);
  172. if (err_code < 0)
  173. return;
  174. data[0] = 0x23; /* address */
  175. data[1] = 0x09; /* reg 35, append frame header */
  176. err_code = pcam_reg_write(dev, data[0], data, 0x02);
  177. if (err_code < 0) {
  178. PDEBUG(D_ERR, "Register write failed");
  179. return;
  180. }
  181. data[0] = 0x3C; /* address */
  182. /* if (pcam->width == 1280) */
  183. /* data[1] = 200; * reg 60, pc-cam frame size
  184. * (unit: 4KB) 800KB */
  185. /* else */
  186. data[1] = 50; /* 50 reg 60, pc-cam frame size
  187. * (unit: 4KB) 200KB */
  188. err_code = pcam_reg_write(dev, data[0], data, 0x02);
  189. if (err_code < 0)
  190. return;
  191. if (0) { /* fixed dark-gain */
  192. data[1] = 0; /* reg 94, Y Gain (1.75) */
  193. data[2] = 0; /* reg 95, UV Gain (1.75) */
  194. data[3] = 0x3f; /* reg 96, Y Gain/UV Gain/disable auto dark-gain */
  195. data[4] = 0; /* reg 97, set fixed dark level */
  196. data[5] = 0; /* reg 98, don't care */
  197. } else { /* auto dark-gain */
  198. data[1] = 0; /* reg 94, Y Gain (auto) */
  199. data[2] = 0; /* reg 95, UV Gain (1.75) */
  200. data[3] = 0x78; /* reg 96, Y Gain/UV Gain/disable auto dark-gain */
  201. switch (gspca_dev->width) {
  202. /* case 1280: */
  203. /* data[4] = 154;
  204. * reg 97, %3 shadow point (unit: 256 pixel) */
  205. /* data[5] = 51;
  206. * reg 98, %1 highlight point
  207. * (uint: 256 pixel) */
  208. /* break; */
  209. default:
  210. /* case 640: */
  211. data[4] = 36; /* reg 97, %3 shadow point
  212. * (unit: 256 pixel) */
  213. data[5] = 12; /* reg 98, %1 highlight point
  214. * (uint: 256 pixel) */
  215. break;
  216. case 320:
  217. data[4] = 9; /* reg 97, %3 shadow point
  218. * (unit: 256 pixel) */
  219. data[5] = 3; /* reg 98, %1 highlight point
  220. * (uint: 256 pixel) */
  221. break;
  222. }
  223. }
  224. /* auto dark-gain */
  225. data[0] = 0x5e; /* address */
  226. err_code = pcam_reg_write(dev, data[0], data, 0x06);
  227. if (err_code < 0)
  228. return;
  229. data[0] = 0x67;
  230. data[1] = 0x13; /* reg 103, first pixel B, disable sharpness */
  231. err_code = pcam_reg_write(dev, data[0], data, 0x02);
  232. if (err_code < 0)
  233. return;
  234. /*
  235. * initialize the value of MI sensor...
  236. */
  237. MI_buf[REG_HW_MI_1] = 0x000a;
  238. MI_buf[REG_HW_MI_2] = 0x000c;
  239. MI_buf[REG_HW_MI_3] = 0x0405;
  240. MI_buf[REG_HW_MI_4] = 0x0507;
  241. /* mi_Attr_Reg_[REG_HW_MI_5] = 0x01ff;//13 */
  242. MI_buf[REG_HW_MI_5] = 0x0013; /* 13 */
  243. MI_buf[REG_HW_MI_6] = 0x001f; /* vertical blanking */
  244. /* mi_Attr_Reg_[REG_HW_MI_6] = 0x0400; // vertical blanking */
  245. MI_buf[REG_HW_MI_7] = 0x0002;
  246. /* mi_Attr_Reg_[REG_HW_MI_9] = 0x015f; */
  247. /* mi_Attr_Reg_[REG_HW_MI_9] = 0x030f; */
  248. MI_buf[REG_HW_MI_9] = 0x0374;
  249. MI_buf[REG_HW_MI_B] = 0x0000;
  250. MI_buf[REG_HW_MI_C] = 0x0000;
  251. MI_buf[REG_HW_MI_D] = 0x0000;
  252. MI_buf[REG_HW_MI_1E] = 0x8000;
  253. /* mi_Attr_Reg_[REG_HW_MI_20] = 0x1104; */
  254. MI_buf[REG_HW_MI_20] = 0x1104; /* 0x111c; */
  255. MI_buf[REG_HW_MI_2B] = 0x0008;
  256. /* mi_Attr_Reg_[REG_HW_MI_2C] = 0x000f; */
  257. MI_buf[REG_HW_MI_2C] = 0x001f; /* lita suggest */
  258. MI_buf[REG_HW_MI_2D] = 0x0008;
  259. MI_buf[REG_HW_MI_2E] = 0x0008;
  260. MI_buf[REG_HW_MI_35] = 0x0051;
  261. MI_buf[REG_HW_MI_5F] = 0x0904; /* fail to write */
  262. MI_buf[REG_HW_MI_60] = 0x0000;
  263. MI_buf[REG_HW_MI_61] = 0x0000;
  264. MI_buf[REG_HW_MI_62] = 0x0498;
  265. MI_buf[REG_HW_MI_63] = 0x0000;
  266. MI_buf[REG_HW_MI_64] = 0x0000;
  267. MI_buf[REG_HW_MI_F1] = 0x0001;
  268. /* changing while setting up the different value of dx/dy */
  269. if (gspca_dev->width != 1280) {
  270. MI_buf[0x01] = 0x010a;
  271. MI_buf[0x02] = 0x014c;
  272. MI_buf[0x03] = 0x01e5;
  273. MI_buf[0x04] = 0x0287;
  274. }
  275. MI_buf[0x20] = 0x1104;
  276. MISensor_BulkWrite(dev, MI_buf + 1, 1);
  277. MISensor_BulkWrite(dev, MI_buf + 2, 2);
  278. MISensor_BulkWrite(dev, MI_buf + 3, 3);
  279. MISensor_BulkWrite(dev, MI_buf + 4, 4);
  280. MISensor_BulkWrite(dev, MI_buf + 5, 5);
  281. MISensor_BulkWrite(dev, MI_buf + 6, 6);
  282. MISensor_BulkWrite(dev, MI_buf + 7, 7);
  283. MISensor_BulkWrite(dev, MI_buf + 9, 9);
  284. MISensor_BulkWrite(dev, MI_buf + 0x0b, 0x0b);
  285. MISensor_BulkWrite(dev, MI_buf + 0x0c, 0x0c);
  286. MISensor_BulkWrite(dev, MI_buf + 0x0d, 0x0d);
  287. MISensor_BulkWrite(dev, MI_buf + 0x1e, 0x1e);
  288. MISensor_BulkWrite(dev, MI_buf + 0x20, 0x20);
  289. MISensor_BulkWrite(dev, MI_buf + 0x2b, 0x2b);
  290. MISensor_BulkWrite(dev, MI_buf + 0x2c, 0x2c);
  291. MISensor_BulkWrite(dev, MI_buf + 0x2d, 0x2d);
  292. MISensor_BulkWrite(dev, MI_buf + 0x2e, 0x2e);
  293. MISensor_BulkWrite(dev, MI_buf + 0x35, 0x35);
  294. MISensor_BulkWrite(dev, MI_buf + 0x5f, 0x5f);
  295. MISensor_BulkWrite(dev, MI_buf + 0x60, 0x60);
  296. MISensor_BulkWrite(dev, MI_buf + 0x61, 0x61);
  297. MISensor_BulkWrite(dev, MI_buf + 0x62, 0x62);
  298. MISensor_BulkWrite(dev, MI_buf + 0x63, 0x63);
  299. MISensor_BulkWrite(dev, MI_buf + 0x64, 0x64);
  300. MISensor_BulkWrite(dev, MI_buf + 0xf1, 0xf1);
  301. intpipe = usb_sndintpipe(dev, 0);
  302. err_code = usb_clear_halt(dev, intpipe);
  303. data[0] = 0x00;
  304. data[1] = 0x4d; /* ISOC transfering enable... */
  305. pcam_reg_write(dev, data[0], data, 0x02);
  306. }
  307. static void sd_stopN(struct gspca_dev *gspca_dev)
  308. {
  309. int result;
  310. __u8 data[2];
  311. data[0] = 1;
  312. data[1] = 0;
  313. result = pcam_reg_write(gspca_dev->dev, data[0], data, 2);
  314. if (result < 0)
  315. PDEBUG(D_ERR, "Camera Stop failed");
  316. }
  317. static void sd_stop0(struct gspca_dev *gspca_dev)
  318. {
  319. }
  320. static void sd_close(struct gspca_dev *gspca_dev)
  321. {
  322. }
  323. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  324. struct gspca_frame *frame, /* target */
  325. unsigned char *data, /* isoc packet */
  326. int len) /* iso packet length */
  327. {
  328. struct sd *sd = (struct sd *) gspca_dev;
  329. int p;
  330. if (len < 6) {
  331. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  332. return;
  333. }
  334. for (p = 0; p < len - 6; p++) {
  335. if (data[0 + p] == 0xff
  336. && data[1 + p] == 0xff
  337. && data[2 + p] == 0x00
  338. && data[3 + p] == 0xff
  339. && data[4 + p] == 0x96) {
  340. if (data[5 + p] == 0x64
  341. || data[5 + p] == 0x65
  342. || data[5 + p] == 0x66
  343. || data[5 + p] == 0x67) {
  344. PDEBUG(D_PACK, "sof offset: %d leng: %d",
  345. p, len);
  346. frame = gspca_frame_add(gspca_dev, LAST_PACKET,
  347. frame, data, 0);
  348. /* put the JPEG header */
  349. jpeg_put_header(gspca_dev, frame,
  350. sd->qindex, 0x21);
  351. data += 16;
  352. len -= 16;
  353. break;
  354. }
  355. }
  356. }
  357. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  358. }
  359. /* sub-driver description */
  360. static struct sd_desc sd_desc = {
  361. .name = MODULE_NAME,
  362. .ctrls = sd_ctrls,
  363. .nctrls = ARRAY_SIZE(sd_ctrls),
  364. .config = sd_config,
  365. .open = sd_open,
  366. .start = sd_start,
  367. .stopN = sd_stopN,
  368. .stop0 = sd_stop0,
  369. .close = sd_close,
  370. .pkt_scan = sd_pkt_scan,
  371. };
  372. /* -- module initialisation -- */
  373. #define DVNM(name) .driver_info = (kernel_ulong_t) name
  374. static __devinitdata struct usb_device_id device_table[] = {
  375. {USB_DEVICE(0x093a, 0x050f), DVNM("Mars-Semi Pc-Camera")},
  376. {}
  377. };
  378. MODULE_DEVICE_TABLE(usb, device_table);
  379. /* -- device connect -- */
  380. static int sd_probe(struct usb_interface *intf,
  381. const struct usb_device_id *id)
  382. {
  383. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  384. THIS_MODULE);
  385. }
  386. static struct usb_driver sd_driver = {
  387. .name = MODULE_NAME,
  388. .id_table = device_table,
  389. .probe = sd_probe,
  390. .disconnect = gspca_disconnect,
  391. };
  392. /* -- module insert / remove -- */
  393. static int __init sd_mod_init(void)
  394. {
  395. if (usb_register(&sd_driver) < 0)
  396. return -1;
  397. PDEBUG(D_PROBE, "v%s registered", version);
  398. return 0;
  399. }
  400. static void __exit sd_mod_exit(void)
  401. {
  402. usb_deregister(&sd_driver);
  403. PDEBUG(D_PROBE, "deregistered");
  404. }
  405. module_init(sd_mod_init);
  406. module_exit(sd_mod_exit);