]> Pileus Git - ~andy/linux/blob - drivers/media/video/gspca/konica.c
5dcfed208c31617361480ad51ee9ca0ae6bc2d21
[~andy/linux] / drivers / media / video / gspca / konica.c
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
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #define MODULE_NAME "konica"
34
35 #include <linux/input.h>
36 #include "gspca.h"
37
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40 MODULE_LICENSE("GPL");
41
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
47
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;
53 };
54
55
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
69 */
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},
86 };
87
88 static void sd_isoc_irq(struct urb *urb);
89
90 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
91 {
92         struct usb_device *dev = gspca_dev->dev;
93         int ret;
94
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,
103                         0,
104                         1000);
105         if (ret < 0) {
106                 pr_err("reg_w err %d\n", ret);
107                 gspca_dev->usb_err = ret;
108         }
109 }
110
111 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
112 {
113         struct usb_device *dev = gspca_dev->dev;
114         int ret;
115
116         if (gspca_dev->usb_err < 0)
117                 return;
118         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
119                         0x03,
120                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121                         value,
122                         index,
123                         gspca_dev->usb_buf,
124                         2,
125                         1000);
126         if (ret < 0) {
127                 pr_err("reg_w err %d\n", ret);
128                 gspca_dev->usb_err = ret;
129         }
130 }
131
132 static void konica_stream_on(struct gspca_dev *gspca_dev)
133 {
134         reg_w(gspca_dev, 1, 0x0b);
135 }
136
137 static void konica_stream_off(struct gspca_dev *gspca_dev)
138 {
139         reg_w(gspca_dev, 0, 0x0b);
140 }
141
142 /* this function is called at probe time */
143 static int sd_config(struct gspca_dev *gspca_dev,
144                         const struct usb_device_id *id)
145 {
146         gspca_dev->cam.cam_mode = vga_mode;
147         gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
148         gspca_dev->cam.no_urb_create = 1;
149
150         return 0;
151 }
152
153 /* this function is called at probe and resume time */
154 static int sd_init(struct gspca_dev *gspca_dev)
155 {
156         /* HDG not sure if these 2 reads are needed */
157         reg_r(gspca_dev, 0, 0x10);
158         PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
159                gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
160         reg_r(gspca_dev, 0, 0x10);
161         PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
162                gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
163         reg_w(gspca_dev, 0, 0x0d);
164
165         return 0;
166 }
167
168 static int sd_start(struct gspca_dev *gspca_dev)
169 {
170         struct sd *sd = (struct sd *) gspca_dev;
171         struct urb *urb;
172         int i, n, packet_size;
173         struct usb_host_interface *alt;
174         struct usb_interface *intf;
175
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);
178         if (!alt) {
179                 pr_err("Couldn't get altsetting\n");
180                 return -EIO;
181         }
182
183         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
184
185         n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
186         reg_w(gspca_dev, n, 0x08);
187
188         konica_stream_on(gspca_dev);
189
190         if (gspca_dev->usb_err)
191                 return gspca_dev->usb_err;
192
193         /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
194 #if MAX_NURBS < 4
195 #error "Not enough URBs in the gspca table"
196 #endif
197 #define SD_NPKT 32
198         for (n = 0; n < 4; n++) {
199                 i = n & 1 ? 0 : 1;
200                 packet_size =
201                         le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
202                 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
203                 if (!urb) {
204                         pr_err("usb_alloc_urb failed\n");
205                         return -ENOMEM;
206                 }
207                 gspca_dev->urb[n] = urb;
208                 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
209                                                 packet_size * SD_NPKT,
210                                                 GFP_KERNEL,
211                                                 &urb->transfer_dma);
212                 if (urb->transfer_buffer == NULL) {
213                         pr_err("usb_buffer_alloc failed\n");
214                         return -ENOMEM;
215                 }
216
217                 urb->dev = gspca_dev->dev;
218                 urb->context = gspca_dev;
219                 urb->transfer_buffer_length = packet_size * SD_NPKT;
220                 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
221                                         n & 1 ? 0x81 : 0x82);
222                 urb->transfer_flags = URB_ISO_ASAP
223                                         | URB_NO_TRANSFER_DMA_MAP;
224                 urb->interval = 1;
225                 urb->complete = sd_isoc_irq;
226                 urb->number_of_packets = SD_NPKT;
227                 for (i = 0; i < SD_NPKT; i++) {
228                         urb->iso_frame_desc[i].length = packet_size;
229                         urb->iso_frame_desc[i].offset = packet_size * i;
230                 }
231         }
232
233         return 0;
234 }
235
236 static void sd_stopN(struct gspca_dev *gspca_dev)
237 {
238         struct sd *sd = (struct sd *) gspca_dev;
239
240         konica_stream_off(gspca_dev);
241 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
242         /* Don't keep the button in the pressed state "forever" if it was
243            pressed when streaming is stopped */
244         if (sd->snapshot_pressed) {
245                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
246                 input_sync(gspca_dev->input_dev);
247                 sd->snapshot_pressed = 0;
248         }
249 #endif
250 }
251
252 /* reception of an URB */
253 static void sd_isoc_irq(struct urb *urb)
254 {
255         struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
256         struct sd *sd = (struct sd *) gspca_dev;
257         struct urb *data_urb, *status_urb;
258         u8 *data;
259         int i, st;
260
261         PDEBUG(D_PACK, "sd isoc irq");
262         if (!gspca_dev->streaming)
263                 return;
264
265         if (urb->status != 0) {
266                 if (urb->status == -ESHUTDOWN)
267                         return;         /* disconnection */
268 #ifdef CONFIG_PM
269                 if (gspca_dev->frozen)
270                         return;
271 #endif
272                 PDEBUG(D_ERR, "urb status: %d", urb->status);
273                 st = usb_submit_urb(urb, GFP_ATOMIC);
274                 if (st < 0)
275                         pr_err("resubmit urb error %d\n", st);
276                 return;
277         }
278
279         /* if this is a data URB (ep 0x82), wait */
280         if (urb->transfer_buffer_length > 32) {
281                 sd->last_data_urb = urb;
282                 return;
283         }
284
285         status_urb = urb;
286         data_urb   = sd->last_data_urb;
287         sd->last_data_urb = NULL;
288
289         if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
290                 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
291                 goto resubmit;
292         }
293
294         if (data_urb->number_of_packets != status_urb->number_of_packets) {
295                 PDEBUG(D_ERR|D_PACK,
296                        "no packets does not match, data: %d, status: %d",
297                        data_urb->number_of_packets,
298                        status_urb->number_of_packets);
299                 goto resubmit;
300         }
301
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                         PDEBUG(D_ERR|D_PACK,
306                                "pkt %d data-status %d, status-status %d", i,
307                                data_urb->iso_frame_desc[i].status,
308                                status_urb->iso_frame_desc[i].status);
309                         gspca_dev->last_packet_type = DISCARD_PACKET;
310                         continue;
311                 }
312
313                 if (status_urb->iso_frame_desc[i].actual_length != 1) {
314                         PDEBUG(D_ERR|D_PACK,
315                                "bad status packet length %d",
316                                status_urb->iso_frame_desc[i].actual_length);
317                         gspca_dev->last_packet_type = DISCARD_PACKET;
318                         continue;
319                 }
320
321                 st = *((u8 *)status_urb->transfer_buffer
322                                 + status_urb->iso_frame_desc[i].offset);
323
324                 data = (u8 *)data_urb->transfer_buffer
325                                 + data_urb->iso_frame_desc[i].offset;
326
327                 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
328                  * otherwise:
329                  * bit 0 0: keep packet
330                  *       1: drop packet (padding data)
331                  *
332                  * bit 4 0 button not clicked
333                  *       1 button clicked
334                  * button is used to `take a picture' (in software)
335                  */
336                 if (st & 0x80) {
337                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
338                         gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
339                 } else {
340 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
341                         u8 button_state = st & 0x40 ? 1 : 0;
342                         if (sd->snapshot_pressed != button_state) {
343                                 input_report_key(gspca_dev->input_dev,
344                                                  KEY_CAMERA,
345                                                  button_state);
346                                 input_sync(gspca_dev->input_dev);
347                                 sd->snapshot_pressed = button_state;
348                         }
349 #endif
350                         if (st & 0x01)
351                                 continue;
352                 }
353                 gspca_frame_add(gspca_dev, INTER_PACKET, data,
354                                 data_urb->iso_frame_desc[i].actual_length);
355         }
356
357 resubmit:
358         if (data_urb) {
359                 st = usb_submit_urb(data_urb, GFP_ATOMIC);
360                 if (st < 0)
361                         PDEBUG(D_ERR|D_PACK,
362                                "usb_submit_urb(data_urb) ret %d", st);
363         }
364         st = usb_submit_urb(status_urb, GFP_ATOMIC);
365         if (st < 0)
366                 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
367 }
368
369 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
370 {
371         struct gspca_dev *gspca_dev =
372                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
373
374         gspca_dev->usb_err = 0;
375
376         if (!gspca_dev->streaming)
377                 return 0;
378
379         switch (ctrl->id) {
380         case V4L2_CID_BRIGHTNESS:
381                 konica_stream_off(gspca_dev);
382                 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
383                 konica_stream_on(gspca_dev);
384                 break;
385         case V4L2_CID_CONTRAST:
386                 konica_stream_off(gspca_dev);
387                 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
388                 konica_stream_on(gspca_dev);
389                 break;
390         case V4L2_CID_SATURATION:
391                 konica_stream_off(gspca_dev);
392                 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
393                 konica_stream_on(gspca_dev);
394                 break;
395         case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
396                 konica_stream_off(gspca_dev);
397                 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
398                 konica_stream_on(gspca_dev);
399                 break;
400         case V4L2_CID_SHARPNESS:
401                 konica_stream_off(gspca_dev);
402                 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
403                 konica_stream_on(gspca_dev);
404                 break;
405         }
406         return gspca_dev->usb_err;
407 }
408
409 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
410         .s_ctrl = sd_s_ctrl,
411 };
412
413 static int sd_init_controls(struct gspca_dev *gspca_dev)
414 {
415         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
416
417         gspca_dev->vdev.ctrl_handler = hdl;
418         v4l2_ctrl_handler_init(hdl, 5);
419         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
420                         V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
421         /* Needs to be verified */
422         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
423                         V4L2_CID_CONTRAST, 0, 9, 1, 4);
424         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
425                         V4L2_CID_SATURATION, 0, 9, 1, 4);
426         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
427                         V4L2_CID_WHITE_BALANCE_TEMPERATURE,
428                         0, 33, 1, 25);
429         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
430                         V4L2_CID_SHARPNESS, 0, 9, 1, 4);
431
432         if (hdl->error) {
433                 pr_err("Could not initialize controls\n");
434                 return hdl->error;
435         }
436         return 0;
437 }
438
439 /* sub-driver description */
440 static const struct sd_desc sd_desc = {
441         .name = MODULE_NAME,
442         .config = sd_config,
443         .init = sd_init,
444         .init_controls = sd_init_controls,
445         .start = sd_start,
446         .stopN = sd_stopN,
447 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
448         .other_input = 1,
449 #endif
450 };
451
452 /* -- module initialisation -- */
453 static const struct usb_device_id device_table[] = {
454         {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
455         {}
456 };
457 MODULE_DEVICE_TABLE(usb, device_table);
458
459 /* -- device connect -- */
460 static int sd_probe(struct usb_interface *intf,
461                         const struct usb_device_id *id)
462 {
463         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
464                                 THIS_MODULE);
465 }
466
467 static struct usb_driver sd_driver = {
468         .name = MODULE_NAME,
469         .id_table = device_table,
470         .probe = sd_probe,
471         .disconnect = gspca_disconnect,
472 #ifdef CONFIG_PM
473         .suspend = gspca_suspend,
474         .resume = gspca_resume,
475 #endif
476 };
477
478 module_usb_driver(sd_driver);