konica.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Driver for USB webcams based on Konica chipset. This
  3. * chipset is used in Intel YC76 camera.
  4. *
  5. * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * Based on the usbvideo v4l1 konicawc driver which is:
  8. *
  9. * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
  10. *
  11. * The code for making gspca work with a webcam with 2 isoc endpoints was
  12. * taken from the benq gspca subdriver which is:
  13. *
  14. * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #define MODULE_NAME "konica"
  28. #include <linux/input.h>
  29. #include "gspca.h"
  30. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  31. MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
  32. MODULE_LICENSE("GPL");
  33. #define WHITEBAL_REG 0x01
  34. #define BRIGHTNESS_REG 0x02
  35. #define SHARPNESS_REG 0x03
  36. #define CONTRAST_REG 0x04
  37. #define SATURATION_REG 0x05
  38. /* specific webcam descriptor */
  39. struct sd {
  40. struct gspca_dev gspca_dev; /* !! must be the first item */
  41. struct urb *last_data_urb;
  42. u8 snapshot_pressed;
  43. };
  44. /* .priv is what goes to register 8 for this mode, known working values:
  45. 0x00 -> 176x144, cropped
  46. 0x01 -> 176x144, cropped
  47. 0x02 -> 176x144, cropped
  48. 0x03 -> 176x144, cropped
  49. 0x04 -> 176x144, binned
  50. 0x05 -> 320x240
  51. 0x06 -> 320x240
  52. 0x07 -> 160x120, cropped
  53. 0x08 -> 160x120, cropped
  54. 0x09 -> 160x120, binned (note has 136 lines)
  55. 0x0a -> 160x120, binned (note has 136 lines)
  56. 0x0b -> 160x120, cropped
  57. */
  58. static const struct v4l2_pix_format vga_mode[] = {
  59. {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
  60. .bytesperline = 160,
  61. .sizeimage = 160 * 136 * 3 / 2 + 960,
  62. .colorspace = V4L2_COLORSPACE_SRGB,
  63. .priv = 0x0a},
  64. {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
  65. .bytesperline = 176,
  66. .sizeimage = 176 * 144 * 3 / 2 + 960,
  67. .colorspace = V4L2_COLORSPACE_SRGB,
  68. .priv = 0x04},
  69. {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
  70. .bytesperline = 320,
  71. .sizeimage = 320 * 240 * 3 / 2 + 960,
  72. .colorspace = V4L2_COLORSPACE_SRGB,
  73. .priv = 0x05},
  74. };
  75. static void sd_isoc_irq(struct urb *urb);
  76. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  77. {
  78. struct usb_device *dev = gspca_dev->dev;
  79. int ret;
  80. if (gspca_dev->usb_err < 0)
  81. return;
  82. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  83. 0x02,
  84. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  85. value,
  86. index,
  87. NULL,
  88. 0,
  89. 1000);
  90. if (ret < 0) {
  91. pr_err("reg_w err writing %02x to %02x: %d\n",
  92. value, index, ret);
  93. gspca_dev->usb_err = ret;
  94. }
  95. }
  96. static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
  97. {
  98. struct usb_device *dev = gspca_dev->dev;
  99. int ret;
  100. if (gspca_dev->usb_err < 0)
  101. return;
  102. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  103. 0x03,
  104. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  105. value,
  106. index,
  107. gspca_dev->usb_buf,
  108. 2,
  109. 1000);
  110. if (ret < 0) {
  111. pr_err("reg_r err %d\n", ret);
  112. gspca_dev->usb_err = ret;
  113. }
  114. }
  115. static void konica_stream_on(struct gspca_dev *gspca_dev)
  116. {
  117. reg_w(gspca_dev, 1, 0x0b);
  118. }
  119. static void konica_stream_off(struct gspca_dev *gspca_dev)
  120. {
  121. reg_w(gspca_dev, 0, 0x0b);
  122. }
  123. /* this function is called at probe time */
  124. static int sd_config(struct gspca_dev *gspca_dev,
  125. const struct usb_device_id *id)
  126. {
  127. gspca_dev->cam.cam_mode = vga_mode;
  128. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  129. gspca_dev->cam.no_urb_create = 1;
  130. return 0;
  131. }
  132. /* this function is called at probe and resume time */
  133. static int sd_init(struct gspca_dev *gspca_dev)
  134. {
  135. int i;
  136. /*
  137. * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
  138. * and does not want to be bothered while doing so :|
  139. * Register 0x10 counts from 1 - 3, with 3 being "ready"
  140. */
  141. msleep(6000);
  142. for (i = 0; i < 20; i++) {
  143. reg_r(gspca_dev, 0, 0x10);
  144. if (gspca_dev->usb_buf[0] == 3)
  145. break;
  146. msleep(100);
  147. }
  148. reg_w(gspca_dev, 0, 0x0d);
  149. return gspca_dev->usb_err;
  150. }
  151. static int sd_start(struct gspca_dev *gspca_dev)
  152. {
  153. struct sd *sd = (struct sd *) gspca_dev;
  154. struct urb *urb;
  155. int i, n, packet_size;
  156. struct usb_host_interface *alt;
  157. struct usb_interface *intf;
  158. intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
  159. alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
  160. if (!alt) {
  161. pr_err("Couldn't get altsetting\n");
  162. return -EIO;
  163. }
  164. if (alt->desc.bNumEndpoints < 2)
  165. return -ENODEV;
  166. packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
  167. n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  168. reg_w(gspca_dev, n, 0x08);
  169. konica_stream_on(gspca_dev);
  170. if (gspca_dev->usb_err)
  171. return gspca_dev->usb_err;
  172. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  173. #if MAX_NURBS < 4
  174. #error "Not enough URBs in the gspca table"
  175. #endif
  176. #define SD_NPKT 32
  177. for (n = 0; n < 4; n++) {
  178. i = n & 1 ? 0 : 1;
  179. packet_size =
  180. le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
  181. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  182. if (!urb)
  183. return -ENOMEM;
  184. gspca_dev->urb[n] = urb;
  185. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  186. packet_size * SD_NPKT,
  187. GFP_KERNEL,
  188. &urb->transfer_dma);
  189. if (urb->transfer_buffer == NULL) {
  190. pr_err("usb_buffer_alloc failed\n");
  191. return -ENOMEM;
  192. }
  193. urb->dev = gspca_dev->dev;
  194. urb->context = gspca_dev;
  195. urb->transfer_buffer_length = packet_size * SD_NPKT;
  196. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  197. n & 1 ? 0x81 : 0x82);
  198. urb->transfer_flags = URB_ISO_ASAP
  199. | URB_NO_TRANSFER_DMA_MAP;
  200. urb->interval = 1;
  201. urb->complete = sd_isoc_irq;
  202. urb->number_of_packets = SD_NPKT;
  203. for (i = 0; i < SD_NPKT; i++) {
  204. urb->iso_frame_desc[i].length = packet_size;
  205. urb->iso_frame_desc[i].offset = packet_size * i;
  206. }
  207. }
  208. return 0;
  209. }
  210. static void sd_stopN(struct gspca_dev *gspca_dev)
  211. {
  212. struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
  213. konica_stream_off(gspca_dev);
  214. #if IS_ENABLED(CONFIG_INPUT)
  215. /* Don't keep the button in the pressed state "forever" if it was
  216. pressed when streaming is stopped */
  217. if (sd->snapshot_pressed) {
  218. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  219. input_sync(gspca_dev->input_dev);
  220. sd->snapshot_pressed = 0;
  221. }
  222. #endif
  223. }
  224. /* reception of an URB */
  225. static void sd_isoc_irq(struct urb *urb)
  226. {
  227. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  228. struct sd *sd = (struct sd *) gspca_dev;
  229. struct urb *data_urb, *status_urb;
  230. u8 *data;
  231. int i, st;
  232. PDEBUG(D_PACK, "sd isoc irq");
  233. if (!gspca_dev->streaming)
  234. return;
  235. if (urb->status != 0) {
  236. if (urb->status == -ESHUTDOWN)
  237. return; /* disconnection */
  238. #ifdef CONFIG_PM
  239. if (gspca_dev->frozen)
  240. return;
  241. #endif
  242. PERR("urb status: %d", urb->status);
  243. st = usb_submit_urb(urb, GFP_ATOMIC);
  244. if (st < 0)
  245. pr_err("resubmit urb error %d\n", st);
  246. return;
  247. }
  248. /* if this is a data URB (ep 0x82), wait */
  249. if (urb->transfer_buffer_length > 32) {
  250. sd->last_data_urb = urb;
  251. return;
  252. }
  253. status_urb = urb;
  254. data_urb = sd->last_data_urb;
  255. sd->last_data_urb = NULL;
  256. if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
  257. PERR("lost sync on frames");
  258. goto resubmit;
  259. }
  260. if (data_urb->number_of_packets != status_urb->number_of_packets) {
  261. PERR("no packets does not match, data: %d, status: %d",
  262. data_urb->number_of_packets,
  263. status_urb->number_of_packets);
  264. goto resubmit;
  265. }
  266. for (i = 0; i < status_urb->number_of_packets; i++) {
  267. if (data_urb->iso_frame_desc[i].status ||
  268. status_urb->iso_frame_desc[i].status) {
  269. PERR("pkt %d data-status %d, status-status %d", i,
  270. data_urb->iso_frame_desc[i].status,
  271. status_urb->iso_frame_desc[i].status);
  272. gspca_dev->last_packet_type = DISCARD_PACKET;
  273. continue;
  274. }
  275. if (status_urb->iso_frame_desc[i].actual_length != 1) {
  276. PERR("bad status packet length %d",
  277. status_urb->iso_frame_desc[i].actual_length);
  278. gspca_dev->last_packet_type = DISCARD_PACKET;
  279. continue;
  280. }
  281. st = *((u8 *)status_urb->transfer_buffer
  282. + status_urb->iso_frame_desc[i].offset);
  283. data = (u8 *)data_urb->transfer_buffer
  284. + data_urb->iso_frame_desc[i].offset;
  285. /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
  286. * otherwise:
  287. * bit 0 0: keep packet
  288. * 1: drop packet (padding data)
  289. *
  290. * bit 4 0 button not clicked
  291. * 1 button clicked
  292. * button is used to `take a picture' (in software)
  293. */
  294. if (st & 0x80) {
  295. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  296. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  297. } else {
  298. #if IS_ENABLED(CONFIG_INPUT)
  299. u8 button_state = st & 0x40 ? 1 : 0;
  300. if (sd->snapshot_pressed != button_state) {
  301. input_report_key(gspca_dev->input_dev,
  302. KEY_CAMERA,
  303. button_state);
  304. input_sync(gspca_dev->input_dev);
  305. sd->snapshot_pressed = button_state;
  306. }
  307. #endif
  308. if (st & 0x01)
  309. continue;
  310. }
  311. gspca_frame_add(gspca_dev, INTER_PACKET, data,
  312. data_urb->iso_frame_desc[i].actual_length);
  313. }
  314. resubmit:
  315. if (data_urb) {
  316. st = usb_submit_urb(data_urb, GFP_ATOMIC);
  317. if (st < 0)
  318. PERR("usb_submit_urb(data_urb) ret %d", st);
  319. }
  320. st = usb_submit_urb(status_urb, GFP_ATOMIC);
  321. if (st < 0)
  322. PERR("usb_submit_urb(status_urb) ret %d\n", st);
  323. }
  324. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  325. {
  326. struct gspca_dev *gspca_dev =
  327. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  328. gspca_dev->usb_err = 0;
  329. if (!gspca_dev->streaming)
  330. return 0;
  331. switch (ctrl->id) {
  332. case V4L2_CID_BRIGHTNESS:
  333. konica_stream_off(gspca_dev);
  334. reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
  335. konica_stream_on(gspca_dev);
  336. break;
  337. case V4L2_CID_CONTRAST:
  338. konica_stream_off(gspca_dev);
  339. reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
  340. konica_stream_on(gspca_dev);
  341. break;
  342. case V4L2_CID_SATURATION:
  343. konica_stream_off(gspca_dev);
  344. reg_w(gspca_dev, ctrl->val, SATURATION_REG);
  345. konica_stream_on(gspca_dev);
  346. break;
  347. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  348. konica_stream_off(gspca_dev);
  349. reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
  350. konica_stream_on(gspca_dev);
  351. break;
  352. case V4L2_CID_SHARPNESS:
  353. konica_stream_off(gspca_dev);
  354. reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
  355. konica_stream_on(gspca_dev);
  356. break;
  357. }
  358. return gspca_dev->usb_err;
  359. }
  360. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  361. .s_ctrl = sd_s_ctrl,
  362. };
  363. static int sd_init_controls(struct gspca_dev *gspca_dev)
  364. {
  365. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  366. gspca_dev->vdev.ctrl_handler = hdl;
  367. v4l2_ctrl_handler_init(hdl, 5);
  368. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  369. V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
  370. /* Needs to be verified */
  371. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  372. V4L2_CID_CONTRAST, 0, 9, 1, 4);
  373. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  374. V4L2_CID_SATURATION, 0, 9, 1, 4);
  375. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  376. V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  377. 0, 33, 1, 25);
  378. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  379. V4L2_CID_SHARPNESS, 0, 9, 1, 4);
  380. if (hdl->error) {
  381. pr_err("Could not initialize controls\n");
  382. return hdl->error;
  383. }
  384. return 0;
  385. }
  386. /* sub-driver description */
  387. static const struct sd_desc sd_desc = {
  388. .name = MODULE_NAME,
  389. .config = sd_config,
  390. .init = sd_init,
  391. .init_controls = sd_init_controls,
  392. .start = sd_start,
  393. .stopN = sd_stopN,
  394. #if IS_ENABLED(CONFIG_INPUT)
  395. .other_input = 1,
  396. #endif
  397. };
  398. /* -- module initialisation -- */
  399. static const struct usb_device_id device_table[] = {
  400. {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
  401. {}
  402. };
  403. MODULE_DEVICE_TABLE(usb, device_table);
  404. /* -- device connect -- */
  405. static int sd_probe(struct usb_interface *intf,
  406. const struct usb_device_id *id)
  407. {
  408. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  409. THIS_MODULE);
  410. }
  411. static struct usb_driver sd_driver = {
  412. .name = MODULE_NAME,
  413. .id_table = device_table,
  414. .probe = sd_probe,
  415. .disconnect = gspca_disconnect,
  416. #ifdef CONFIG_PM
  417. .suspend = gspca_suspend,
  418. .resume = gspca_resume,
  419. .reset_resume = gspca_resume,
  420. #endif
  421. };
  422. module_usb_driver(sd_driver);