konica.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 %d\n", ret);
  96. gspca_dev->usb_err = ret;
  97. }
  98. }
  99. static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
  100. {
  101. struct usb_device *dev = gspca_dev->dev;
  102. int ret;
  103. if (gspca_dev->usb_err < 0)
  104. return;
  105. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  106. 0x03,
  107. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  108. value,
  109. index,
  110. gspca_dev->usb_buf,
  111. 2,
  112. 1000);
  113. if (ret < 0) {
  114. pr_err("reg_w err %d\n", ret);
  115. gspca_dev->usb_err = ret;
  116. }
  117. }
  118. static void konica_stream_on(struct gspca_dev *gspca_dev)
  119. {
  120. reg_w(gspca_dev, 1, 0x0b);
  121. }
  122. static void konica_stream_off(struct gspca_dev *gspca_dev)
  123. {
  124. reg_w(gspca_dev, 0, 0x0b);
  125. }
  126. /* this function is called at probe time */
  127. static int sd_config(struct gspca_dev *gspca_dev,
  128. const struct usb_device_id *id)
  129. {
  130. gspca_dev->cam.cam_mode = vga_mode;
  131. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  132. gspca_dev->cam.no_urb_create = 1;
  133. return 0;
  134. }
  135. /* this function is called at probe and resume time */
  136. static int sd_init(struct gspca_dev *gspca_dev)
  137. {
  138. /* HDG not sure if these 2 reads are needed */
  139. reg_r(gspca_dev, 0, 0x10);
  140. PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
  141. gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
  142. reg_r(gspca_dev, 0, 0x10);
  143. PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
  144. gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
  145. reg_w(gspca_dev, 0, 0x0d);
  146. return 0;
  147. }
  148. static int sd_start(struct gspca_dev *gspca_dev)
  149. {
  150. struct sd *sd = (struct sd *) gspca_dev;
  151. struct urb *urb;
  152. int i, n, packet_size;
  153. struct usb_host_interface *alt;
  154. struct usb_interface *intf;
  155. intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
  156. alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
  157. if (!alt) {
  158. pr_err("Couldn't get altsetting\n");
  159. return -EIO;
  160. }
  161. packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
  162. n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  163. reg_w(gspca_dev, n, 0x08);
  164. konica_stream_on(gspca_dev);
  165. if (gspca_dev->usb_err)
  166. return gspca_dev->usb_err;
  167. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  168. #if MAX_NURBS < 4
  169. #error "Not enough URBs in the gspca table"
  170. #endif
  171. #define SD_NPKT 32
  172. for (n = 0; n < 4; n++) {
  173. i = n & 1 ? 0 : 1;
  174. packet_size =
  175. le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
  176. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  177. if (!urb) {
  178. pr_err("usb_alloc_urb failed\n");
  179. return -ENOMEM;
  180. }
  181. gspca_dev->urb[n] = urb;
  182. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  183. packet_size * SD_NPKT,
  184. GFP_KERNEL,
  185. &urb->transfer_dma);
  186. if (urb->transfer_buffer == NULL) {
  187. pr_err("usb_buffer_alloc failed\n");
  188. return -ENOMEM;
  189. }
  190. urb->dev = gspca_dev->dev;
  191. urb->context = gspca_dev;
  192. urb->transfer_buffer_length = packet_size * SD_NPKT;
  193. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  194. n & 1 ? 0x81 : 0x82);
  195. urb->transfer_flags = URB_ISO_ASAP
  196. | URB_NO_TRANSFER_DMA_MAP;
  197. urb->interval = 1;
  198. urb->complete = sd_isoc_irq;
  199. urb->number_of_packets = SD_NPKT;
  200. for (i = 0; i < SD_NPKT; i++) {
  201. urb->iso_frame_desc[i].length = packet_size;
  202. urb->iso_frame_desc[i].offset = packet_size * i;
  203. }
  204. }
  205. return 0;
  206. }
  207. static void sd_stopN(struct gspca_dev *gspca_dev)
  208. {
  209. struct sd *sd = (struct sd *) gspca_dev;
  210. konica_stream_off(gspca_dev);
  211. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  212. /* Don't keep the button in the pressed state "forever" if it was
  213. pressed when streaming is stopped */
  214. if (sd->snapshot_pressed) {
  215. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  216. input_sync(gspca_dev->input_dev);
  217. sd->snapshot_pressed = 0;
  218. }
  219. #endif
  220. }
  221. /* reception of an URB */
  222. static void sd_isoc_irq(struct urb *urb)
  223. {
  224. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  225. struct sd *sd = (struct sd *) gspca_dev;
  226. struct urb *data_urb, *status_urb;
  227. u8 *data;
  228. int i, st;
  229. PDEBUG(D_PACK, "sd isoc irq");
  230. if (!gspca_dev->streaming)
  231. return;
  232. if (urb->status != 0) {
  233. if (urb->status == -ESHUTDOWN)
  234. return; /* disconnection */
  235. #ifdef CONFIG_PM
  236. if (gspca_dev->frozen)
  237. return;
  238. #endif
  239. PDEBUG(D_ERR, "urb status: %d", urb->status);
  240. st = usb_submit_urb(urb, GFP_ATOMIC);
  241. if (st < 0)
  242. pr_err("resubmit urb error %d\n", st);
  243. return;
  244. }
  245. /* if this is a data URB (ep 0x82), wait */
  246. if (urb->transfer_buffer_length > 32) {
  247. sd->last_data_urb = urb;
  248. return;
  249. }
  250. status_urb = urb;
  251. data_urb = sd->last_data_urb;
  252. sd->last_data_urb = NULL;
  253. if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
  254. PDEBUG(D_ERR|D_PACK, "lost sync on frames");
  255. goto resubmit;
  256. }
  257. if (data_urb->number_of_packets != status_urb->number_of_packets) {
  258. PDEBUG(D_ERR|D_PACK,
  259. "no packets does not match, data: %d, status: %d",
  260. data_urb->number_of_packets,
  261. status_urb->number_of_packets);
  262. goto resubmit;
  263. }
  264. for (i = 0; i < status_urb->number_of_packets; i++) {
  265. if (data_urb->iso_frame_desc[i].status ||
  266. status_urb->iso_frame_desc[i].status) {
  267. PDEBUG(D_ERR|D_PACK,
  268. "pkt %d data-status %d, status-status %d", i,
  269. data_urb->iso_frame_desc[i].status,
  270. status_urb->iso_frame_desc[i].status);
  271. gspca_dev->last_packet_type = DISCARD_PACKET;
  272. continue;
  273. }
  274. if (status_urb->iso_frame_desc[i].actual_length != 1) {
  275. PDEBUG(D_ERR|D_PACK,
  276. "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 defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  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. PDEBUG(D_ERR|D_PACK,
  319. "usb_submit_urb(data_urb) ret %d", st);
  320. }
  321. st = usb_submit_urb(status_urb, GFP_ATOMIC);
  322. if (st < 0)
  323. pr_err("usb_submit_urb(status_urb) ret %d\n", st);
  324. }
  325. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  326. {
  327. struct gspca_dev *gspca_dev =
  328. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  329. gspca_dev->usb_err = 0;
  330. if (!gspca_dev->streaming)
  331. return 0;
  332. switch (ctrl->id) {
  333. case V4L2_CID_BRIGHTNESS:
  334. konica_stream_off(gspca_dev);
  335. reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
  336. konica_stream_on(gspca_dev);
  337. break;
  338. case V4L2_CID_CONTRAST:
  339. konica_stream_off(gspca_dev);
  340. reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
  341. konica_stream_on(gspca_dev);
  342. break;
  343. case V4L2_CID_SATURATION:
  344. konica_stream_off(gspca_dev);
  345. reg_w(gspca_dev, ctrl->val, SATURATION_REG);
  346. konica_stream_on(gspca_dev);
  347. break;
  348. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  349. konica_stream_off(gspca_dev);
  350. reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
  351. konica_stream_on(gspca_dev);
  352. break;
  353. case V4L2_CID_SHARPNESS:
  354. konica_stream_off(gspca_dev);
  355. reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
  356. konica_stream_on(gspca_dev);
  357. break;
  358. }
  359. return gspca_dev->usb_err;
  360. }
  361. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  362. .s_ctrl = sd_s_ctrl,
  363. };
  364. static int sd_init_controls(struct gspca_dev *gspca_dev)
  365. {
  366. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  367. gspca_dev->vdev.ctrl_handler = hdl;
  368. v4l2_ctrl_handler_init(hdl, 5);
  369. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  370. V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
  371. /* Needs to be verified */
  372. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  373. V4L2_CID_CONTRAST, 0, 9, 1, 4);
  374. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  375. V4L2_CID_SATURATION, 0, 9, 1, 4);
  376. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  377. V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  378. 0, 33, 1, 25);
  379. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  380. V4L2_CID_SHARPNESS, 0, 9, 1, 4);
  381. if (hdl->error) {
  382. pr_err("Could not initialize controls\n");
  383. return hdl->error;
  384. }
  385. return 0;
  386. }
  387. /* sub-driver description */
  388. static const struct sd_desc sd_desc = {
  389. .name = MODULE_NAME,
  390. .config = sd_config,
  391. .init = sd_init,
  392. .init_controls = sd_init_controls,
  393. .start = sd_start,
  394. .stopN = sd_stopN,
  395. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  396. .other_input = 1,
  397. #endif
  398. };
  399. /* -- module initialisation -- */
  400. static const struct usb_device_id device_table[] = {
  401. {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
  402. {}
  403. };
  404. MODULE_DEVICE_TABLE(usb, device_table);
  405. /* -- device connect -- */
  406. static int sd_probe(struct usb_interface *intf,
  407. const struct usb_device_id *id)
  408. {
  409. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  410. THIS_MODULE);
  411. }
  412. static struct usb_driver sd_driver = {
  413. .name = MODULE_NAME,
  414. .id_table = device_table,
  415. .probe = sd_probe,
  416. .disconnect = gspca_disconnect,
  417. #ifdef CONFIG_PM
  418. .suspend = gspca_suspend,
  419. .resume = gspca_resume,
  420. .reset_resume = gspca_resume,
  421. #endif
  422. };
  423. module_usb_driver(sd_driver);