Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Jeilinj subdriver |
| 3 | * |
| 4 | * Supports some Jeilin dual-mode cameras which use bulk transport and |
| 5 | * download raw JPEG data. |
| 6 | * |
| 7 | * Copyright (C) 2009 Theodore Kilgore |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 8 | * |
| 9 | * Sportscam DV15 support and control settings are |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 10 | * Copyright (C) 2011 Patrice Chotard |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 |
| 25 | */ |
| 26 | |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 27 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 28 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 29 | #define MODULE_NAME "jeilinj" |
| 30 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 32 | #include "gspca.h" |
| 33 | #include "jpeg.h" |
| 34 | |
| 35 | MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>"); |
| 36 | MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver"); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
| 39 | /* Default timeouts, in ms */ |
| 40 | #define JEILINJ_CMD_TIMEOUT 500 |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 41 | #define JEILINJ_CMD_DELAY 160 |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 42 | #define JEILINJ_DATA_TIMEOUT 1000 |
| 43 | |
| 44 | /* Maximum transfer size to use. */ |
| 45 | #define JEILINJ_MAX_TRANSFER 0x200 |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 46 | #define FRAME_HEADER_LEN 0x10 |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 47 | #define FRAME_START 0xFFFFFFFF |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 48 | |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 49 | enum { |
| 50 | SAKAR_57379, |
| 51 | SPORTSCAM_DV15, |
| 52 | }; |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 53 | |
| 54 | #define CAMQUALITY_MIN 0 /* highest cam quality */ |
| 55 | #define CAMQUALITY_MAX 97 /* lowest cam quality */ |
| 56 | |
| 57 | enum e_ctrl { |
| 58 | LIGHTFREQ, |
| 59 | AUTOGAIN, |
| 60 | RED, |
| 61 | GREEN, |
| 62 | BLUE, |
| 63 | NCTRLS /* number of controls */ |
| 64 | }; |
| 65 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 66 | /* Structure to hold all of our device specific stuff */ |
| 67 | struct sd { |
| 68 | struct gspca_dev gspca_dev; /* !! must be the first item */ |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 69 | struct gspca_ctrl ctrls[NCTRLS]; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 70 | int blocks_left; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 71 | const struct v4l2_pix_format *cap_mode; |
| 72 | /* Driver stuff */ |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 73 | u8 type; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 74 | u8 quality; /* image quality */ |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 75 | #define QUALITY_MIN 35 |
| 76 | #define QUALITY_MAX 85 |
| 77 | #define QUALITY_DEF 85 |
Jean-François Moine | 9a731a3 | 2010-06-04 05:26:42 -0300 | [diff] [blame] | 78 | u8 jpeg_hdr[JPEG_HDR_SZ]; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 79 | }; |
| 80 | |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 81 | struct jlj_command { |
| 82 | unsigned char instruction[2]; |
| 83 | unsigned char ack_wanted; |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 84 | unsigned char delay; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 85 | }; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 86 | |
| 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, |
Patrice Chotard | 6f8efcf | 2011-04-18 17:39:38 -0300 | [diff] [blame] | 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, |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 98 | .priv = 0} |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * cam uses endpoint 0x03 to send commands, 0x84 for read commands, |
| 103 | * and 0x82 for bulk transfer. |
| 104 | */ |
| 105 | |
| 106 | /* All commands are two bytes only */ |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 107 | static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command) |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 108 | { |
| 109 | int retval; |
| 110 | |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 111 | if (gspca_dev->usb_err < 0) |
| 112 | return; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 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); |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 117 | if (retval < 0) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 118 | pr_err("command write [%02x] error %d\n", |
| 119 | gspca_dev->usb_buf[0], retval); |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 120 | gspca_dev->usb_err = retval; |
| 121 | } |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Responses are one byte only */ |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 125 | static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char response) |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 126 | { |
| 127 | int retval; |
| 128 | |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 129 | if (gspca_dev->usb_err < 0) |
| 130 | return; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 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]; |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 135 | if (retval < 0) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 136 | pr_err("read command [%02x] error %d\n", |
| 137 | gspca_dev->usb_buf[0], retval); |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 138 | gspca_dev->usb_err = retval; |
| 139 | } |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 140 | } |
| 141 | |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 142 | static void setfreq(struct gspca_dev *gspca_dev) |
| 143 | { |
| 144 | struct sd *sd = (struct sd *) gspca_dev; |
| 145 | u8 freq_commands[][2] = { |
| 146 | {0x71, 0x80}, |
| 147 | {0x70, 0x07} |
| 148 | }; |
| 149 | |
| 150 | freq_commands[0][1] |= (sd->ctrls[LIGHTFREQ].val >> 1); |
| 151 | |
| 152 | jlj_write2(gspca_dev, freq_commands[0]); |
| 153 | jlj_write2(gspca_dev, freq_commands[1]); |
| 154 | } |
| 155 | |
| 156 | static void setcamquality(struct gspca_dev *gspca_dev) |
| 157 | { |
| 158 | struct sd *sd = (struct sd *) gspca_dev; |
| 159 | u8 quality_commands[][2] = { |
| 160 | {0x71, 0x1E}, |
| 161 | {0x70, 0x06} |
| 162 | }; |
| 163 | u8 camquality; |
| 164 | |
| 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; |
| 169 | |
| 170 | jlj_write2(gspca_dev, quality_commands[0]); |
| 171 | jlj_write2(gspca_dev, quality_commands[1]); |
| 172 | } |
| 173 | |
| 174 | static void setautogain(struct gspca_dev *gspca_dev) |
| 175 | { |
| 176 | struct sd *sd = (struct sd *) gspca_dev; |
| 177 | u8 autogain_commands[][2] = { |
| 178 | {0x94, 0x02}, |
| 179 | {0xcf, 0x00} |
| 180 | }; |
| 181 | |
| 182 | autogain_commands[1][1] = (sd->ctrls[AUTOGAIN].val << 4); |
| 183 | |
| 184 | jlj_write2(gspca_dev, autogain_commands[0]); |
| 185 | jlj_write2(gspca_dev, autogain_commands[1]); |
| 186 | } |
| 187 | |
| 188 | static void setred(struct gspca_dev *gspca_dev) |
| 189 | { |
| 190 | struct sd *sd = (struct sd *) gspca_dev; |
| 191 | u8 setred_commands[][2] = { |
| 192 | {0x94, 0x02}, |
| 193 | {0xe6, 0x00} |
| 194 | }; |
| 195 | |
| 196 | setred_commands[1][1] = sd->ctrls[RED].val; |
| 197 | |
| 198 | jlj_write2(gspca_dev, setred_commands[0]); |
| 199 | jlj_write2(gspca_dev, setred_commands[1]); |
| 200 | } |
| 201 | |
| 202 | static void setgreen(struct gspca_dev *gspca_dev) |
| 203 | { |
| 204 | struct sd *sd = (struct sd *) gspca_dev; |
| 205 | u8 setgreen_commands[][2] = { |
| 206 | {0x94, 0x02}, |
| 207 | {0xe7, 0x00} |
| 208 | }; |
| 209 | |
| 210 | setgreen_commands[1][1] = sd->ctrls[GREEN].val; |
| 211 | |
| 212 | jlj_write2(gspca_dev, setgreen_commands[0]); |
| 213 | jlj_write2(gspca_dev, setgreen_commands[1]); |
| 214 | } |
| 215 | |
| 216 | static void setblue(struct gspca_dev *gspca_dev) |
| 217 | { |
| 218 | struct sd *sd = (struct sd *) gspca_dev; |
| 219 | u8 setblue_commands[][2] = { |
| 220 | {0x94, 0x02}, |
| 221 | {0xe9, 0x00} |
| 222 | }; |
| 223 | |
| 224 | setblue_commands[1][1] = sd->ctrls[BLUE].val; |
| 225 | |
| 226 | jlj_write2(gspca_dev, setblue_commands[0]); |
| 227 | jlj_write2(gspca_dev, setblue_commands[1]); |
| 228 | } |
| 229 | |
| 230 | static const struct ctrl sd_ctrls[NCTRLS] = { |
| 231 | [LIGHTFREQ] = { |
| 232 | { |
| 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, |
| 240 | }, |
| 241 | .set_control = setfreq |
| 242 | }, |
| 243 | [AUTOGAIN] = { |
| 244 | { |
| 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, |
| 253 | }, |
| 254 | .set_control = setautogain |
| 255 | }, |
| 256 | [RED] = { |
| 257 | { |
| 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, |
| 266 | }, |
| 267 | .set_control = setred |
| 268 | }, |
| 269 | |
| 270 | [GREEN] = { |
| 271 | { |
| 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, |
| 280 | }, |
| 281 | .set_control = setgreen |
| 282 | }, |
| 283 | [BLUE] = { |
| 284 | { |
| 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, |
| 293 | }, |
| 294 | .set_control = setblue |
| 295 | }, |
| 296 | }; |
| 297 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 298 | static int jlj_start(struct gspca_dev *gspca_dev) |
| 299 | { |
| 300 | int i; |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 301 | int start_commands_size; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 302 | u8 response = 0xff; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 303 | struct sd *sd = (struct sd *) gspca_dev; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 304 | struct jlj_command start_commands[] = { |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 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}, |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 328 | {{0xaa, 0x00}, 0, 0} |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 329 | }; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 330 | |
| 331 | sd->blocks_left = 0; |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 332 | /* Under Windows, USB spy shows that only the 9 first start |
| 333 | * commands are used for SPORTSCAM_DV15 webcam |
| 334 | */ |
| 335 | if (sd->type == SPORTSCAM_DV15) |
| 336 | start_commands_size = SPORTSCAM_DV15_CMD_SIZE; |
| 337 | else |
| 338 | start_commands_size = ARRAY_SIZE(start_commands); |
| 339 | |
| 340 | for (i = 0; i < start_commands_size; i++) { |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 341 | jlj_write2(gspca_dev, start_commands[i].instruction); |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 342 | if (start_commands[i].delay) |
| 343 | msleep(start_commands[i].delay); |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 344 | if (start_commands[i].ack_wanted) |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 345 | jlj_read1(gspca_dev, response); |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 346 | } |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 347 | setcamquality(gspca_dev); |
| 348 | msleep(2); |
| 349 | setfreq(gspca_dev); |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 350 | if (gspca_dev->usb_err < 0) |
| 351 | PDEBUG(D_ERR, "Start streaming command failed"); |
| 352 | return gspca_dev->usb_err; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 353 | } |
| 354 | |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 355 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, |
| 356 | u8 *data, int len) |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 357 | { |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 358 | struct sd *sd = (struct sd *) gspca_dev; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 359 | int packet_type; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 360 | u32 header_marker; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 361 | |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 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; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 367 | } |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 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); |
Hans de Goede | 6ca3f25 | 2009-10-09 03:58:35 -0300 | [diff] [blame] | 373 | /* Start a new frame, and add the JPEG header, first thing */ |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 374 | gspca_frame_add(gspca_dev, FIRST_PACKET, |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 375 | sd->jpeg_hdr, JPEG_HDR_SZ); |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 376 | /* Toss line 0 of data block 0, keep the rest. */ |
| 377 | gspca_frame_add(gspca_dev, INTER_PACKET, |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 378 | data + FRAME_HEADER_LEN, |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 379 | JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN); |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 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; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 396 | } |
| 397 | |
| 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) |
| 401 | { |
| 402 | struct cam *cam = &gspca_dev->cam; |
| 403 | struct sd *dev = (struct sd *) gspca_dev; |
| 404 | |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 405 | dev->type = id->driver_info; |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 406 | gspca_dev->cam.ctrls = dev->ctrls; |
| 407 | dev->quality = QUALITY_DEF; |
Jean-François Moine | 7d84a17 | 2011-07-03 07:27:24 -0300 | [diff] [blame] | 408 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 409 | cam->cam_mode = jlj_mode; |
Patrice Chotard | 6f8efcf | 2011-04-18 17:39:38 -0300 | [diff] [blame] | 410 | cam->nmodes = ARRAY_SIZE(jlj_mode); |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 411 | cam->bulk = 1; |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 412 | cam->bulk_nurbs = 1; |
| 413 | cam->bulk_size = JEILINJ_MAX_TRANSFER; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 417 | static void sd_stopN(struct gspca_dev *gspca_dev) |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 418 | { |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 419 | int i; |
| 420 | u8 *buf; |
Jean-François Moine | 7d84a17 | 2011-07-03 07:27:24 -0300 | [diff] [blame] | 421 | static u8 stop_commands[][2] = { |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 422 | {0x71, 0x00}, |
| 423 | {0x70, 0x09}, |
| 424 | {0x71, 0x80}, |
| 425 | {0x70, 0x05} |
| 426 | }; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 427 | |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 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); |
| 435 | |
| 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++; |
| 442 | |
| 443 | if (i != (JEILINJ_MAX_TRANSFER - 1)) |
| 444 | /* last remaining block found */ |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | for (i = 0; i < ARRAY_SIZE(stop_commands); i++) |
| 449 | jlj_write2(gspca_dev, stop_commands[i]); |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* this function is called at probe and resume time */ |
| 453 | static int sd_init(struct gspca_dev *gspca_dev) |
| 454 | { |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 455 | return gspca_dev->usb_err; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Set up for getting frames. */ |
| 459 | static int sd_start(struct gspca_dev *gspca_dev) |
| 460 | { |
| 461 | struct sd *dev = (struct sd *) gspca_dev; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 462 | |
| 463 | /* create the JPEG header */ |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 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); |
Patrice Chotard | 6f8efcf | 2011-04-18 17:39:38 -0300 | [diff] [blame] | 467 | PDEBUG(D_STREAM, "Start streaming at %dx%d", |
| 468 | gspca_dev->height, gspca_dev->width); |
Patrice Chotard | 8715b16 | 2011-04-18 17:38:37 -0300 | [diff] [blame] | 469 | jlj_start(gspca_dev); |
| 470 | return gspca_dev->usb_err; |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* Table of supported USB devices */ |
Jean-François Moine | 95c967c | 2011-01-13 05:20:29 -0300 | [diff] [blame] | 474 | static const struct usb_device_id device_table[] = { |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 475 | {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379}, |
| 476 | {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15}, |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 477 | {} |
| 478 | }; |
| 479 | |
| 480 | MODULE_DEVICE_TABLE(usb, device_table); |
| 481 | |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 482 | static int sd_querymenu(struct gspca_dev *gspca_dev, |
| 483 | struct v4l2_querymenu *menu) |
| 484 | { |
| 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; |
| 497 | } |
| 498 | break; |
| 499 | } |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | |
| 503 | static int sd_set_jcomp(struct gspca_dev *gspca_dev, |
| 504 | struct v4l2_jpegcompression *jcomp) |
| 505 | { |
| 506 | struct sd *sd = (struct sd *) gspca_dev; |
| 507 | |
| 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); |
| 517 | } |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int sd_get_jcomp(struct gspca_dev *gspca_dev, |
| 522 | struct v4l2_jpegcompression *jcomp) |
| 523 | { |
| 524 | struct sd *sd = (struct sd *) gspca_dev; |
| 525 | |
| 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; |
| 531 | } |
| 532 | |
| 533 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 534 | /* sub-driver description */ |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 535 | static const struct sd_desc sd_desc_sakar_57379 = { |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 536 | .name = MODULE_NAME, |
| 537 | .config = sd_config, |
| 538 | .init = sd_init, |
| 539 | .start = sd_start, |
Patrice Chotard | c3d8692 | 2011-04-18 17:37:06 -0300 | [diff] [blame] | 540 | .stopN = sd_stopN, |
| 541 | .pkt_scan = sd_pkt_scan, |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 542 | }; |
| 543 | |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 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, |
Patrice Chotard | 5396e62 | 2011-04-18 17:42:06 -0300 | [diff] [blame] | 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, |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | static const struct sd_desc *sd_desc[2] = { |
| 560 | &sd_desc_sakar_57379, |
| 561 | &sd_desc_sportscam_dv15 |
| 562 | }; |
| 563 | |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 564 | /* -- device connect -- */ |
| 565 | static int sd_probe(struct usb_interface *intf, |
| 566 | const struct usb_device_id *id) |
| 567 | { |
| 568 | return gspca_dev_probe(intf, id, |
Patrice Chotard | 713b466 | 2011-04-18 17:40:54 -0300 | [diff] [blame] | 569 | sd_desc[id->driver_info], |
Theodore Kilgore | 3040b04 | 2009-08-03 04:13:23 -0300 | [diff] [blame] | 570 | sizeof(struct sd), |
| 571 | THIS_MODULE); |
| 572 | } |
| 573 | |
| 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 |
| 583 | }; |
| 584 | |
Greg Kroah-Hartman | ecb3b2b | 2011-11-18 09:46:12 -0800 | [diff] [blame] | 585 | module_usb_driver(sd_driver); |