konica.c 12 KB

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