Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / media / video / gspca / jeilinj.c
blob5ab3f7e12760dcf4c8490fb77d9cdeac46ec9c2e
1 /*
2 * Jeilinj subdriver
4 * Supports some Jeilin dual-mode cameras which use bulk transport and
5 * download raw JPEG data.
7 * Copyright (C) 2009 Theodore Kilgore
9 * Sportscam DV15 support and control settings are
10 * Copyright (C) 2011 Patrice Chotard
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #define MODULE_NAME "jeilinj"
31 #include <linux/slab.h>
32 #include "gspca.h"
33 #include "jpeg.h"
35 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
36 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
37 MODULE_LICENSE("GPL");
39 /* Default timeouts, in ms */
40 #define JEILINJ_CMD_TIMEOUT 500
41 #define JEILINJ_CMD_DELAY 160
42 #define JEILINJ_DATA_TIMEOUT 1000
44 /* Maximum transfer size to use. */
45 #define JEILINJ_MAX_TRANSFER 0x200
46 #define FRAME_HEADER_LEN 0x10
47 #define FRAME_START 0xFFFFFFFF
49 enum {
50 SAKAR_57379,
51 SPORTSCAM_DV15,
54 #define CAMQUALITY_MIN 0 /* highest cam quality */
55 #define CAMQUALITY_MAX 97 /* lowest cam quality */
57 enum e_ctrl {
58 LIGHTFREQ,
59 AUTOGAIN,
60 RED,
61 GREEN,
62 BLUE,
63 NCTRLS /* number of controls */
66 /* Structure to hold all of our device specific stuff */
67 struct sd {
68 struct gspca_dev gspca_dev; /* !! must be the first item */
69 struct gspca_ctrl ctrls[NCTRLS];
70 int blocks_left;
71 const struct v4l2_pix_format *cap_mode;
72 /* Driver stuff */
73 u8 type;
74 u8 quality; /* image quality */
75 #define QUALITY_MIN 35
76 #define QUALITY_MAX 85
77 #define QUALITY_DEF 85
78 u8 jpeg_hdr[JPEG_HDR_SZ];
81 struct jlj_command {
82 unsigned char instruction[2];
83 unsigned char ack_wanted;
84 unsigned char delay;
87 /* AFAICT these cameras will only do 320x240. */
88 static struct v4l2_pix_format jlj_mode[] = {
89 { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
90 .bytesperline = 320,
91 .sizeimage = 320 * 240,
92 .colorspace = V4L2_COLORSPACE_JPEG,
93 .priv = 0},
94 { 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
95 .bytesperline = 640,
96 .sizeimage = 640 * 480,
97 .colorspace = V4L2_COLORSPACE_JPEG,
98 .priv = 0}
102 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
103 * and 0x82 for bulk transfer.
106 /* All commands are two bytes only */
107 static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
109 int retval;
111 if (gspca_dev->usb_err < 0)
112 return;
113 memcpy(gspca_dev->usb_buf, command, 2);
114 retval = usb_bulk_msg(gspca_dev->dev,
115 usb_sndbulkpipe(gspca_dev->dev, 3),
116 gspca_dev->usb_buf, 2, NULL, 500);
117 if (retval < 0) {
118 pr_err("command write [%02x] error %d\n",
119 gspca_dev->usb_buf[0], retval);
120 gspca_dev->usb_err = retval;
124 /* Responses are one byte only */
125 static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
127 int retval;
129 if (gspca_dev->usb_err < 0)
130 return;
131 retval = usb_bulk_msg(gspca_dev->dev,
132 usb_rcvbulkpipe(gspca_dev->dev, 0x84),
133 gspca_dev->usb_buf, 1, NULL, 500);
134 response = gspca_dev->usb_buf[0];
135 if (retval < 0) {
136 pr_err("read command [%02x] error %d\n",
137 gspca_dev->usb_buf[0], retval);
138 gspca_dev->usb_err = retval;
142 static void setfreq(struct gspca_dev *gspca_dev)
144 struct sd *sd = (struct sd *) gspca_dev;
145 u8 freq_commands[][2] = {
146 {0x71, 0x80},
147 {0x70, 0x07}
150 freq_commands[0][1] |= (sd->ctrls[LIGHTFREQ].val >> 1);
152 jlj_write2(gspca_dev, freq_commands[0]);
153 jlj_write2(gspca_dev, freq_commands[1]);
156 static void setcamquality(struct gspca_dev *gspca_dev)
158 struct sd *sd = (struct sd *) gspca_dev;
159 u8 quality_commands[][2] = {
160 {0x71, 0x1E},
161 {0x70, 0x06}
163 u8 camquality;
165 /* adapt camera quality from jpeg quality */
166 camquality = ((QUALITY_MAX - sd->quality) * CAMQUALITY_MAX)
167 / (QUALITY_MAX - QUALITY_MIN);
168 quality_commands[0][1] += camquality;
170 jlj_write2(gspca_dev, quality_commands[0]);
171 jlj_write2(gspca_dev, quality_commands[1]);
174 static void setautogain(struct gspca_dev *gspca_dev)
176 struct sd *sd = (struct sd *) gspca_dev;
177 u8 autogain_commands[][2] = {
178 {0x94, 0x02},
179 {0xcf, 0x00}
182 autogain_commands[1][1] = (sd->ctrls[AUTOGAIN].val << 4);
184 jlj_write2(gspca_dev, autogain_commands[0]);
185 jlj_write2(gspca_dev, autogain_commands[1]);
188 static void setred(struct gspca_dev *gspca_dev)
190 struct sd *sd = (struct sd *) gspca_dev;
191 u8 setred_commands[][2] = {
192 {0x94, 0x02},
193 {0xe6, 0x00}
196 setred_commands[1][1] = sd->ctrls[RED].val;
198 jlj_write2(gspca_dev, setred_commands[0]);
199 jlj_write2(gspca_dev, setred_commands[1]);
202 static void setgreen(struct gspca_dev *gspca_dev)
204 struct sd *sd = (struct sd *) gspca_dev;
205 u8 setgreen_commands[][2] = {
206 {0x94, 0x02},
207 {0xe7, 0x00}
210 setgreen_commands[1][1] = sd->ctrls[GREEN].val;
212 jlj_write2(gspca_dev, setgreen_commands[0]);
213 jlj_write2(gspca_dev, setgreen_commands[1]);
216 static void setblue(struct gspca_dev *gspca_dev)
218 struct sd *sd = (struct sd *) gspca_dev;
219 u8 setblue_commands[][2] = {
220 {0x94, 0x02},
221 {0xe9, 0x00}
224 setblue_commands[1][1] = sd->ctrls[BLUE].val;
226 jlj_write2(gspca_dev, setblue_commands[0]);
227 jlj_write2(gspca_dev, setblue_commands[1]);
230 static const struct ctrl sd_ctrls[NCTRLS] = {
231 [LIGHTFREQ] = {
233 .id = V4L2_CID_POWER_LINE_FREQUENCY,
234 .type = V4L2_CTRL_TYPE_MENU,
235 .name = "Light frequency filter",
236 .minimum = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED, /* 1 */
237 .maximum = V4L2_CID_POWER_LINE_FREQUENCY_60HZ, /* 2 */
238 .step = 1,
239 .default_value = V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
241 .set_control = setfreq
243 [AUTOGAIN] = {
245 .id = V4L2_CID_AUTOGAIN,
246 .type = V4L2_CTRL_TYPE_INTEGER,
247 .name = "Automatic Gain (and Exposure)",
248 .minimum = 0,
249 .maximum = 3,
250 .step = 1,
251 #define AUTOGAIN_DEF 0
252 .default_value = AUTOGAIN_DEF,
254 .set_control = setautogain
256 [RED] = {
258 .id = V4L2_CID_RED_BALANCE,
259 .type = V4L2_CTRL_TYPE_INTEGER,
260 .name = "red balance",
261 .minimum = 0,
262 .maximum = 3,
263 .step = 1,
264 #define RED_BALANCE_DEF 2
265 .default_value = RED_BALANCE_DEF,
267 .set_control = setred
270 [GREEN] = {
272 .id = V4L2_CID_GAIN,
273 .type = V4L2_CTRL_TYPE_INTEGER,
274 .name = "green balance",
275 .minimum = 0,
276 .maximum = 3,
277 .step = 1,
278 #define GREEN_BALANCE_DEF 2
279 .default_value = GREEN_BALANCE_DEF,
281 .set_control = setgreen
283 [BLUE] = {
285 .id = V4L2_CID_BLUE_BALANCE,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 .name = "blue balance",
288 .minimum = 0,
289 .maximum = 3,
290 .step = 1,
291 #define BLUE_BALANCE_DEF 2
292 .default_value = BLUE_BALANCE_DEF,
294 .set_control = setblue
298 static int jlj_start(struct gspca_dev *gspca_dev)
300 int i;
301 int start_commands_size;
302 u8 response = 0xff;
303 struct sd *sd = (struct sd *) gspca_dev;
304 struct jlj_command start_commands[] = {
305 {{0x71, 0x81}, 0, 0},
306 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
307 {{0x95, 0x70}, 1, 0},
308 {{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
309 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
310 {{0x95, 0x70}, 1, 0},
311 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
312 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
313 {{0x95, 0x70}, 1, 0},
314 #define SPORTSCAM_DV15_CMD_SIZE 9
315 {{0x94, 0x02}, 0, 0},
316 {{0xde, 0x24}, 0, 0},
317 {{0x94, 0x02}, 0, 0},
318 {{0xdd, 0xf0}, 0, 0},
319 {{0x94, 0x02}, 0, 0},
320 {{0xe3, 0x2c}, 0, 0},
321 {{0x94, 0x02}, 0, 0},
322 {{0xe4, 0x00}, 0, 0},
323 {{0x94, 0x02}, 0, 0},
324 {{0xe5, 0x00}, 0, 0},
325 {{0x94, 0x02}, 0, 0},
326 {{0xe6, 0x2c}, 0, 0},
327 {{0x94, 0x03}, 0, 0},
328 {{0xaa, 0x00}, 0, 0}
331 sd->blocks_left = 0;
332 /* Under Windows, USB spy shows that only the 9 first start
333 * commands are used for SPORTSCAM_DV15 webcam
335 if (sd->type == SPORTSCAM_DV15)
336 start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
337 else
338 start_commands_size = ARRAY_SIZE(start_commands);
340 for (i = 0; i < start_commands_size; i++) {
341 jlj_write2(gspca_dev, start_commands[i].instruction);
342 if (start_commands[i].delay)
343 msleep(start_commands[i].delay);
344 if (start_commands[i].ack_wanted)
345 jlj_read1(gspca_dev, response);
347 setcamquality(gspca_dev);
348 msleep(2);
349 setfreq(gspca_dev);
350 if (gspca_dev->usb_err < 0)
351 PDEBUG(D_ERR, "Start streaming command failed");
352 return gspca_dev->usb_err;
355 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
356 u8 *data, int len)
358 struct sd *sd = (struct sd *) gspca_dev;
359 int packet_type;
360 u32 header_marker;
362 PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
363 len, JEILINJ_MAX_TRANSFER);
364 if (len != JEILINJ_MAX_TRANSFER) {
365 PDEBUG(D_PACK, "bad length");
366 goto discard;
368 /* check if it's start of frame */
369 header_marker = ((u32 *)data)[0];
370 if (header_marker == FRAME_START) {
371 sd->blocks_left = data[0x0a] - 1;
372 PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
373 /* Start a new frame, and add the JPEG header, first thing */
374 gspca_frame_add(gspca_dev, FIRST_PACKET,
375 sd->jpeg_hdr, JPEG_HDR_SZ);
376 /* Toss line 0 of data block 0, keep the rest. */
377 gspca_frame_add(gspca_dev, INTER_PACKET,
378 data + FRAME_HEADER_LEN,
379 JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
380 } else if (sd->blocks_left > 0) {
381 PDEBUG(D_STREAM, "%d blocks remaining for frame",
382 sd->blocks_left);
383 sd->blocks_left -= 1;
384 if (sd->blocks_left == 0)
385 packet_type = LAST_PACKET;
386 else
387 packet_type = INTER_PACKET;
388 gspca_frame_add(gspca_dev, packet_type,
389 data, JEILINJ_MAX_TRANSFER);
390 } else
391 goto discard;
392 return;
393 discard:
394 /* Discard data until a new frame starts. */
395 gspca_dev->last_packet_type = DISCARD_PACKET;
398 /* This function is called at probe time just before sd_init */
399 static int sd_config(struct gspca_dev *gspca_dev,
400 const struct usb_device_id *id)
402 struct cam *cam = &gspca_dev->cam;
403 struct sd *dev = (struct sd *) gspca_dev;
405 dev->type = id->driver_info;
406 gspca_dev->cam.ctrls = dev->ctrls;
407 dev->quality = QUALITY_DEF;
409 cam->cam_mode = jlj_mode;
410 cam->nmodes = ARRAY_SIZE(jlj_mode);
411 cam->bulk = 1;
412 cam->bulk_nurbs = 1;
413 cam->bulk_size = JEILINJ_MAX_TRANSFER;
414 return 0;
417 static void sd_stopN(struct gspca_dev *gspca_dev)
419 int i;
420 u8 *buf;
421 static u8 stop_commands[][2] = {
422 {0x71, 0x00},
423 {0x70, 0x09},
424 {0x71, 0x80},
425 {0x70, 0x05}
428 for (;;) {
429 /* get the image remaining blocks */
430 usb_bulk_msg(gspca_dev->dev,
431 gspca_dev->urb[0]->pipe,
432 gspca_dev->urb[0]->transfer_buffer,
433 JEILINJ_MAX_TRANSFER, NULL,
434 JEILINJ_DATA_TIMEOUT);
436 /* search for 0xff 0xd9 (EOF for JPEG) */
437 i = 0;
438 buf = gspca_dev->urb[0]->transfer_buffer;
439 while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
440 ((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
441 i++;
443 if (i != (JEILINJ_MAX_TRANSFER - 1))
444 /* last remaining block found */
445 break;
448 for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
449 jlj_write2(gspca_dev, stop_commands[i]);
452 /* this function is called at probe and resume time */
453 static int sd_init(struct gspca_dev *gspca_dev)
455 return gspca_dev->usb_err;
458 /* Set up for getting frames. */
459 static int sd_start(struct gspca_dev *gspca_dev)
461 struct sd *dev = (struct sd *) gspca_dev;
463 /* create the JPEG header */
464 jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
465 0x21); /* JPEG 422 */
466 jpeg_set_qual(dev->jpeg_hdr, dev->quality);
467 PDEBUG(D_STREAM, "Start streaming at %dx%d",
468 gspca_dev->height, gspca_dev->width);
469 jlj_start(gspca_dev);
470 return gspca_dev->usb_err;
473 /* Table of supported USB devices */
474 static const struct usb_device_id device_table[] = {
475 {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
476 {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
480 MODULE_DEVICE_TABLE(usb, device_table);
482 static int sd_querymenu(struct gspca_dev *gspca_dev,
483 struct v4l2_querymenu *menu)
485 switch (menu->id) {
486 case V4L2_CID_POWER_LINE_FREQUENCY:
487 switch (menu->index) {
488 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
489 strcpy((char *) menu->name, "disable");
490 return 0;
491 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
492 strcpy((char *) menu->name, "50 Hz");
493 return 0;
494 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
495 strcpy((char *) menu->name, "60 Hz");
496 return 0;
498 break;
500 return -EINVAL;
503 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
504 struct v4l2_jpegcompression *jcomp)
506 struct sd *sd = (struct sd *) gspca_dev;
508 if (jcomp->quality < QUALITY_MIN)
509 sd->quality = QUALITY_MIN;
510 else if (jcomp->quality > QUALITY_MAX)
511 sd->quality = QUALITY_MAX;
512 else
513 sd->quality = jcomp->quality;
514 if (gspca_dev->streaming) {
515 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
516 setcamquality(gspca_dev);
518 return 0;
521 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
522 struct v4l2_jpegcompression *jcomp)
524 struct sd *sd = (struct sd *) gspca_dev;
526 memset(jcomp, 0, sizeof *jcomp);
527 jcomp->quality = sd->quality;
528 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
529 | V4L2_JPEG_MARKER_DQT;
530 return 0;
534 /* sub-driver description */
535 static const struct sd_desc sd_desc_sakar_57379 = {
536 .name = MODULE_NAME,
537 .config = sd_config,
538 .init = sd_init,
539 .start = sd_start,
540 .stopN = sd_stopN,
541 .pkt_scan = sd_pkt_scan,
544 /* sub-driver description */
545 static const struct sd_desc sd_desc_sportscam_dv15 = {
546 .name = MODULE_NAME,
547 .config = sd_config,
548 .init = sd_init,
549 .start = sd_start,
550 .stopN = sd_stopN,
551 .pkt_scan = sd_pkt_scan,
552 .ctrls = sd_ctrls,
553 .nctrls = ARRAY_SIZE(sd_ctrls),
554 .querymenu = sd_querymenu,
555 .get_jcomp = sd_get_jcomp,
556 .set_jcomp = sd_set_jcomp,
559 static const struct sd_desc *sd_desc[2] = {
560 &sd_desc_sakar_57379,
561 &sd_desc_sportscam_dv15
564 /* -- device connect -- */
565 static int sd_probe(struct usb_interface *intf,
566 const struct usb_device_id *id)
568 return gspca_dev_probe(intf, id,
569 sd_desc[id->driver_info],
570 sizeof(struct sd),
571 THIS_MODULE);
574 static struct usb_driver sd_driver = {
575 .name = MODULE_NAME,
576 .id_table = device_table,
577 .probe = sd_probe,
578 .disconnect = gspca_disconnect,
579 #ifdef CONFIG_PM
580 .suspend = gspca_suspend,
581 .resume = gspca_resume,
582 #endif
585 module_usb_driver(sd_driver);