konica.c 12 KB

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