1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for USB webcams based on Konica chipset. This
4 * chipset is used in Intel YC76 camera.
8 * Based on the usbvideo v4l1 konicawc driver which is:
12 * The code for making gspca work with a webcam with 2 isoc endpoints was
13 * taken from the benq gspca subdriver which is:
15 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #define MODULE_NAME "konica"
22 #include <linux/input.h>
26 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
27 MODULE_LICENSE("GPL");
29 #define WHITEBAL_REG 0x01
30 #define BRIGHTNESS_REG 0x02
31 #define SHARPNESS_REG 0x03
32 #define CONTRAST_REG 0x04
33 #define SATURATION_REG 0x05
35 /* specific webcam descriptor */
37 struct gspca_dev gspca_dev; /* !! must be the first item */
38 struct urb *last_data_urb;
43 /* .priv is what goes to register 8 for this mode, known working values:
44 0x00 -> 176x144, cropped
45 0x01 -> 176x144, cropped
46 0x02 -> 176x144, cropped
47 0x03 -> 176x144, cropped
48 0x04 -> 176x144, binned
51 0x07 -> 160x120, cropped
52 0x08 -> 160x120, cropped
53 0x09 -> 160x120, binned (note has 136 lines)
54 0x0a -> 160x120, binned (note has 136 lines)
55 0x0b -> 160x120, cropped
57 static const struct v4l2_pix_format vga_mode[] = {
58 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
60 .sizeimage = 160 * 136 * 3 / 2 + 960,
61 .colorspace = V4L2_COLORSPACE_SRGB,
63 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
65 .sizeimage = 176 * 144 * 3 / 2 + 960,
66 .colorspace = V4L2_COLORSPACE_SRGB,
68 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
70 .sizeimage = 320 * 240 * 3 / 2 + 960,
71 .colorspace = V4L2_COLORSPACE_SRGB,
75 static void sd_isoc_irq(struct urb *urb);
77 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
79 struct usb_device *dev = gspca_dev->dev;
82 if (gspca_dev->usb_err < 0)
84 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
86 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
93 pr_err("reg_w err writing %02x to %02x: %d\n",
95 gspca_dev->usb_err = ret;
99 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
101 struct usb_device *dev = gspca_dev->dev;
104 if (gspca_dev->usb_err < 0)
106 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
115 pr_err("reg_r err %d\n", ret);
116 gspca_dev->usb_err = ret;
118 * Make sure the buffer is zeroed to avoid uninitialized
121 memset(gspca_dev->usb_buf, 0, 2);
125 static void konica_stream_on(struct gspca_dev *gspca_dev)
127 reg_w(gspca_dev, 1, 0x0b);
130 static void konica_stream_off(struct gspca_dev *gspca_dev)
132 reg_w(gspca_dev, 0, 0x0b);
135 /* this function is called at probe time */
136 static int sd_config(struct gspca_dev *gspca_dev,
137 const struct usb_device_id *id)
139 gspca_dev->cam.cam_mode = vga_mode;
140 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
141 gspca_dev->cam.no_urb_create = 1;
146 /* this function is called at probe and resume time */
147 static int sd_init(struct gspca_dev *gspca_dev)
152 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
153 * and does not want to be bothered while doing so :|
154 * Register 0x10 counts from 1 - 3, with 3 being "ready"
157 for (i = 0; i < 20; i++) {
158 reg_r(gspca_dev, 0, 0x10);
159 if (gspca_dev->usb_buf[0] == 3)
163 reg_w(gspca_dev, 0, 0x0d);
165 return gspca_dev->usb_err;
168 static int sd_start(struct gspca_dev *gspca_dev)
170 struct sd *sd = (struct sd *) gspca_dev;
172 int i, n, packet_size;
173 struct usb_host_interface *alt;
174 struct usb_interface *intf;
176 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
177 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
179 pr_err("Couldn't get altsetting\n");
183 if (alt->desc.bNumEndpoints < 2)
186 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
188 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
189 reg_w(gspca_dev, n, 0x08);
191 konica_stream_on(gspca_dev);
193 if (gspca_dev->usb_err)
194 return gspca_dev->usb_err;
196 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
198 #error "Not enough URBs in the gspca table"
201 for (n = 0; n < 4; n++) {
204 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
205 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
208 gspca_dev->urb[n] = urb;
209 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
210 packet_size * SD_NPKT,
213 if (urb->transfer_buffer == NULL) {
214 pr_err("usb_buffer_alloc failed\n");
218 urb->dev = gspca_dev->dev;
219 urb->context = gspca_dev;
220 urb->transfer_buffer_length = packet_size * SD_NPKT;
221 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
222 n & 1 ? 0x81 : 0x82);
223 urb->transfer_flags = URB_ISO_ASAP
224 | URB_NO_TRANSFER_DMA_MAP;
226 urb->complete = sd_isoc_irq;
227 urb->number_of_packets = SD_NPKT;
228 for (i = 0; i < SD_NPKT; i++) {
229 urb->iso_frame_desc[i].length = packet_size;
230 urb->iso_frame_desc[i].offset = packet_size * i;
237 static void sd_stopN(struct gspca_dev *gspca_dev)
239 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
241 konica_stream_off(gspca_dev);
242 #if IS_ENABLED(CONFIG_INPUT)
243 /* Don't keep the button in the pressed state "forever" if it was
244 pressed when streaming is stopped */
245 if (sd->snapshot_pressed) {
246 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
247 input_sync(gspca_dev->input_dev);
248 sd->snapshot_pressed = 0;
253 /* reception of an URB */
254 static void sd_isoc_irq(struct urb *urb)
256 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
257 struct sd *sd = (struct sd *) gspca_dev;
258 struct urb *data_urb, *status_urb;
262 gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
263 if (!gspca_dev->streaming)
266 if (urb->status != 0) {
267 if (urb->status == -ESHUTDOWN)
268 return; /* disconnection */
270 if (gspca_dev->frozen)
273 gspca_err(gspca_dev, "urb status: %d\n", urb->status);
274 st = usb_submit_urb(urb, GFP_ATOMIC);
276 pr_err("resubmit urb error %d\n", st);
280 /* if this is a data URB (ep 0x82), wait */
281 if (urb->transfer_buffer_length > 32) {
282 sd->last_data_urb = urb;
287 data_urb = sd->last_data_urb;
288 sd->last_data_urb = NULL;
290 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
291 gspca_err(gspca_dev, "lost sync on frames\n");
295 if (data_urb->number_of_packets != status_urb->number_of_packets) {
296 gspca_err(gspca_dev, "no packets does not match, data: %d, status: %d\n",
297 data_urb->number_of_packets,
298 status_urb->number_of_packets);
302 for (i = 0; i < status_urb->number_of_packets; i++) {
303 if (data_urb->iso_frame_desc[i].status ||
304 status_urb->iso_frame_desc[i].status) {
305 gspca_err(gspca_dev, "pkt %d data-status %d, status-status %d\n",
307 data_urb->iso_frame_desc[i].status,
308 status_urb->iso_frame_desc[i].status);
309 gspca_dev->last_packet_type = DISCARD_PACKET;
313 if (status_urb->iso_frame_desc[i].actual_length != 1) {
314 gspca_err(gspca_dev, "bad status packet length %d\n",
315 status_urb->iso_frame_desc[i].actual_length);
316 gspca_dev->last_packet_type = DISCARD_PACKET;
320 st = *((u8 *)status_urb->transfer_buffer
321 + status_urb->iso_frame_desc[i].offset);
323 data = (u8 *)data_urb->transfer_buffer
324 + data_urb->iso_frame_desc[i].offset;
326 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
328 * bit 0 0: keep packet
329 * 1: drop packet (padding data)
331 * bit 4 0 button not clicked
333 * button is used to `take a picture' (in software)
336 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
337 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
339 #if IS_ENABLED(CONFIG_INPUT)
340 u8 button_state = st & 0x40 ? 1 : 0;
341 if (sd->snapshot_pressed != button_state) {
342 input_report_key(gspca_dev->input_dev,
345 input_sync(gspca_dev->input_dev);
346 sd->snapshot_pressed = button_state;
352 gspca_frame_add(gspca_dev, INTER_PACKET, data,
353 data_urb->iso_frame_desc[i].actual_length);
358 st = usb_submit_urb(data_urb, GFP_ATOMIC);
360 gspca_err(gspca_dev, "usb_submit_urb(data_urb) ret %d\n",
363 st = usb_submit_urb(status_urb, GFP_ATOMIC);
365 gspca_err(gspca_dev, "usb_submit_urb(status_urb) ret %d\n", st);
368 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
370 struct gspca_dev *gspca_dev =
371 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
373 gspca_dev->usb_err = 0;
375 if (!gspca_dev->streaming)
379 case V4L2_CID_BRIGHTNESS:
380 konica_stream_off(gspca_dev);
381 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
382 konica_stream_on(gspca_dev);
384 case V4L2_CID_CONTRAST:
385 konica_stream_off(gspca_dev);
386 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
387 konica_stream_on(gspca_dev);
389 case V4L2_CID_SATURATION:
390 konica_stream_off(gspca_dev);
391 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
392 konica_stream_on(gspca_dev);
394 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
395 konica_stream_off(gspca_dev);
396 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
397 konica_stream_on(gspca_dev);
399 case V4L2_CID_SHARPNESS:
400 konica_stream_off(gspca_dev);
401 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
402 konica_stream_on(gspca_dev);
405 return gspca_dev->usb_err;
408 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
412 static int sd_init_controls(struct gspca_dev *gspca_dev)
414 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
416 gspca_dev->vdev.ctrl_handler = hdl;
417 v4l2_ctrl_handler_init(hdl, 5);
418 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
419 V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
420 /* Needs to be verified */
421 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
422 V4L2_CID_CONTRAST, 0, 9, 1, 4);
423 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
424 V4L2_CID_SATURATION, 0, 9, 1, 4);
425 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
426 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
428 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
429 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
432 pr_err("Could not initialize controls\n");
438 /* sub-driver description */
439 static const struct sd_desc sd_desc = {
443 .init_controls = sd_init_controls,
446 #if IS_ENABLED(CONFIG_INPUT)
451 /* -- module initialisation -- */
452 static const struct usb_device_id device_table[] = {
453 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
456 MODULE_DEVICE_TABLE(usb, device_table);
458 /* -- device connect -- */
459 static int sd_probe(struct usb_interface *intf,
460 const struct usb_device_id *id)
462 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
466 static struct usb_driver sd_driver = {
468 .id_table = device_table,
470 .disconnect = gspca_disconnect,
472 .suspend = gspca_suspend,
473 .resume = gspca_resume,
474 .reset_resume = gspca_resume,
478 module_usb_driver(sd_driver);