blob: 19736e237b37d6a14e50141c8d24104141783f16 [file] [log] [blame]
Theodore Kilgore3040b042009-08-03 04:13:23 -03001/*
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 Chotard5396e622011-04-18 17:42:06 -03008 *
9 * Sportscam DV15 support and control settings are
Patrice Chotardc3d86922011-04-18 17:37:06 -030010 * Copyright (C) 2011 Patrice Chotard
Theodore Kilgore3040b042009-08-03 04:13:23 -030011 *
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 Perches133a9fe2011-08-21 19:56:57 -030027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Theodore Kilgore3040b042009-08-03 04:13:23 -030029#define MODULE_NAME "jeilinj"
30
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Theodore Kilgore3040b042009-08-03 04:13:23 -030032#include "gspca.h"
33#include "jpeg.h"
34
35MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
36MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
37MODULE_LICENSE("GPL");
38
39/* Default timeouts, in ms */
40#define JEILINJ_CMD_TIMEOUT 500
Patrice Chotard713b4662011-04-18 17:40:54 -030041#define JEILINJ_CMD_DELAY 160
Theodore Kilgore3040b042009-08-03 04:13:23 -030042#define JEILINJ_DATA_TIMEOUT 1000
43
44/* Maximum transfer size to use. */
45#define JEILINJ_MAX_TRANSFER 0x200
Theodore Kilgore3040b042009-08-03 04:13:23 -030046#define FRAME_HEADER_LEN 0x10
Patrice Chotardc3d86922011-04-18 17:37:06 -030047#define FRAME_START 0xFFFFFFFF
Theodore Kilgore3040b042009-08-03 04:13:23 -030048
Patrice Chotard713b4662011-04-18 17:40:54 -030049enum {
50 SAKAR_57379,
51 SPORTSCAM_DV15,
52};
Patrice Chotard5396e622011-04-18 17:42:06 -030053
54#define CAMQUALITY_MIN 0 /* highest cam quality */
55#define CAMQUALITY_MAX 97 /* lowest cam quality */
56
Theodore Kilgore3040b042009-08-03 04:13:23 -030057/* Structure to hold all of our device specific stuff */
58struct sd {
59 struct gspca_dev gspca_dev; /* !! must be the first item */
Patrice Chotardc3d86922011-04-18 17:37:06 -030060 int blocks_left;
Theodore Kilgore3040b042009-08-03 04:13:23 -030061 const struct v4l2_pix_format *cap_mode;
Hans Verkuilbfaab892012-05-14 06:30:06 -030062 struct v4l2_ctrl *freq;
63 struct v4l2_ctrl *jpegqual;
Theodore Kilgore3040b042009-08-03 04:13:23 -030064 /* Driver stuff */
Patrice Chotard713b4662011-04-18 17:40:54 -030065 u8 type;
Theodore Kilgore3040b042009-08-03 04:13:23 -030066 u8 quality; /* image quality */
Patrice Chotard5396e622011-04-18 17:42:06 -030067#define QUALITY_MIN 35
68#define QUALITY_MAX 85
69#define QUALITY_DEF 85
Jean-François Moine9a731a32010-06-04 05:26:42 -030070 u8 jpeg_hdr[JPEG_HDR_SZ];
Theodore Kilgore3040b042009-08-03 04:13:23 -030071};
72
Patrice Chotardc3d86922011-04-18 17:37:06 -030073struct jlj_command {
74 unsigned char instruction[2];
75 unsigned char ack_wanted;
Patrice Chotard713b4662011-04-18 17:40:54 -030076 unsigned char delay;
Patrice Chotardc3d86922011-04-18 17:37:06 -030077};
Theodore Kilgore3040b042009-08-03 04:13:23 -030078
79/* AFAICT these cameras will only do 320x240. */
80static struct v4l2_pix_format jlj_mode[] = {
81 { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
82 .bytesperline = 320,
83 .sizeimage = 320 * 240,
84 .colorspace = V4L2_COLORSPACE_JPEG,
Patrice Chotard6f8efcf2011-04-18 17:39:38 -030085 .priv = 0},
86 { 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
87 .bytesperline = 640,
88 .sizeimage = 640 * 480,
89 .colorspace = V4L2_COLORSPACE_JPEG,
Theodore Kilgore3040b042009-08-03 04:13:23 -030090 .priv = 0}
91};
92
93/*
94 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
95 * and 0x82 for bulk transfer.
96 */
97
98/* All commands are two bytes only */
Patrice Chotard8715b162011-04-18 17:38:37 -030099static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
Theodore Kilgore3040b042009-08-03 04:13:23 -0300100{
101 int retval;
102
Patrice Chotard8715b162011-04-18 17:38:37 -0300103 if (gspca_dev->usb_err < 0)
104 return;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300105 memcpy(gspca_dev->usb_buf, command, 2);
106 retval = usb_bulk_msg(gspca_dev->dev,
107 usb_sndbulkpipe(gspca_dev->dev, 3),
108 gspca_dev->usb_buf, 2, NULL, 500);
Patrice Chotard8715b162011-04-18 17:38:37 -0300109 if (retval < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300110 pr_err("command write [%02x] error %d\n",
111 gspca_dev->usb_buf[0], retval);
Patrice Chotard8715b162011-04-18 17:38:37 -0300112 gspca_dev->usb_err = retval;
113 }
Theodore Kilgore3040b042009-08-03 04:13:23 -0300114}
115
116/* Responses are one byte only */
Mauro Carvalho Chehab9825f372012-10-27 14:54:01 -0300117static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char *response)
Theodore Kilgore3040b042009-08-03 04:13:23 -0300118{
119 int retval;
120
Patrice Chotard8715b162011-04-18 17:38:37 -0300121 if (gspca_dev->usb_err < 0)
122 return;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300123 retval = usb_bulk_msg(gspca_dev->dev,
124 usb_rcvbulkpipe(gspca_dev->dev, 0x84),
125 gspca_dev->usb_buf, 1, NULL, 500);
Mauro Carvalho Chehab9825f372012-10-27 14:54:01 -0300126 *response = gspca_dev->usb_buf[0];
Patrice Chotard8715b162011-04-18 17:38:37 -0300127 if (retval < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300128 pr_err("read command [%02x] error %d\n",
129 gspca_dev->usb_buf[0], retval);
Patrice Chotard8715b162011-04-18 17:38:37 -0300130 gspca_dev->usb_err = retval;
131 }
Theodore Kilgore3040b042009-08-03 04:13:23 -0300132}
133
Hans Verkuilbfaab892012-05-14 06:30:06 -0300134static void setfreq(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300135{
Patrice Chotard5396e622011-04-18 17:42:06 -0300136 u8 freq_commands[][2] = {
137 {0x71, 0x80},
138 {0x70, 0x07}
139 };
140
Hans Verkuilbfaab892012-05-14 06:30:06 -0300141 freq_commands[0][1] |= val >> 1;
Patrice Chotard5396e622011-04-18 17:42:06 -0300142
143 jlj_write2(gspca_dev, freq_commands[0]);
144 jlj_write2(gspca_dev, freq_commands[1]);
145}
146
Hans Verkuilbfaab892012-05-14 06:30:06 -0300147static void setcamquality(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300148{
Patrice Chotard5396e622011-04-18 17:42:06 -0300149 u8 quality_commands[][2] = {
150 {0x71, 0x1E},
151 {0x70, 0x06}
152 };
153 u8 camquality;
154
155 /* adapt camera quality from jpeg quality */
Hans Verkuilbfaab892012-05-14 06:30:06 -0300156 camquality = ((QUALITY_MAX - val) * CAMQUALITY_MAX)
Patrice Chotard5396e622011-04-18 17:42:06 -0300157 / (QUALITY_MAX - QUALITY_MIN);
158 quality_commands[0][1] += camquality;
159
160 jlj_write2(gspca_dev, quality_commands[0]);
161 jlj_write2(gspca_dev, quality_commands[1]);
162}
163
Hans Verkuilbfaab892012-05-14 06:30:06 -0300164static void setautogain(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300165{
Patrice Chotard5396e622011-04-18 17:42:06 -0300166 u8 autogain_commands[][2] = {
167 {0x94, 0x02},
168 {0xcf, 0x00}
169 };
170
Hans Verkuilbfaab892012-05-14 06:30:06 -0300171 autogain_commands[1][1] = val << 4;
Patrice Chotard5396e622011-04-18 17:42:06 -0300172
173 jlj_write2(gspca_dev, autogain_commands[0]);
174 jlj_write2(gspca_dev, autogain_commands[1]);
175}
176
Hans Verkuilbfaab892012-05-14 06:30:06 -0300177static void setred(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300178{
Patrice Chotard5396e622011-04-18 17:42:06 -0300179 u8 setred_commands[][2] = {
180 {0x94, 0x02},
181 {0xe6, 0x00}
182 };
183
Hans Verkuilbfaab892012-05-14 06:30:06 -0300184 setred_commands[1][1] = val;
Patrice Chotard5396e622011-04-18 17:42:06 -0300185
186 jlj_write2(gspca_dev, setred_commands[0]);
187 jlj_write2(gspca_dev, setred_commands[1]);
188}
189
Hans Verkuilbfaab892012-05-14 06:30:06 -0300190static void setgreen(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300191{
Patrice Chotard5396e622011-04-18 17:42:06 -0300192 u8 setgreen_commands[][2] = {
193 {0x94, 0x02},
194 {0xe7, 0x00}
195 };
196
Hans Verkuilbfaab892012-05-14 06:30:06 -0300197 setgreen_commands[1][1] = val;
Patrice Chotard5396e622011-04-18 17:42:06 -0300198
199 jlj_write2(gspca_dev, setgreen_commands[0]);
200 jlj_write2(gspca_dev, setgreen_commands[1]);
201}
202
Hans Verkuilbfaab892012-05-14 06:30:06 -0300203static void setblue(struct gspca_dev *gspca_dev, s32 val)
Patrice Chotard5396e622011-04-18 17:42:06 -0300204{
Patrice Chotard5396e622011-04-18 17:42:06 -0300205 u8 setblue_commands[][2] = {
206 {0x94, 0x02},
207 {0xe9, 0x00}
208 };
209
Hans Verkuilbfaab892012-05-14 06:30:06 -0300210 setblue_commands[1][1] = val;
Patrice Chotard5396e622011-04-18 17:42:06 -0300211
212 jlj_write2(gspca_dev, setblue_commands[0]);
213 jlj_write2(gspca_dev, setblue_commands[1]);
214}
215
Theodore Kilgore3040b042009-08-03 04:13:23 -0300216static int jlj_start(struct gspca_dev *gspca_dev)
217{
218 int i;
Patrice Chotard713b4662011-04-18 17:40:54 -0300219 int start_commands_size;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300220 u8 response = 0xff;
Patrice Chotardc3d86922011-04-18 17:37:06 -0300221 struct sd *sd = (struct sd *) gspca_dev;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300222 struct jlj_command start_commands[] = {
Patrice Chotard713b4662011-04-18 17:40:54 -0300223 {{0x71, 0x81}, 0, 0},
224 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
225 {{0x95, 0x70}, 1, 0},
226 {{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
227 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
228 {{0x95, 0x70}, 1, 0},
229 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
230 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
231 {{0x95, 0x70}, 1, 0},
232#define SPORTSCAM_DV15_CMD_SIZE 9
233 {{0x94, 0x02}, 0, 0},
234 {{0xde, 0x24}, 0, 0},
235 {{0x94, 0x02}, 0, 0},
236 {{0xdd, 0xf0}, 0, 0},
237 {{0x94, 0x02}, 0, 0},
238 {{0xe3, 0x2c}, 0, 0},
239 {{0x94, 0x02}, 0, 0},
240 {{0xe4, 0x00}, 0, 0},
241 {{0x94, 0x02}, 0, 0},
242 {{0xe5, 0x00}, 0, 0},
243 {{0x94, 0x02}, 0, 0},
244 {{0xe6, 0x2c}, 0, 0},
245 {{0x94, 0x03}, 0, 0},
Patrice Chotard5396e622011-04-18 17:42:06 -0300246 {{0xaa, 0x00}, 0, 0}
Theodore Kilgore3040b042009-08-03 04:13:23 -0300247 };
Patrice Chotardc3d86922011-04-18 17:37:06 -0300248
249 sd->blocks_left = 0;
Patrice Chotard713b4662011-04-18 17:40:54 -0300250 /* Under Windows, USB spy shows that only the 9 first start
251 * commands are used for SPORTSCAM_DV15 webcam
252 */
253 if (sd->type == SPORTSCAM_DV15)
254 start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
255 else
256 start_commands_size = ARRAY_SIZE(start_commands);
257
258 for (i = 0; i < start_commands_size; i++) {
Patrice Chotard8715b162011-04-18 17:38:37 -0300259 jlj_write2(gspca_dev, start_commands[i].instruction);
Patrice Chotard713b4662011-04-18 17:40:54 -0300260 if (start_commands[i].delay)
261 msleep(start_commands[i].delay);
Theodore Kilgore3040b042009-08-03 04:13:23 -0300262 if (start_commands[i].ack_wanted)
Mauro Carvalho Chehab9825f372012-10-27 14:54:01 -0300263 jlj_read1(gspca_dev, &response);
Theodore Kilgore3040b042009-08-03 04:13:23 -0300264 }
Hans Verkuilbfaab892012-05-14 06:30:06 -0300265 setcamquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
Patrice Chotard5396e622011-04-18 17:42:06 -0300266 msleep(2);
Hans Verkuilbfaab892012-05-14 06:30:06 -0300267 setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
Patrice Chotard8715b162011-04-18 17:38:37 -0300268 if (gspca_dev->usb_err < 0)
Theodore Kilgorec93396e2013-02-04 13:17:55 -0300269 PERR("Start streaming command failed");
Patrice Chotard8715b162011-04-18 17:38:37 -0300270 return gspca_dev->usb_err;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300271}
272
Patrice Chotardc3d86922011-04-18 17:37:06 -0300273static void sd_pkt_scan(struct gspca_dev *gspca_dev,
274 u8 *data, int len)
Theodore Kilgore3040b042009-08-03 04:13:23 -0300275{
Patrice Chotardc3d86922011-04-18 17:37:06 -0300276 struct sd *sd = (struct sd *) gspca_dev;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300277 int packet_type;
Patrice Chotardc3d86922011-04-18 17:37:06 -0300278 u32 header_marker;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300279
Patrice Chotardc3d86922011-04-18 17:37:06 -0300280 PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
281 len, JEILINJ_MAX_TRANSFER);
282 if (len != JEILINJ_MAX_TRANSFER) {
283 PDEBUG(D_PACK, "bad length");
284 goto discard;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300285 }
Patrice Chotardc3d86922011-04-18 17:37:06 -0300286 /* check if it's start of frame */
287 header_marker = ((u32 *)data)[0];
288 if (header_marker == FRAME_START) {
289 sd->blocks_left = data[0x0a] - 1;
290 PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
Hans de Goede6ca3f252009-10-09 03:58:35 -0300291 /* Start a new frame, and add the JPEG header, first thing */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300292 gspca_frame_add(gspca_dev, FIRST_PACKET,
Patrice Chotardc3d86922011-04-18 17:37:06 -0300293 sd->jpeg_hdr, JPEG_HDR_SZ);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300294 /* Toss line 0 of data block 0, keep the rest. */
295 gspca_frame_add(gspca_dev, INTER_PACKET,
Patrice Chotardc3d86922011-04-18 17:37:06 -0300296 data + FRAME_HEADER_LEN,
Theodore Kilgore3040b042009-08-03 04:13:23 -0300297 JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
Patrice Chotardc3d86922011-04-18 17:37:06 -0300298 } else if (sd->blocks_left > 0) {
299 PDEBUG(D_STREAM, "%d blocks remaining for frame",
300 sd->blocks_left);
301 sd->blocks_left -= 1;
302 if (sd->blocks_left == 0)
303 packet_type = LAST_PACKET;
304 else
305 packet_type = INTER_PACKET;
306 gspca_frame_add(gspca_dev, packet_type,
307 data, JEILINJ_MAX_TRANSFER);
308 } else
309 goto discard;
310 return;
311discard:
312 /* Discard data until a new frame starts. */
313 gspca_dev->last_packet_type = DISCARD_PACKET;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300314}
315
316/* This function is called at probe time just before sd_init */
317static int sd_config(struct gspca_dev *gspca_dev,
318 const struct usb_device_id *id)
319{
320 struct cam *cam = &gspca_dev->cam;
321 struct sd *dev = (struct sd *) gspca_dev;
322
Patrice Chotard713b4662011-04-18 17:40:54 -0300323 dev->type = id->driver_info;
Patrice Chotard5396e622011-04-18 17:42:06 -0300324 dev->quality = QUALITY_DEF;
Jean-François Moine7d84a172011-07-03 07:27:24 -0300325
Theodore Kilgore3040b042009-08-03 04:13:23 -0300326 cam->cam_mode = jlj_mode;
Patrice Chotard6f8efcf2011-04-18 17:39:38 -0300327 cam->nmodes = ARRAY_SIZE(jlj_mode);
Theodore Kilgore3040b042009-08-03 04:13:23 -0300328 cam->bulk = 1;
Patrice Chotardc3d86922011-04-18 17:37:06 -0300329 cam->bulk_nurbs = 1;
330 cam->bulk_size = JEILINJ_MAX_TRANSFER;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300331 return 0;
332}
333
Patrice Chotardc3d86922011-04-18 17:37:06 -0300334static void sd_stopN(struct gspca_dev *gspca_dev)
Theodore Kilgore3040b042009-08-03 04:13:23 -0300335{
Patrice Chotardc3d86922011-04-18 17:37:06 -0300336 int i;
337 u8 *buf;
Jean-François Moine7d84a172011-07-03 07:27:24 -0300338 static u8 stop_commands[][2] = {
Patrice Chotardc3d86922011-04-18 17:37:06 -0300339 {0x71, 0x00},
340 {0x70, 0x09},
341 {0x71, 0x80},
342 {0x70, 0x05}
343 };
Theodore Kilgore3040b042009-08-03 04:13:23 -0300344
Patrice Chotardc3d86922011-04-18 17:37:06 -0300345 for (;;) {
346 /* get the image remaining blocks */
347 usb_bulk_msg(gspca_dev->dev,
348 gspca_dev->urb[0]->pipe,
349 gspca_dev->urb[0]->transfer_buffer,
350 JEILINJ_MAX_TRANSFER, NULL,
351 JEILINJ_DATA_TIMEOUT);
352
353 /* search for 0xff 0xd9 (EOF for JPEG) */
354 i = 0;
355 buf = gspca_dev->urb[0]->transfer_buffer;
356 while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
357 ((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
358 i++;
359
360 if (i != (JEILINJ_MAX_TRANSFER - 1))
361 /* last remaining block found */
362 break;
363 }
364
365 for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
366 jlj_write2(gspca_dev, stop_commands[i]);
Theodore Kilgore3040b042009-08-03 04:13:23 -0300367}
368
369/* this function is called at probe and resume time */
370static int sd_init(struct gspca_dev *gspca_dev)
371{
Patrice Chotard8715b162011-04-18 17:38:37 -0300372 return gspca_dev->usb_err;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300373}
374
375/* Set up for getting frames. */
376static int sd_start(struct gspca_dev *gspca_dev)
377{
378 struct sd *dev = (struct sd *) gspca_dev;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300379
380 /* create the JPEG header */
Ondrej Zary1966bc22013-08-30 17:54:23 -0300381 jpeg_define(dev->jpeg_hdr, gspca_dev->pixfmt.height,
382 gspca_dev->pixfmt.width,
Theodore Kilgore3040b042009-08-03 04:13:23 -0300383 0x21); /* JPEG 422 */
384 jpeg_set_qual(dev->jpeg_hdr, dev->quality);
Patrice Chotard6f8efcf2011-04-18 17:39:38 -0300385 PDEBUG(D_STREAM, "Start streaming at %dx%d",
Ondrej Zary1966bc22013-08-30 17:54:23 -0300386 gspca_dev->pixfmt.height, gspca_dev->pixfmt.width);
Patrice Chotard8715b162011-04-18 17:38:37 -0300387 jlj_start(gspca_dev);
388 return gspca_dev->usb_err;
Theodore Kilgore3040b042009-08-03 04:13:23 -0300389}
390
391/* Table of supported USB devices */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300392static const struct usb_device_id device_table[] = {
Patrice Chotard713b4662011-04-18 17:40:54 -0300393 {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
394 {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
Theodore Kilgore3040b042009-08-03 04:13:23 -0300395 {}
396};
397
398MODULE_DEVICE_TABLE(usb, device_table);
399
Hans Verkuilbfaab892012-05-14 06:30:06 -0300400static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
Patrice Chotard5396e622011-04-18 17:42:06 -0300401{
Hans Verkuilbfaab892012-05-14 06:30:06 -0300402 struct gspca_dev *gspca_dev =
403 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
404 struct sd *sd = (struct sd *)gspca_dev;
405
406 gspca_dev->usb_err = 0;
407
408 if (!gspca_dev->streaming)
409 return 0;
410
411 switch (ctrl->id) {
Patrice Chotard5396e622011-04-18 17:42:06 -0300412 case V4L2_CID_POWER_LINE_FREQUENCY:
Hans Verkuilbfaab892012-05-14 06:30:06 -0300413 setfreq(gspca_dev, ctrl->val);
414 break;
415 case V4L2_CID_RED_BALANCE:
416 setred(gspca_dev, ctrl->val);
417 break;
418 case V4L2_CID_GAIN:
419 setgreen(gspca_dev, ctrl->val);
420 break;
421 case V4L2_CID_BLUE_BALANCE:
422 setblue(gspca_dev, ctrl->val);
423 break;
424 case V4L2_CID_AUTOGAIN:
425 setautogain(gspca_dev, ctrl->val);
426 break;
427 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
428 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
429 setcamquality(gspca_dev, ctrl->val);
Patrice Chotard5396e622011-04-18 17:42:06 -0300430 break;
431 }
Hans Verkuilbfaab892012-05-14 06:30:06 -0300432 return gspca_dev->usb_err;
433}
434
435static const struct v4l2_ctrl_ops sd_ctrl_ops = {
436 .s_ctrl = sd_s_ctrl,
437};
438
439static int sd_init_controls(struct gspca_dev *gspca_dev)
440{
441 struct sd *sd = (struct sd *)gspca_dev;
442 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
443 static const struct v4l2_ctrl_config custom_autogain = {
444 .ops = &sd_ctrl_ops,
445 .id = V4L2_CID_AUTOGAIN,
446 .type = V4L2_CTRL_TYPE_INTEGER,
447 .name = "Automatic Gain (and Exposure)",
448 .max = 3,
449 .step = 1,
450 .def = 0,
451 };
452
453 gspca_dev->vdev.ctrl_handler = hdl;
454 v4l2_ctrl_handler_init(hdl, 6);
455 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
456 V4L2_CID_POWER_LINE_FREQUENCY,
457 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
458 V4L2_CID_POWER_LINE_FREQUENCY_60HZ);
459 v4l2_ctrl_new_custom(hdl, &custom_autogain, NULL);
460 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
461 V4L2_CID_RED_BALANCE, 0, 3, 1, 2);
462 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
463 V4L2_CID_GAIN, 0, 3, 1, 2);
464 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
465 V4L2_CID_BLUE_BALANCE, 0, 3, 1, 2);
466 sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
467 V4L2_CID_JPEG_COMPRESSION_QUALITY,
468 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
469
470 if (hdl->error) {
471 pr_err("Could not initialize controls\n");
472 return hdl->error;
473 }
474 return 0;
Patrice Chotard5396e622011-04-18 17:42:06 -0300475}
476
477static int sd_set_jcomp(struct gspca_dev *gspca_dev,
Hans Verkuild88aab52012-09-17 05:05:25 -0300478 const struct v4l2_jpegcompression *jcomp)
Patrice Chotard5396e622011-04-18 17:42:06 -0300479{
480 struct sd *sd = (struct sd *) gspca_dev;
481
Hans Verkuilbfaab892012-05-14 06:30:06 -0300482 v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
Patrice Chotard5396e622011-04-18 17:42:06 -0300483 return 0;
484}
485
486static int sd_get_jcomp(struct gspca_dev *gspca_dev,
487 struct v4l2_jpegcompression *jcomp)
488{
489 struct sd *sd = (struct sd *) gspca_dev;
490
491 memset(jcomp, 0, sizeof *jcomp);
Hans Verkuilbfaab892012-05-14 06:30:06 -0300492 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
Patrice Chotard5396e622011-04-18 17:42:06 -0300493 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
494 | V4L2_JPEG_MARKER_DQT;
495 return 0;
496}
497
498
Theodore Kilgore3040b042009-08-03 04:13:23 -0300499/* sub-driver description */
Patrice Chotard713b4662011-04-18 17:40:54 -0300500static const struct sd_desc sd_desc_sakar_57379 = {
Theodore Kilgore3040b042009-08-03 04:13:23 -0300501 .name = MODULE_NAME,
502 .config = sd_config,
503 .init = sd_init,
504 .start = sd_start,
Patrice Chotardc3d86922011-04-18 17:37:06 -0300505 .stopN = sd_stopN,
506 .pkt_scan = sd_pkt_scan,
Theodore Kilgore3040b042009-08-03 04:13:23 -0300507};
508
Patrice Chotard713b4662011-04-18 17:40:54 -0300509/* sub-driver description */
510static const struct sd_desc sd_desc_sportscam_dv15 = {
511 .name = MODULE_NAME,
512 .config = sd_config,
513 .init = sd_init,
Hans Verkuilbfaab892012-05-14 06:30:06 -0300514 .init_controls = sd_init_controls,
Patrice Chotard713b4662011-04-18 17:40:54 -0300515 .start = sd_start,
516 .stopN = sd_stopN,
517 .pkt_scan = sd_pkt_scan,
Patrice Chotard5396e622011-04-18 17:42:06 -0300518 .get_jcomp = sd_get_jcomp,
519 .set_jcomp = sd_set_jcomp,
Patrice Chotard713b4662011-04-18 17:40:54 -0300520};
521
522static const struct sd_desc *sd_desc[2] = {
523 &sd_desc_sakar_57379,
524 &sd_desc_sportscam_dv15
525};
526
Theodore Kilgore3040b042009-08-03 04:13:23 -0300527/* -- device connect -- */
528static int sd_probe(struct usb_interface *intf,
529 const struct usb_device_id *id)
530{
531 return gspca_dev_probe(intf, id,
Patrice Chotard713b4662011-04-18 17:40:54 -0300532 sd_desc[id->driver_info],
Theodore Kilgore3040b042009-08-03 04:13:23 -0300533 sizeof(struct sd),
534 THIS_MODULE);
535}
536
537static struct usb_driver sd_driver = {
538 .name = MODULE_NAME,
539 .id_table = device_table,
540 .probe = sd_probe,
541 .disconnect = gspca_disconnect,
542#ifdef CONFIG_PM
543 .suspend = gspca_suspend,
544 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -0300545 .reset_resume = gspca_resume,
Theodore Kilgore3040b042009-08-03 04:13:23 -0300546#endif
547};
548
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800549module_usb_driver(sd_driver);