pvrusb2: reduce stack usage pvr2_eeprom_analyze()
[linux/fpc-iii.git] / drivers / media / usb / gspca / konica.c
blob78542fff403fcd13792d1a9f5fd135f695064101
1 /*
2 * Driver for USB webcams based on Konica chipset. This
3 * chipset is used in Intel YC76 camera.
5 * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
7 * Based on the usbvideo v4l1 konicawc driver which is:
9 * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
11 * The code for making gspca work with a webcam with 2 isoc endpoints was
12 * taken from the benq gspca subdriver which is:
14 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
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.
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.
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
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #define MODULE_NAME "konica"
35 #include <linux/input.h>
36 #include "gspca.h"
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40 MODULE_LICENSE("GPL");
42 #define WHITEBAL_REG 0x01
43 #define BRIGHTNESS_REG 0x02
44 #define SHARPNESS_REG 0x03
45 #define CONTRAST_REG 0x04
46 #define SATURATION_REG 0x05
48 /* specific webcam descriptor */
49 struct sd {
50 struct gspca_dev gspca_dev; /* !! must be the first item */
51 struct urb *last_data_urb;
52 u8 snapshot_pressed;
56 /* .priv is what goes to register 8 for this mode, known working values:
57 0x00 -> 176x144, cropped
58 0x01 -> 176x144, cropped
59 0x02 -> 176x144, cropped
60 0x03 -> 176x144, cropped
61 0x04 -> 176x144, binned
62 0x05 -> 320x240
63 0x06 -> 320x240
64 0x07 -> 160x120, cropped
65 0x08 -> 160x120, cropped
66 0x09 -> 160x120, binned (note has 136 lines)
67 0x0a -> 160x120, binned (note has 136 lines)
68 0x0b -> 160x120, cropped
70 static const struct v4l2_pix_format vga_mode[] = {
71 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
72 .bytesperline = 160,
73 .sizeimage = 160 * 136 * 3 / 2 + 960,
74 .colorspace = V4L2_COLORSPACE_SRGB,
75 .priv = 0x0a},
76 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
77 .bytesperline = 176,
78 .sizeimage = 176 * 144 * 3 / 2 + 960,
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .priv = 0x04},
81 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
82 .bytesperline = 320,
83 .sizeimage = 320 * 240 * 3 / 2 + 960,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = 0x05},
88 static void sd_isoc_irq(struct urb *urb);
90 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
92 struct usb_device *dev = gspca_dev->dev;
93 int ret;
95 if (gspca_dev->usb_err < 0)
96 return;
97 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
98 0x02,
99 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100 value,
101 index,
102 NULL,
104 1000);
105 if (ret < 0) {
106 pr_err("reg_w err writing %02x to %02x: %d\n",
107 value, index, ret);
108 gspca_dev->usb_err = ret;
112 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
114 struct usb_device *dev = gspca_dev->dev;
115 int ret;
117 if (gspca_dev->usb_err < 0)
118 return;
119 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
120 0x03,
121 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122 value,
123 index,
124 gspca_dev->usb_buf,
126 1000);
127 if (ret < 0) {
128 pr_err("reg_r err %d\n", ret);
129 gspca_dev->usb_err = ret;
133 static void konica_stream_on(struct gspca_dev *gspca_dev)
135 reg_w(gspca_dev, 1, 0x0b);
138 static void konica_stream_off(struct gspca_dev *gspca_dev)
140 reg_w(gspca_dev, 0, 0x0b);
143 /* this function is called at probe time */
144 static int sd_config(struct gspca_dev *gspca_dev,
145 const struct usb_device_id *id)
147 gspca_dev->cam.cam_mode = vga_mode;
148 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
149 gspca_dev->cam.no_urb_create = 1;
151 return 0;
154 /* this function is called at probe and resume time */
155 static int sd_init(struct gspca_dev *gspca_dev)
157 int i;
160 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
161 * and does not want to be bothered while doing so :|
162 * Register 0x10 counts from 1 - 3, with 3 being "ready"
164 msleep(6000);
165 for (i = 0; i < 20; i++) {
166 reg_r(gspca_dev, 0, 0x10);
167 if (gspca_dev->usb_buf[0] == 3)
168 break;
169 msleep(100);
171 reg_w(gspca_dev, 0, 0x0d);
173 return gspca_dev->usb_err;
176 static int sd_start(struct gspca_dev *gspca_dev)
178 struct sd *sd = (struct sd *) gspca_dev;
179 struct urb *urb;
180 int i, n, packet_size;
181 struct usb_host_interface *alt;
182 struct usb_interface *intf;
184 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
185 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
186 if (!alt) {
187 pr_err("Couldn't get altsetting\n");
188 return -EIO;
191 if (alt->desc.bNumEndpoints < 2)
192 return -ENODEV;
194 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
196 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
197 reg_w(gspca_dev, n, 0x08);
199 konica_stream_on(gspca_dev);
201 if (gspca_dev->usb_err)
202 return gspca_dev->usb_err;
204 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
205 #if MAX_NURBS < 4
206 #error "Not enough URBs in the gspca table"
207 #endif
208 #define SD_NPKT 32
209 for (n = 0; n < 4; n++) {
210 i = n & 1 ? 0 : 1;
211 packet_size =
212 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
213 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
214 if (!urb)
215 return -ENOMEM;
216 gspca_dev->urb[n] = urb;
217 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
218 packet_size * SD_NPKT,
219 GFP_KERNEL,
220 &urb->transfer_dma);
221 if (urb->transfer_buffer == NULL) {
222 pr_err("usb_buffer_alloc failed\n");
223 return -ENOMEM;
226 urb->dev = gspca_dev->dev;
227 urb->context = gspca_dev;
228 urb->transfer_buffer_length = packet_size * SD_NPKT;
229 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
230 n & 1 ? 0x81 : 0x82);
231 urb->transfer_flags = URB_ISO_ASAP
232 | URB_NO_TRANSFER_DMA_MAP;
233 urb->interval = 1;
234 urb->complete = sd_isoc_irq;
235 urb->number_of_packets = SD_NPKT;
236 for (i = 0; i < SD_NPKT; i++) {
237 urb->iso_frame_desc[i].length = packet_size;
238 urb->iso_frame_desc[i].offset = packet_size * i;
242 return 0;
245 static void sd_stopN(struct gspca_dev *gspca_dev)
247 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
249 konica_stream_off(gspca_dev);
250 #if IS_ENABLED(CONFIG_INPUT)
251 /* Don't keep the button in the pressed state "forever" if it was
252 pressed when streaming is stopped */
253 if (sd->snapshot_pressed) {
254 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
255 input_sync(gspca_dev->input_dev);
256 sd->snapshot_pressed = 0;
258 #endif
261 /* reception of an URB */
262 static void sd_isoc_irq(struct urb *urb)
264 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
265 struct sd *sd = (struct sd *) gspca_dev;
266 struct urb *data_urb, *status_urb;
267 u8 *data;
268 int i, st;
270 PDEBUG(D_PACK, "sd isoc irq");
271 if (!gspca_dev->streaming)
272 return;
274 if (urb->status != 0) {
275 if (urb->status == -ESHUTDOWN)
276 return; /* disconnection */
277 #ifdef CONFIG_PM
278 if (gspca_dev->frozen)
279 return;
280 #endif
281 PERR("urb status: %d", urb->status);
282 st = usb_submit_urb(urb, GFP_ATOMIC);
283 if (st < 0)
284 pr_err("resubmit urb error %d\n", st);
285 return;
288 /* if this is a data URB (ep 0x82), wait */
289 if (urb->transfer_buffer_length > 32) {
290 sd->last_data_urb = urb;
291 return;
294 status_urb = urb;
295 data_urb = sd->last_data_urb;
296 sd->last_data_urb = NULL;
298 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
299 PERR("lost sync on frames");
300 goto resubmit;
303 if (data_urb->number_of_packets != status_urb->number_of_packets) {
304 PERR("no packets does not match, data: %d, status: %d",
305 data_urb->number_of_packets,
306 status_urb->number_of_packets);
307 goto resubmit;
310 for (i = 0; i < status_urb->number_of_packets; i++) {
311 if (data_urb->iso_frame_desc[i].status ||
312 status_urb->iso_frame_desc[i].status) {
313 PERR("pkt %d data-status %d, status-status %d", i,
314 data_urb->iso_frame_desc[i].status,
315 status_urb->iso_frame_desc[i].status);
316 gspca_dev->last_packet_type = DISCARD_PACKET;
317 continue;
320 if (status_urb->iso_frame_desc[i].actual_length != 1) {
321 PERR("bad status packet length %d",
322 status_urb->iso_frame_desc[i].actual_length);
323 gspca_dev->last_packet_type = DISCARD_PACKET;
324 continue;
327 st = *((u8 *)status_urb->transfer_buffer
328 + status_urb->iso_frame_desc[i].offset);
330 data = (u8 *)data_urb->transfer_buffer
331 + data_urb->iso_frame_desc[i].offset;
333 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
334 * otherwise:
335 * bit 0 0: keep packet
336 * 1: drop packet (padding data)
338 * bit 4 0 button not clicked
339 * 1 button clicked
340 * button is used to `take a picture' (in software)
342 if (st & 0x80) {
343 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
344 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
345 } else {
346 #if IS_ENABLED(CONFIG_INPUT)
347 u8 button_state = st & 0x40 ? 1 : 0;
348 if (sd->snapshot_pressed != button_state) {
349 input_report_key(gspca_dev->input_dev,
350 KEY_CAMERA,
351 button_state);
352 input_sync(gspca_dev->input_dev);
353 sd->snapshot_pressed = button_state;
355 #endif
356 if (st & 0x01)
357 continue;
359 gspca_frame_add(gspca_dev, INTER_PACKET, data,
360 data_urb->iso_frame_desc[i].actual_length);
363 resubmit:
364 if (data_urb) {
365 st = usb_submit_urb(data_urb, GFP_ATOMIC);
366 if (st < 0)
367 PERR("usb_submit_urb(data_urb) ret %d", st);
369 st = usb_submit_urb(status_urb, GFP_ATOMIC);
370 if (st < 0)
371 PERR("usb_submit_urb(status_urb) ret %d\n", st);
374 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
376 struct gspca_dev *gspca_dev =
377 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
379 gspca_dev->usb_err = 0;
381 if (!gspca_dev->streaming)
382 return 0;
384 switch (ctrl->id) {
385 case V4L2_CID_BRIGHTNESS:
386 konica_stream_off(gspca_dev);
387 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
388 konica_stream_on(gspca_dev);
389 break;
390 case V4L2_CID_CONTRAST:
391 konica_stream_off(gspca_dev);
392 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
393 konica_stream_on(gspca_dev);
394 break;
395 case V4L2_CID_SATURATION:
396 konica_stream_off(gspca_dev);
397 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
398 konica_stream_on(gspca_dev);
399 break;
400 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
401 konica_stream_off(gspca_dev);
402 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
403 konica_stream_on(gspca_dev);
404 break;
405 case V4L2_CID_SHARPNESS:
406 konica_stream_off(gspca_dev);
407 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
408 konica_stream_on(gspca_dev);
409 break;
411 return gspca_dev->usb_err;
414 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
415 .s_ctrl = sd_s_ctrl,
418 static int sd_init_controls(struct gspca_dev *gspca_dev)
420 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
422 gspca_dev->vdev.ctrl_handler = hdl;
423 v4l2_ctrl_handler_init(hdl, 5);
424 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
425 V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
426 /* Needs to be verified */
427 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
428 V4L2_CID_CONTRAST, 0, 9, 1, 4);
429 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
430 V4L2_CID_SATURATION, 0, 9, 1, 4);
431 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
432 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
433 0, 33, 1, 25);
434 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
435 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
437 if (hdl->error) {
438 pr_err("Could not initialize controls\n");
439 return hdl->error;
441 return 0;
444 /* sub-driver description */
445 static const struct sd_desc sd_desc = {
446 .name = MODULE_NAME,
447 .config = sd_config,
448 .init = sd_init,
449 .init_controls = sd_init_controls,
450 .start = sd_start,
451 .stopN = sd_stopN,
452 #if IS_ENABLED(CONFIG_INPUT)
453 .other_input = 1,
454 #endif
457 /* -- module initialisation -- */
458 static const struct usb_device_id device_table[] = {
459 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
462 MODULE_DEVICE_TABLE(usb, device_table);
464 /* -- device connect -- */
465 static int sd_probe(struct usb_interface *intf,
466 const struct usb_device_id *id)
468 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
469 THIS_MODULE);
472 static struct usb_driver sd_driver = {
473 .name = MODULE_NAME,
474 .id_table = device_table,
475 .probe = sd_probe,
476 .disconnect = gspca_disconnect,
477 #ifdef CONFIG_PM
478 .suspend = gspca_suspend,
479 .resume = gspca_resume,
480 .reset_resume = gspca_resume,
481 #endif
484 module_usb_driver(sd_driver);