spca1528.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * spca1528 subdriver
  4. *
  5. * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #define MODULE_NAME "spca1528"
  9. #include "gspca.h"
  10. #include "jpeg.h"
  11. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  12. MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
  13. MODULE_LICENSE("GPL");
  14. /* specific webcam descriptor */
  15. struct sd {
  16. struct gspca_dev gspca_dev; /* !! must be the first item */
  17. u8 pkt_seq;
  18. u8 jpeg_hdr[JPEG_HDR_SZ];
  19. };
  20. static const struct v4l2_pix_format vga_mode[] = {
  21. /* (does not work correctly)
  22. {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  23. .bytesperline = 176,
  24. .sizeimage = 176 * 144 * 5 / 8 + 590,
  25. .colorspace = V4L2_COLORSPACE_JPEG,
  26. .priv = 3},
  27. */
  28. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  29. .bytesperline = 320,
  30. .sizeimage = 320 * 240 * 4 / 8 + 590,
  31. .colorspace = V4L2_COLORSPACE_JPEG,
  32. .priv = 2},
  33. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  34. .bytesperline = 640,
  35. .sizeimage = 640 * 480 * 3 / 8 + 590,
  36. .colorspace = V4L2_COLORSPACE_JPEG,
  37. .priv = 1},
  38. };
  39. /* read <len> bytes to gspca usb_buf */
  40. static void reg_r(struct gspca_dev *gspca_dev,
  41. u8 req,
  42. u16 index,
  43. int len)
  44. {
  45. #if USB_BUF_SZ < 64
  46. #error "USB buffer too small"
  47. #endif
  48. struct usb_device *dev = gspca_dev->dev;
  49. int ret;
  50. if (gspca_dev->usb_err < 0)
  51. return;
  52. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  53. req,
  54. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  55. 0x0000, /* value */
  56. index,
  57. gspca_dev->usb_buf, len,
  58. 500);
  59. gspca_dbg(gspca_dev, D_USBI, "GET %02x 0000 %04x %02x\n", req, index,
  60. gspca_dev->usb_buf[0]);
  61. if (ret < 0) {
  62. pr_err("reg_r err %d\n", ret);
  63. gspca_dev->usb_err = ret;
  64. /*
  65. * Make sure the buffer is zeroed to avoid uninitialized
  66. * values.
  67. */
  68. memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
  69. }
  70. }
  71. static void reg_w(struct gspca_dev *gspca_dev,
  72. u8 req,
  73. u16 value,
  74. u16 index)
  75. {
  76. struct usb_device *dev = gspca_dev->dev;
  77. int ret;
  78. if (gspca_dev->usb_err < 0)
  79. return;
  80. gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x\n", req, value, index);
  81. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  82. req,
  83. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  84. value, index,
  85. NULL, 0, 500);
  86. if (ret < 0) {
  87. pr_err("reg_w err %d\n", ret);
  88. gspca_dev->usb_err = ret;
  89. }
  90. }
  91. static void reg_wb(struct gspca_dev *gspca_dev,
  92. u8 req,
  93. u16 value,
  94. u16 index,
  95. u8 byte)
  96. {
  97. struct usb_device *dev = gspca_dev->dev;
  98. int ret;
  99. if (gspca_dev->usb_err < 0)
  100. return;
  101. gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x %02x\n",
  102. req, value, index, byte);
  103. gspca_dev->usb_buf[0] = byte;
  104. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  105. req,
  106. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  107. value, index,
  108. gspca_dev->usb_buf, 1, 500);
  109. if (ret < 0) {
  110. pr_err("reg_w err %d\n", ret);
  111. gspca_dev->usb_err = ret;
  112. }
  113. }
  114. static void wait_status_0(struct gspca_dev *gspca_dev)
  115. {
  116. int i, w;
  117. i = 16;
  118. w = 0;
  119. do {
  120. reg_r(gspca_dev, 0x21, 0x0000, 1);
  121. if (gspca_dev->usb_buf[0] == 0)
  122. return;
  123. w += 15;
  124. msleep(w);
  125. } while (--i > 0);
  126. gspca_err(gspca_dev, "wait_status_0 timeout\n");
  127. gspca_dev->usb_err = -ETIME;
  128. }
  129. static void wait_status_1(struct gspca_dev *gspca_dev)
  130. {
  131. int i;
  132. i = 10;
  133. do {
  134. reg_r(gspca_dev, 0x21, 0x0001, 1);
  135. msleep(10);
  136. if (gspca_dev->usb_buf[0] == 1) {
  137. reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
  138. reg_r(gspca_dev, 0x21, 0x0001, 1);
  139. return;
  140. }
  141. } while (--i > 0);
  142. gspca_err(gspca_dev, "wait_status_1 timeout\n");
  143. gspca_dev->usb_err = -ETIME;
  144. }
  145. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  146. {
  147. reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
  148. }
  149. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  150. {
  151. reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
  152. }
  153. static void sethue(struct gspca_dev *gspca_dev, s32 val)
  154. {
  155. reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
  156. }
  157. static void setcolor(struct gspca_dev *gspca_dev, s32 val)
  158. {
  159. reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
  160. }
  161. static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
  162. {
  163. reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
  164. }
  165. /* this function is called at probe time */
  166. static int sd_config(struct gspca_dev *gspca_dev,
  167. const struct usb_device_id *id)
  168. {
  169. gspca_dev->cam.cam_mode = vga_mode;
  170. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  171. gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
  172. /*fixme: 256 in ms-win traces*/
  173. return 0;
  174. }
  175. /* this function is called at probe and resume time */
  176. static int sd_init(struct gspca_dev *gspca_dev)
  177. {
  178. reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
  179. reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
  180. reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
  181. reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
  182. msleep(8);
  183. reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
  184. reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
  185. reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
  186. reg_r(gspca_dev, 0x20, 0x0000, 1);
  187. reg_r(gspca_dev, 0x20, 0x0000, 5);
  188. reg_r(gspca_dev, 0x23, 0x0000, 64);
  189. gspca_dbg(gspca_dev, D_PROBE, "%s%s\n", &gspca_dev->usb_buf[0x1c],
  190. &gspca_dev->usb_buf[0x30]);
  191. reg_r(gspca_dev, 0x23, 0x0001, 64);
  192. return gspca_dev->usb_err;
  193. }
  194. /* function called at start time before URB creation */
  195. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  196. {
  197. u8 mode;
  198. reg_r(gspca_dev, 0x00, 0x2520, 1);
  199. wait_status_0(gspca_dev);
  200. reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
  201. wait_status_1(gspca_dev);
  202. wait_status_0(gspca_dev);
  203. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  204. reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
  205. reg_r(gspca_dev, 0x25, 0x0004, 1);
  206. reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
  207. reg_r(gspca_dev, 0x27, 0x0000, 1);
  208. /* not useful..
  209. gspca_dev->alt = 4; * use alternate setting 3 */
  210. return gspca_dev->usb_err;
  211. }
  212. /* -- start the camera -- */
  213. static int sd_start(struct gspca_dev *gspca_dev)
  214. {
  215. struct sd *sd = (struct sd *) gspca_dev;
  216. /* initialize the JPEG header */
  217. jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
  218. gspca_dev->pixfmt.width,
  219. 0x22); /* JPEG 411 */
  220. /* the JPEG quality shall be 85% */
  221. jpeg_set_qual(sd->jpeg_hdr, 85);
  222. reg_r(gspca_dev, 0x00, 0x2520, 1);
  223. msleep(8);
  224. /* start the capture */
  225. wait_status_0(gspca_dev);
  226. reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
  227. wait_status_1(gspca_dev);
  228. wait_status_0(gspca_dev);
  229. msleep(200);
  230. sd->pkt_seq = 0;
  231. return gspca_dev->usb_err;
  232. }
  233. static void sd_stopN(struct gspca_dev *gspca_dev)
  234. {
  235. /* stop the capture */
  236. wait_status_0(gspca_dev);
  237. reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
  238. wait_status_1(gspca_dev);
  239. wait_status_0(gspca_dev);
  240. }
  241. /* move a packet adding 0x00 after 0xff */
  242. static void add_packet(struct gspca_dev *gspca_dev,
  243. u8 *data,
  244. int len)
  245. {
  246. int i;
  247. i = 0;
  248. do {
  249. if (data[i] == 0xff) {
  250. gspca_frame_add(gspca_dev, INTER_PACKET,
  251. data, i + 1);
  252. len -= i;
  253. data += i;
  254. *data = 0x00;
  255. i = 0;
  256. }
  257. } while (++i < len);
  258. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  259. }
  260. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  261. u8 *data, /* isoc packet */
  262. int len) /* iso packet length */
  263. {
  264. struct sd *sd = (struct sd *) gspca_dev;
  265. static const u8 ffd9[] = {0xff, 0xd9};
  266. /* image packets start with:
  267. * 02 8n
  268. * with <n> bit:
  269. * 0x01: even (0) / odd (1) image
  270. * 0x02: end of image when set
  271. */
  272. if (len < 3)
  273. return; /* empty packet */
  274. if (*data == 0x02) {
  275. if (data[1] & 0x02) {
  276. sd->pkt_seq = !(data[1] & 1);
  277. add_packet(gspca_dev, data + 2, len - 2);
  278. gspca_frame_add(gspca_dev, LAST_PACKET,
  279. ffd9, 2);
  280. return;
  281. }
  282. if ((data[1] & 1) != sd->pkt_seq)
  283. goto err;
  284. if (gspca_dev->last_packet_type == LAST_PACKET)
  285. gspca_frame_add(gspca_dev, FIRST_PACKET,
  286. sd->jpeg_hdr, JPEG_HDR_SZ);
  287. add_packet(gspca_dev, data + 2, len - 2);
  288. return;
  289. }
  290. err:
  291. gspca_dev->last_packet_type = DISCARD_PACKET;
  292. }
  293. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  294. {
  295. struct gspca_dev *gspca_dev =
  296. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  297. gspca_dev->usb_err = 0;
  298. if (!gspca_dev->streaming)
  299. return 0;
  300. switch (ctrl->id) {
  301. case V4L2_CID_BRIGHTNESS:
  302. setbrightness(gspca_dev, ctrl->val);
  303. break;
  304. case V4L2_CID_CONTRAST:
  305. setcontrast(gspca_dev, ctrl->val);
  306. break;
  307. case V4L2_CID_HUE:
  308. sethue(gspca_dev, ctrl->val);
  309. break;
  310. case V4L2_CID_SATURATION:
  311. setcolor(gspca_dev, ctrl->val);
  312. break;
  313. case V4L2_CID_SHARPNESS:
  314. setsharpness(gspca_dev, ctrl->val);
  315. break;
  316. }
  317. return gspca_dev->usb_err;
  318. }
  319. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  320. .s_ctrl = sd_s_ctrl,
  321. };
  322. static int sd_init_controls(struct gspca_dev *gspca_dev)
  323. {
  324. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  325. gspca_dev->vdev.ctrl_handler = hdl;
  326. v4l2_ctrl_handler_init(hdl, 5);
  327. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  328. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  329. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  330. V4L2_CID_CONTRAST, 0, 8, 1, 1);
  331. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  332. V4L2_CID_HUE, 0, 255, 1, 0);
  333. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  334. V4L2_CID_SATURATION, 0, 8, 1, 1);
  335. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  336. V4L2_CID_SHARPNESS, 0, 255, 1, 0);
  337. if (hdl->error) {
  338. pr_err("Could not initialize controls\n");
  339. return hdl->error;
  340. }
  341. return 0;
  342. }
  343. /* sub-driver description */
  344. static const struct sd_desc sd_desc = {
  345. .name = MODULE_NAME,
  346. .config = sd_config,
  347. .init = sd_init,
  348. .init_controls = sd_init_controls,
  349. .isoc_init = sd_isoc_init,
  350. .start = sd_start,
  351. .stopN = sd_stopN,
  352. .pkt_scan = sd_pkt_scan,
  353. };
  354. /* -- module initialisation -- */
  355. static const struct usb_device_id device_table[] = {
  356. {USB_DEVICE(0x04fc, 0x1528)},
  357. {}
  358. };
  359. MODULE_DEVICE_TABLE(usb, device_table);
  360. /* -- device connect -- */
  361. static int sd_probe(struct usb_interface *intf,
  362. const struct usb_device_id *id)
  363. {
  364. /* the video interface for isochronous transfer is 1 */
  365. if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
  366. return -ENODEV;
  367. return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
  368. THIS_MODULE);
  369. }
  370. static struct usb_driver sd_driver = {
  371. .name = MODULE_NAME,
  372. .id_table = device_table,
  373. .probe = sd_probe,
  374. .disconnect = gspca_disconnect,
  375. #ifdef CONFIG_PM
  376. .suspend = gspca_suspend,
  377. .resume = gspca_resume,
  378. .reset_resume = gspca_resume,
  379. #endif
  380. };
  381. module_usb_driver(sd_driver);