mars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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
  195. * auto dark-gain */
  196. data[4] = 0; /* reg 97, set fixed dark level */
  197. data[5] = 0; /* reg 98, don't care */
  198. } else { /* auto dark-gain */
  199. data[1] = 0; /* reg 94, Y Gain (auto) */
  200. data[2] = 0; /* reg 95, UV Gain (1.75) */
  201. data[3] = 0x78; /* reg 96, Y Gain/UV Gain/disable
  202. * auto dark-gain */
  203. switch (gspca_dev->width) {
  204. /* case 1280: */
  205. /* data[4] = 154;
  206. * reg 97, %3 shadow point (unit: 256 pixel) */
  207. /* data[5] = 51;
  208. * reg 98, %1 highlight point
  209. * (uint: 256 pixel) */
  210. /* break; */
  211. default:
  212. /* case 640: */
  213. data[4] = 36; /* reg 97, %3 shadow point
  214. * (unit: 256 pixel) */
  215. data[5] = 12; /* reg 98, %1 highlight point
  216. * (uint: 256 pixel) */
  217. break;
  218. case 320:
  219. data[4] = 9; /* reg 97, %3 shadow point
  220. * (unit: 256 pixel) */
  221. data[5] = 3; /* reg 98, %1 highlight point
  222. * (uint: 256 pixel) */
  223. break;
  224. }
  225. }
  226. /* auto dark-gain */
  227. data[0] = 0x5e; /* address */
  228. err_code = pcam_reg_write(dev, data[0], data, 0x06);
  229. if (err_code < 0)
  230. return;
  231. data[0] = 0x67;
  232. data[1] = 0x13; /* reg 103, first pixel B, disable sharpness */
  233. err_code = pcam_reg_write(dev, data[0], data, 0x02);
  234. if (err_code < 0)
  235. return;
  236. /*
  237. * initialize the value of MI sensor...
  238. */
  239. MI_buf[REG_HW_MI_1] = 0x000a;
  240. MI_buf[REG_HW_MI_2] = 0x000c;
  241. MI_buf[REG_HW_MI_3] = 0x0405;
  242. MI_buf[REG_HW_MI_4] = 0x0507;
  243. /* mi_Attr_Reg_[REG_HW_MI_5] = 0x01ff;//13 */
  244. MI_buf[REG_HW_MI_5] = 0x0013; /* 13 */
  245. MI_buf[REG_HW_MI_6] = 0x001f; /* vertical blanking */
  246. /* mi_Attr_Reg_[REG_HW_MI_6] = 0x0400; // vertical blanking */
  247. MI_buf[REG_HW_MI_7] = 0x0002;
  248. /* mi_Attr_Reg_[REG_HW_MI_9] = 0x015f; */
  249. /* mi_Attr_Reg_[REG_HW_MI_9] = 0x030f; */
  250. MI_buf[REG_HW_MI_9] = 0x0374;
  251. MI_buf[REG_HW_MI_B] = 0x0000;
  252. MI_buf[REG_HW_MI_C] = 0x0000;
  253. MI_buf[REG_HW_MI_D] = 0x0000;
  254. MI_buf[REG_HW_MI_1E] = 0x8000;
  255. /* mi_Attr_Reg_[REG_HW_MI_20] = 0x1104; */
  256. MI_buf[REG_HW_MI_20] = 0x1104; /* 0x111c; */
  257. MI_buf[REG_HW_MI_2B] = 0x0008;
  258. /* mi_Attr_Reg_[REG_HW_MI_2C] = 0x000f; */
  259. MI_buf[REG_HW_MI_2C] = 0x001f; /* lita suggest */
  260. MI_buf[REG_HW_MI_2D] = 0x0008;
  261. MI_buf[REG_HW_MI_2E] = 0x0008;
  262. MI_buf[REG_HW_MI_35] = 0x0051;
  263. MI_buf[REG_HW_MI_5F] = 0x0904; /* fail to write */
  264. MI_buf[REG_HW_MI_60] = 0x0000;
  265. MI_buf[REG_HW_MI_61] = 0x0000;
  266. MI_buf[REG_HW_MI_62] = 0x0498;
  267. MI_buf[REG_HW_MI_63] = 0x0000;
  268. MI_buf[REG_HW_MI_64] = 0x0000;
  269. MI_buf[REG_HW_MI_F1] = 0x0001;
  270. /* changing while setting up the different value of dx/dy */
  271. if (gspca_dev->width != 1280) {
  272. MI_buf[0x01] = 0x010a;
  273. MI_buf[0x02] = 0x014c;
  274. MI_buf[0x03] = 0x01e5;
  275. MI_buf[0x04] = 0x0287;
  276. }
  277. MI_buf[0x20] = 0x1104;
  278. MISensor_BulkWrite(dev, MI_buf + 1, 1);
  279. MISensor_BulkWrite(dev, MI_buf + 2, 2);
  280. MISensor_BulkWrite(dev, MI_buf + 3, 3);
  281. MISensor_BulkWrite(dev, MI_buf + 4, 4);
  282. MISensor_BulkWrite(dev, MI_buf + 5, 5);
  283. MISensor_BulkWrite(dev, MI_buf + 6, 6);
  284. MISensor_BulkWrite(dev, MI_buf + 7, 7);
  285. MISensor_BulkWrite(dev, MI_buf + 9, 9);
  286. MISensor_BulkWrite(dev, MI_buf + 0x0b, 0x0b);
  287. MISensor_BulkWrite(dev, MI_buf + 0x0c, 0x0c);
  288. MISensor_BulkWrite(dev, MI_buf + 0x0d, 0x0d);
  289. MISensor_BulkWrite(dev, MI_buf + 0x1e, 0x1e);
  290. MISensor_BulkWrite(dev, MI_buf + 0x20, 0x20);
  291. MISensor_BulkWrite(dev, MI_buf + 0x2b, 0x2b);
  292. MISensor_BulkWrite(dev, MI_buf + 0x2c, 0x2c);
  293. MISensor_BulkWrite(dev, MI_buf + 0x2d, 0x2d);
  294. MISensor_BulkWrite(dev, MI_buf + 0x2e, 0x2e);
  295. MISensor_BulkWrite(dev, MI_buf + 0x35, 0x35);
  296. MISensor_BulkWrite(dev, MI_buf + 0x5f, 0x5f);
  297. MISensor_BulkWrite(dev, MI_buf + 0x60, 0x60);
  298. MISensor_BulkWrite(dev, MI_buf + 0x61, 0x61);
  299. MISensor_BulkWrite(dev, MI_buf + 0x62, 0x62);
  300. MISensor_BulkWrite(dev, MI_buf + 0x63, 0x63);
  301. MISensor_BulkWrite(dev, MI_buf + 0x64, 0x64);
  302. MISensor_BulkWrite(dev, MI_buf + 0xf1, 0xf1);
  303. intpipe = usb_sndintpipe(dev, 0);
  304. err_code = usb_clear_halt(dev, intpipe);
  305. data[0] = 0x00;
  306. data[1] = 0x4d; /* ISOC transfering enable... */
  307. pcam_reg_write(dev, data[0], data, 0x02);
  308. }
  309. static void sd_stopN(struct gspca_dev *gspca_dev)
  310. {
  311. int result;
  312. __u8 data[2];
  313. data[0] = 1;
  314. data[1] = 0;
  315. result = pcam_reg_write(gspca_dev->dev, data[0], data, 2);
  316. if (result < 0)
  317. PDEBUG(D_ERR, "Camera Stop failed");
  318. }
  319. static void sd_stop0(struct gspca_dev *gspca_dev)
  320. {
  321. }
  322. static void sd_close(struct gspca_dev *gspca_dev)
  323. {
  324. }
  325. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  326. struct gspca_frame *frame, /* target */
  327. unsigned char *data, /* isoc packet */
  328. int len) /* iso packet length */
  329. {
  330. struct sd *sd = (struct sd *) gspca_dev;
  331. int p;
  332. if (len < 6) {
  333. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  334. return;
  335. }
  336. for (p = 0; p < len - 6; p++) {
  337. if (data[0 + p] == 0xff
  338. && data[1 + p] == 0xff
  339. && data[2 + p] == 0x00
  340. && data[3 + p] == 0xff
  341. && data[4 + p] == 0x96) {
  342. if (data[5 + p] == 0x64
  343. || data[5 + p] == 0x65
  344. || data[5 + p] == 0x66
  345. || data[5 + p] == 0x67) {
  346. PDEBUG(D_PACK, "sof offset: %d leng: %d",
  347. p, len);
  348. frame = gspca_frame_add(gspca_dev, LAST_PACKET,
  349. frame, data, 0);
  350. /* put the JPEG header */
  351. jpeg_put_header(gspca_dev, frame,
  352. sd->qindex, 0x21);
  353. data += 16;
  354. len -= 16;
  355. break;
  356. }
  357. }
  358. }
  359. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  360. }
  361. /* sub-driver description */
  362. static struct sd_desc sd_desc = {
  363. .name = MODULE_NAME,
  364. .ctrls = sd_ctrls,
  365. .nctrls = ARRAY_SIZE(sd_ctrls),
  366. .config = sd_config,
  367. .open = sd_open,
  368. .start = sd_start,
  369. .stopN = sd_stopN,
  370. .stop0 = sd_stop0,
  371. .close = sd_close,
  372. .pkt_scan = sd_pkt_scan,
  373. };
  374. /* -- module initialisation -- */
  375. #define DVNM(name) .driver_info = (kernel_ulong_t) name
  376. static __devinitdata struct usb_device_id device_table[] = {
  377. {USB_DEVICE(0x093a, 0x050f), DVNM("Mars-Semi Pc-Camera")},
  378. {}
  379. };
  380. MODULE_DEVICE_TABLE(usb, device_table);
  381. /* -- device connect -- */
  382. static int sd_probe(struct usb_interface *intf,
  383. const struct usb_device_id *id)
  384. {
  385. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  386. THIS_MODULE);
  387. }
  388. static struct usb_driver sd_driver = {
  389. .name = MODULE_NAME,
  390. .id_table = device_table,
  391. .probe = sd_probe,
  392. .disconnect = gspca_disconnect,
  393. };
  394. /* -- module insert / remove -- */
  395. static int __init sd_mod_init(void)
  396. {
  397. if (usb_register(&sd_driver) < 0)
  398. return -1;
  399. PDEBUG(D_PROBE, "v%s registered", version);
  400. return 0;
  401. }
  402. static void __exit sd_mod_exit(void)
  403. {
  404. usb_deregister(&sd_driver);
  405. PDEBUG(D_PROBE, "deregistered");
  406. }
  407. module_init(sd_mod_init);
  408. module_exit(sd_mod_exit);