mars.c 12 KB

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