~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/media/video/gspca/jeilinj.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 *
7
7
 * Copyright (C) 2009 Theodore Kilgore
8
8
 *
 
9
 * Sportscam DV15 support and control settings are
 
10
 * Copyright (C) 2011 Patrice Chotard
 
11
 *
9
12
 * This program is free software; you can redistribute it and/or modify
10
13
 * it under the terms of the GNU General Public License as published by
11
14
 * the Free Software Foundation; either version 2 of the License, or
23
26
 
24
27
#define MODULE_NAME "jeilinj"
25
28
 
26
 
#include <linux/workqueue.h>
27
29
#include <linux/slab.h>
28
30
#include "gspca.h"
29
31
#include "jpeg.h"
34
36
 
35
37
/* Default timeouts, in ms */
36
38
#define JEILINJ_CMD_TIMEOUT 500
 
39
#define JEILINJ_CMD_DELAY 160
37
40
#define JEILINJ_DATA_TIMEOUT 1000
38
41
 
39
42
/* Maximum transfer size to use. */
40
43
#define JEILINJ_MAX_TRANSFER 0x200
41
 
 
42
44
#define FRAME_HEADER_LEN 0x10
 
45
#define FRAME_START 0xFFFFFFFF
 
46
 
 
47
enum {
 
48
        SAKAR_57379,
 
49
        SPORTSCAM_DV15,
 
50
};
 
51
 
 
52
#define CAMQUALITY_MIN 0        /* highest cam quality */
 
53
#define CAMQUALITY_MAX 97       /* lowest cam quality  */
 
54
 
 
55
enum e_ctrl {
 
56
        LIGHTFREQ,
 
57
        AUTOGAIN,
 
58
        RED,
 
59
        GREEN,
 
60
        BLUE,
 
61
        NCTRLS          /* number of controls */
 
62
};
43
63
 
44
64
/* Structure to hold all of our device specific stuff */
45
65
struct sd {
46
66
        struct gspca_dev gspca_dev;     /* !! must be the first item */
 
67
        struct gspca_ctrl ctrls[NCTRLS];
 
68
        int blocks_left;
47
69
        const struct v4l2_pix_format *cap_mode;
48
70
        /* Driver stuff */
49
 
        struct work_struct work_struct;
50
 
        struct workqueue_struct *work_thread;
 
71
        u8 type;
51
72
        u8 quality;                              /* image quality */
52
 
        u8 jpegqual;                            /* webcam quality */
 
73
#define QUALITY_MIN 35
 
74
#define QUALITY_MAX 85
 
75
#define QUALITY_DEF 85
53
76
        u8 jpeg_hdr[JPEG_HDR_SZ];
54
77
};
55
78
 
56
 
        struct jlj_command {
57
 
                unsigned char instruction[2];
58
 
                unsigned char ack_wanted;
59
 
        };
 
79
struct jlj_command {
 
80
        unsigned char instruction[2];
 
81
        unsigned char ack_wanted;
 
82
        unsigned char delay;
 
83
};
60
84
 
61
85
/* AFAICT these cameras will only do 320x240. */
62
86
static struct v4l2_pix_format jlj_mode[] = {
64
88
                .bytesperline = 320,
65
89
                .sizeimage = 320 * 240,
66
90
                .colorspace = V4L2_COLORSPACE_JPEG,
 
91
                .priv = 0},
 
92
        { 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
 
93
                .bytesperline = 640,
 
94
                .sizeimage = 640 * 480,
 
95
                .colorspace = V4L2_COLORSPACE_JPEG,
67
96
                .priv = 0}
68
97
};
69
98
 
73
102
 */
74
103
 
75
104
/* All commands are two bytes only */
76
 
static int jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
 
105
static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
77
106
{
78
107
        int retval;
79
108
 
 
109
        if (gspca_dev->usb_err < 0)
 
110
                return;
80
111
        memcpy(gspca_dev->usb_buf, command, 2);
81
112
        retval = usb_bulk_msg(gspca_dev->dev,
82
113
                        usb_sndbulkpipe(gspca_dev->dev, 3),
83
114
                        gspca_dev->usb_buf, 2, NULL, 500);
84
 
        if (retval < 0)
 
115
        if (retval < 0) {
85
116
                err("command write [%02x] error %d",
86
117
                                gspca_dev->usb_buf[0], retval);
87
 
        return retval;
 
118
                gspca_dev->usb_err = retval;
 
119
        }
88
120
}
89
121
 
90
122
/* Responses are one byte only */
91
 
static int jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
 
123
static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char response)
92
124
{
93
125
        int retval;
94
126
 
 
127
        if (gspca_dev->usb_err < 0)
 
128
                return;
95
129
        retval = usb_bulk_msg(gspca_dev->dev,
96
130
        usb_rcvbulkpipe(gspca_dev->dev, 0x84),
97
131
                                gspca_dev->usb_buf, 1, NULL, 500);
98
132
        response = gspca_dev->usb_buf[0];
99
 
        if (retval < 0)
 
133
        if (retval < 0) {
100
134
                err("read command [%02x] error %d",
101
135
                                gspca_dev->usb_buf[0], retval);
102
 
        return retval;
103
 
}
 
136
                gspca_dev->usb_err = retval;
 
137
        }
 
138
}
 
139
 
 
140
static void setfreq(struct gspca_dev *gspca_dev)
 
141
{
 
142
        struct sd *sd = (struct sd *) gspca_dev;
 
143
        u8 freq_commands[][2] = {
 
144
                {0x71, 0x80},
 
145
                {0x70, 0x07}
 
146
        };
 
147
 
 
148
        freq_commands[0][1] |= (sd->ctrls[LIGHTFREQ].val >> 1);
 
149
 
 
150
        jlj_write2(gspca_dev, freq_commands[0]);
 
151
        jlj_write2(gspca_dev, freq_commands[1]);
 
152
}
 
153
 
 
154
static void setcamquality(struct gspca_dev *gspca_dev)
 
155
{
 
156
        struct sd *sd = (struct sd *) gspca_dev;
 
157
        u8 quality_commands[][2] = {
 
158
                {0x71, 0x1E},
 
159
                {0x70, 0x06}
 
160
        };
 
161
        u8 camquality;
 
162
 
 
163
        /* adapt camera quality from jpeg quality */
 
164
        camquality = ((QUALITY_MAX - sd->quality) * CAMQUALITY_MAX)
 
165
                / (QUALITY_MAX - QUALITY_MIN);
 
166
        quality_commands[0][1] += camquality;
 
167
 
 
168
        jlj_write2(gspca_dev, quality_commands[0]);
 
169
        jlj_write2(gspca_dev, quality_commands[1]);
 
170
}
 
171
 
 
172
static void setautogain(struct gspca_dev *gspca_dev)
 
173
{
 
174
        struct sd *sd = (struct sd *) gspca_dev;
 
175
        u8 autogain_commands[][2] = {
 
176
                {0x94, 0x02},
 
177
                {0xcf, 0x00}
 
178
        };
 
179
 
 
180
        autogain_commands[1][1] = (sd->ctrls[AUTOGAIN].val << 4);
 
181
 
 
182
        jlj_write2(gspca_dev, autogain_commands[0]);
 
183
        jlj_write2(gspca_dev, autogain_commands[1]);
 
184
}
 
185
 
 
186
static void setred(struct gspca_dev *gspca_dev)
 
187
{
 
188
        struct sd *sd = (struct sd *) gspca_dev;
 
189
        u8 setred_commands[][2] = {
 
190
                {0x94, 0x02},
 
191
                {0xe6, 0x00}
 
192
        };
 
193
 
 
194
        setred_commands[1][1] = sd->ctrls[RED].val;
 
195
 
 
196
        jlj_write2(gspca_dev, setred_commands[0]);
 
197
        jlj_write2(gspca_dev, setred_commands[1]);
 
198
}
 
199
 
 
200
static void setgreen(struct gspca_dev *gspca_dev)
 
201
{
 
202
        struct sd *sd = (struct sd *) gspca_dev;
 
203
        u8 setgreen_commands[][2] = {
 
204
                {0x94, 0x02},
 
205
                {0xe7, 0x00}
 
206
        };
 
207
 
 
208
        setgreen_commands[1][1] = sd->ctrls[GREEN].val;
 
209
 
 
210
        jlj_write2(gspca_dev, setgreen_commands[0]);
 
211
        jlj_write2(gspca_dev, setgreen_commands[1]);
 
212
}
 
213
 
 
214
static void setblue(struct gspca_dev *gspca_dev)
 
215
{
 
216
        struct sd *sd = (struct sd *) gspca_dev;
 
217
        u8 setblue_commands[][2] = {
 
218
                {0x94, 0x02},
 
219
                {0xe9, 0x00}
 
220
        };
 
221
 
 
222
        setblue_commands[1][1] = sd->ctrls[BLUE].val;
 
223
 
 
224
        jlj_write2(gspca_dev, setblue_commands[0]);
 
225
        jlj_write2(gspca_dev, setblue_commands[1]);
 
226
}
 
227
 
 
228
static const struct ctrl sd_ctrls[NCTRLS] = {
 
229
[LIGHTFREQ] = {
 
230
            {
 
231
                .id      = V4L2_CID_POWER_LINE_FREQUENCY,
 
232
                .type    = V4L2_CTRL_TYPE_MENU,
 
233
                .name    = "Light frequency filter",
 
234
                .minimum = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED, /* 1 */
 
235
                .maximum = V4L2_CID_POWER_LINE_FREQUENCY_60HZ, /* 2 */
 
236
                .step    = 1,
 
237
                .default_value = V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
 
238
            },
 
239
            .set_control = setfreq
 
240
        },
 
241
[AUTOGAIN] = {
 
242
            {
 
243
                .id = V4L2_CID_AUTOGAIN,
 
244
                .type = V4L2_CTRL_TYPE_INTEGER,
 
245
                .name = "Automatic Gain (and Exposure)",
 
246
                .minimum = 0,
 
247
                .maximum = 3,
 
248
                .step = 1,
 
249
#define AUTOGAIN_DEF 0
 
250
                .default_value = AUTOGAIN_DEF,
 
251
           },
 
252
           .set_control = setautogain
 
253
        },
 
254
[RED] = {
 
255
            {
 
256
                .id = V4L2_CID_RED_BALANCE,
 
257
                .type = V4L2_CTRL_TYPE_INTEGER,
 
258
                .name = "red balance",
 
259
                .minimum = 0,
 
260
                .maximum = 3,
 
261
                .step = 1,
 
262
#define RED_BALANCE_DEF 2
 
263
                .default_value = RED_BALANCE_DEF,
 
264
           },
 
265
           .set_control = setred
 
266
        },
 
267
 
 
268
[GREEN] = {
 
269
            {
 
270
                .id = V4L2_CID_GAIN,
 
271
                .type = V4L2_CTRL_TYPE_INTEGER,
 
272
                .name = "green balance",
 
273
                .minimum = 0,
 
274
                .maximum = 3,
 
275
                .step = 1,
 
276
#define GREEN_BALANCE_DEF 2
 
277
                .default_value = GREEN_BALANCE_DEF,
 
278
           },
 
279
           .set_control = setgreen
 
280
        },
 
281
[BLUE] = {
 
282
            {
 
283
                .id = V4L2_CID_BLUE_BALANCE,
 
284
                .type = V4L2_CTRL_TYPE_INTEGER,
 
285
                .name = "blue balance",
 
286
                .minimum = 0,
 
287
                .maximum = 3,
 
288
                .step = 1,
 
289
#define BLUE_BALANCE_DEF 2
 
290
                .default_value = BLUE_BALANCE_DEF,
 
291
           },
 
292
           .set_control = setblue
 
293
        },
 
294
};
104
295
 
105
296
static int jlj_start(struct gspca_dev *gspca_dev)
106
297
{
107
298
        int i;
108
 
        int retval = -1;
 
299
        int start_commands_size;
109
300
        u8 response = 0xff;
 
301
        struct sd *sd = (struct sd *) gspca_dev;
110
302
        struct jlj_command start_commands[] = {
111
 
                {{0x71, 0x81}, 0},
112
 
                {{0x70, 0x05}, 0},
113
 
                {{0x95, 0x70}, 1},
114
 
                {{0x71, 0x81}, 0},
115
 
                {{0x70, 0x04}, 0},
116
 
                {{0x95, 0x70}, 1},
117
 
                {{0x71, 0x00}, 0},
118
 
                {{0x70, 0x08}, 0},
119
 
                {{0x95, 0x70}, 1},
120
 
                {{0x94, 0x02}, 0},
121
 
                {{0xde, 0x24}, 0},
122
 
                {{0x94, 0x02}, 0},
123
 
                {{0xdd, 0xf0}, 0},
124
 
                {{0x94, 0x02}, 0},
125
 
                {{0xe3, 0x2c}, 0},
126
 
                {{0x94, 0x02}, 0},
127
 
                {{0xe4, 0x00}, 0},
128
 
                {{0x94, 0x02}, 0},
129
 
                {{0xe5, 0x00}, 0},
130
 
                {{0x94, 0x02}, 0},
131
 
                {{0xe6, 0x2c}, 0},
132
 
                {{0x94, 0x03}, 0},
133
 
                {{0xaa, 0x00}, 0},
134
 
                {{0x71, 0x1e}, 0},
135
 
                {{0x70, 0x06}, 0},
136
 
                {{0x71, 0x80}, 0},
137
 
                {{0x70, 0x07}, 0}
 
303
                {{0x71, 0x81}, 0, 0},
 
304
                {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
 
305
                {{0x95, 0x70}, 1, 0},
 
306
                {{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
 
307
                {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
 
308
                {{0x95, 0x70}, 1, 0},
 
309
                {{0x71, 0x00}, 0, 0},   /* start streaming ??*/
 
310
                {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
 
311
                {{0x95, 0x70}, 1, 0},
 
312
#define SPORTSCAM_DV15_CMD_SIZE 9
 
313
                {{0x94, 0x02}, 0, 0},
 
314
                {{0xde, 0x24}, 0, 0},
 
315
                {{0x94, 0x02}, 0, 0},
 
316
                {{0xdd, 0xf0}, 0, 0},
 
317
                {{0x94, 0x02}, 0, 0},
 
318
                {{0xe3, 0x2c}, 0, 0},
 
319
                {{0x94, 0x02}, 0, 0},
 
320
                {{0xe4, 0x00}, 0, 0},
 
321
                {{0x94, 0x02}, 0, 0},
 
322
                {{0xe5, 0x00}, 0, 0},
 
323
                {{0x94, 0x02}, 0, 0},
 
324
                {{0xe6, 0x2c}, 0, 0},
 
325
                {{0x94, 0x03}, 0, 0},
 
326
                {{0xaa, 0x00}, 0, 0}
138
327
        };
139
 
        for (i = 0; i < ARRAY_SIZE(start_commands); i++) {
140
 
                retval = jlj_write2(gspca_dev, start_commands[i].instruction);
141
 
                if (retval < 0)
142
 
                        return retval;
 
328
 
 
329
        sd->blocks_left = 0;
 
330
        /* Under Windows, USB spy shows that only the 9 first start
 
331
         * commands are used for SPORTSCAM_DV15 webcam
 
332
         */
 
333
        if (sd->type == SPORTSCAM_DV15)
 
334
                start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
 
335
        else
 
336
                start_commands_size = ARRAY_SIZE(start_commands);
 
337
 
 
338
        for (i = 0; i < start_commands_size; i++) {
 
339
                jlj_write2(gspca_dev, start_commands[i].instruction);
 
340
                if (start_commands[i].delay)
 
341
                        msleep(start_commands[i].delay);
143
342
                if (start_commands[i].ack_wanted)
144
 
                        retval = jlj_read1(gspca_dev, response);
145
 
                if (retval < 0)
146
 
                        return retval;
147
 
        }
148
 
        PDEBUG(D_ERR, "jlj_start retval is %d", retval);
149
 
        return retval;
150
 
}
151
 
 
152
 
static int jlj_stop(struct gspca_dev *gspca_dev)
153
 
{
154
 
        int i;
155
 
        int retval;
156
 
        struct jlj_command stop_commands[] = {
157
 
                {{0x71, 0x00}, 0},
158
 
                {{0x70, 0x09}, 0},
159
 
                {{0x71, 0x80}, 0},
160
 
                {{0x70, 0x05}, 0}
161
 
        };
162
 
        for (i = 0; i < ARRAY_SIZE(stop_commands); i++) {
163
 
                retval = jlj_write2(gspca_dev, stop_commands[i].instruction);
164
 
                if (retval < 0)
165
 
                        return retval;
166
 
        }
167
 
        return retval;
168
 
}
169
 
 
170
 
/* This function is called as a workqueue function and runs whenever the camera
171
 
 * is streaming data. Because it is a workqueue function it is allowed to sleep
172
 
 * so we can use synchronous USB calls. To avoid possible collisions with other
173
 
 * threads attempting to use the camera's USB interface the gspca usb_lock is
174
 
 * used when performing the one USB control operation inside the workqueue,
175
 
 * which tells the camera to close the stream. In practice the only thing
176
 
 * which needs to be protected against is the usb_set_interface call that
177
 
 * gspca makes during stream_off. Otherwise the camera doesn't provide any
178
 
 * controls that the user could try to change.
179
 
 */
180
 
 
181
 
static void jlj_dostream(struct work_struct *work)
182
 
{
183
 
        struct sd *dev = container_of(work, struct sd, work_struct);
184
 
        struct gspca_dev *gspca_dev = &dev->gspca_dev;
185
 
        int blocks_left; /* 0x200-sized blocks remaining in current frame. */
186
 
        int size_in_blocks;
187
 
        int act_len;
 
343
                        jlj_read1(gspca_dev, response);
 
344
        }
 
345
        setcamquality(gspca_dev);
 
346
        msleep(2);
 
347
        setfreq(gspca_dev);
 
348
        if (gspca_dev->usb_err < 0)
 
349
                PDEBUG(D_ERR, "Start streaming command failed");
 
350
        return gspca_dev->usb_err;
 
351
}
 
352
 
 
353
static void sd_pkt_scan(struct gspca_dev *gspca_dev,
 
354
                        u8 *data, int len)
 
355
{
 
356
        struct sd *sd = (struct sd *) gspca_dev;
188
357
        int packet_type;
189
 
        int ret;
190
 
        u8 *buffer;
 
358
        u32 header_marker;
191
359
 
192
 
        buffer = kmalloc(JEILINJ_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
193
 
        if (!buffer) {
194
 
                err("Couldn't allocate USB buffer");
195
 
                goto quit_stream;
 
360
        PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
 
361
                        len, JEILINJ_MAX_TRANSFER);
 
362
        if (len != JEILINJ_MAX_TRANSFER) {
 
363
                PDEBUG(D_PACK, "bad length");
 
364
                goto discard;
196
365
        }
197
 
        while (gspca_dev->present && gspca_dev->streaming) {
198
 
                /*
199
 
                 * Now request data block 0. Line 0 reports the size
200
 
                 * to download, in blocks of size 0x200, and also tells the
201
 
                 * "actual" data size, in bytes, which seems best to ignore.
202
 
                 */
203
 
                ret = usb_bulk_msg(gspca_dev->dev,
204
 
                                usb_rcvbulkpipe(gspca_dev->dev, 0x82),
205
 
                                buffer, JEILINJ_MAX_TRANSFER, &act_len,
206
 
                                JEILINJ_DATA_TIMEOUT);
207
 
                PDEBUG(D_STREAM,
208
 
                        "Got %d bytes out of %d for Block 0",
209
 
                        act_len, JEILINJ_MAX_TRANSFER);
210
 
                if (ret < 0 || act_len < FRAME_HEADER_LEN)
211
 
                        goto quit_stream;
212
 
                size_in_blocks = buffer[0x0a];
213
 
                blocks_left = buffer[0x0a] - 1;
214
 
                PDEBUG(D_STREAM, "blocks_left = 0x%x", blocks_left);
215
 
 
 
366
        /* check if it's start of frame */
 
367
        header_marker = ((u32 *)data)[0];
 
368
        if (header_marker == FRAME_START) {
 
369
                sd->blocks_left = data[0x0a] - 1;
 
370
                PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
216
371
                /* Start a new frame, and add the JPEG header, first thing */
217
372
                gspca_frame_add(gspca_dev, FIRST_PACKET,
218
 
                                dev->jpeg_hdr, JPEG_HDR_SZ);
 
373
                                sd->jpeg_hdr, JPEG_HDR_SZ);
219
374
                /* Toss line 0 of data block 0, keep the rest. */
220
375
                gspca_frame_add(gspca_dev, INTER_PACKET,
221
 
                                buffer + FRAME_HEADER_LEN,
 
376
                                data + FRAME_HEADER_LEN,
222
377
                                JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
223
 
 
224
 
                while (blocks_left > 0) {
225
 
                        if (!gspca_dev->present)
226
 
                                goto quit_stream;
227
 
                        ret = usb_bulk_msg(gspca_dev->dev,
228
 
                                usb_rcvbulkpipe(gspca_dev->dev, 0x82),
229
 
                                buffer, JEILINJ_MAX_TRANSFER, &act_len,
230
 
                                JEILINJ_DATA_TIMEOUT);
231
 
                        if (ret < 0 || act_len < JEILINJ_MAX_TRANSFER)
232
 
                                goto quit_stream;
233
 
                        PDEBUG(D_STREAM,
234
 
                                "%d blocks remaining for frame", blocks_left);
235
 
                        blocks_left -= 1;
236
 
                        if (blocks_left == 0)
237
 
                                packet_type = LAST_PACKET;
238
 
                        else
239
 
                                packet_type = INTER_PACKET;
240
 
                        gspca_frame_add(gspca_dev, packet_type,
241
 
                                        buffer, JEILINJ_MAX_TRANSFER);
242
 
                }
243
 
        }
244
 
quit_stream:
245
 
        mutex_lock(&gspca_dev->usb_lock);
246
 
        if (gspca_dev->present)
247
 
                jlj_stop(gspca_dev);
248
 
        mutex_unlock(&gspca_dev->usb_lock);
249
 
        kfree(buffer);
 
378
        } else if (sd->blocks_left > 0) {
 
379
                PDEBUG(D_STREAM, "%d blocks remaining for frame",
 
380
                                sd->blocks_left);
 
381
                sd->blocks_left -= 1;
 
382
                if (sd->blocks_left == 0)
 
383
                        packet_type = LAST_PACKET;
 
384
                else
 
385
                        packet_type = INTER_PACKET;
 
386
                gspca_frame_add(gspca_dev, packet_type,
 
387
                                data, JEILINJ_MAX_TRANSFER);
 
388
        } else
 
389
                goto discard;
 
390
        return;
 
391
discard:
 
392
        /* Discard data until a new frame starts. */
 
393
        gspca_dev->last_packet_type = DISCARD_PACKET;
250
394
}
251
395
 
252
396
/* This function is called at probe time just before sd_init */
256
400
        struct cam *cam = &gspca_dev->cam;
257
401
        struct sd *dev  = (struct sd *) gspca_dev;
258
402
 
259
 
        dev->quality  = 85;
260
 
        dev->jpegqual = 85;
 
403
        dev->type = id->driver_info;
 
404
        gspca_dev->cam.ctrls = dev->ctrls;
 
405
        dev->quality = QUALITY_DEF;
 
406
        dev->ctrls[LIGHTFREQ].def = V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
 
407
        dev->ctrls[RED].def = RED_BALANCE_DEF;
 
408
        dev->ctrls[GREEN].def = GREEN_BALANCE_DEF;
 
409
        dev->ctrls[BLUE].def = BLUE_BALANCE_DEF;
261
410
        PDEBUG(D_PROBE,
262
411
                "JEILINJ camera detected"
263
412
                " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
264
413
        cam->cam_mode = jlj_mode;
265
 
        cam->nmodes = 1;
 
414
        cam->nmodes = ARRAY_SIZE(jlj_mode);
266
415
        cam->bulk = 1;
267
 
        /* We don't use the buffer gspca allocates so make it small. */
268
 
        cam->bulk_size = 32;
269
 
        INIT_WORK(&dev->work_struct, jlj_dostream);
 
416
        cam->bulk_nurbs = 1;
 
417
        cam->bulk_size = JEILINJ_MAX_TRANSFER;
270
418
        return 0;
271
419
}
272
420
 
273
 
/* called on streamoff with alt==0 and on disconnect */
274
 
/* the usb_lock is held at entry - restore on exit */
275
 
static void sd_stop0(struct gspca_dev *gspca_dev)
 
421
static void sd_stopN(struct gspca_dev *gspca_dev)
276
422
{
277
 
        struct sd *dev = (struct sd *) gspca_dev;
278
 
 
279
 
        /* wait for the work queue to terminate */
280
 
        mutex_unlock(&gspca_dev->usb_lock);
281
 
        /* This waits for jlj_dostream to finish */
282
 
        destroy_workqueue(dev->work_thread);
283
 
        dev->work_thread = NULL;
284
 
        mutex_lock(&gspca_dev->usb_lock);
 
423
        int i;
 
424
        u8 *buf;
 
425
        u8 stop_commands[][2] = {
 
426
                {0x71, 0x00},
 
427
                {0x70, 0x09},
 
428
                {0x71, 0x80},
 
429
                {0x70, 0x05}
 
430
        };
 
431
 
 
432
        for (;;) {
 
433
                /* get the image remaining blocks */
 
434
                usb_bulk_msg(gspca_dev->dev,
 
435
                                gspca_dev->urb[0]->pipe,
 
436
                                gspca_dev->urb[0]->transfer_buffer,
 
437
                                JEILINJ_MAX_TRANSFER, NULL,
 
438
                                JEILINJ_DATA_TIMEOUT);
 
439
 
 
440
                /* search for 0xff 0xd9  (EOF for JPEG) */
 
441
                i = 0;
 
442
                buf = gspca_dev->urb[0]->transfer_buffer;
 
443
                while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
 
444
                        ((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
 
445
                        i++;
 
446
 
 
447
                if (i != (JEILINJ_MAX_TRANSFER - 1))
 
448
                        /* last remaining block found */
 
449
                        break;
 
450
                }
 
451
 
 
452
        for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
 
453
                jlj_write2(gspca_dev, stop_commands[i]);
285
454
}
286
455
 
287
456
/* this function is called at probe and resume time */
288
457
static int sd_init(struct gspca_dev *gspca_dev)
289
458
{
290
 
        return 0;
 
459
        return gspca_dev->usb_err;
291
460
}
292
461
 
293
462
/* Set up for getting frames. */
294
463
static int sd_start(struct gspca_dev *gspca_dev)
295
464
{
296
465
        struct sd *dev = (struct sd *) gspca_dev;
297
 
        int ret;
298
466
 
299
467
        /* create the JPEG header */
300
468
        jpeg_define(dev->jpeg_hdr, gspca_dev->height, gspca_dev->width,
301
469
                        0x21);          /* JPEG 422 */
302
470
        jpeg_set_qual(dev->jpeg_hdr, dev->quality);
303
 
        PDEBUG(D_STREAM, "Start streaming at 320x240");
304
 
        ret = jlj_start(gspca_dev);
305
 
        if (ret < 0) {
306
 
                PDEBUG(D_ERR, "Start streaming command failed");
307
 
                return ret;
308
 
        }
309
 
        /* Start the workqueue function to do the streaming */
310
 
        dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
311
 
        queue_work(dev->work_thread, &dev->work_struct);
312
 
 
313
 
        return 0;
 
471
        PDEBUG(D_STREAM, "Start streaming at %dx%d",
 
472
                gspca_dev->height, gspca_dev->width);
 
473
        jlj_start(gspca_dev);
 
474
        return gspca_dev->usb_err;
314
475
}
315
476
 
316
477
/* Table of supported USB devices */
317
478
static const struct usb_device_id device_table[] = {
318
 
        {USB_DEVICE(0x0979, 0x0280)},
 
479
        {USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
 
480
        {USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
319
481
        {}
320
482
};
321
483
 
322
484
MODULE_DEVICE_TABLE(usb, device_table);
323
485
 
324
 
/* sub-driver description */
325
 
static const struct sd_desc sd_desc = {
326
 
        .name   = MODULE_NAME,
327
 
        .config = sd_config,
328
 
        .init   = sd_init,
329
 
        .start  = sd_start,
330
 
        .stop0  = sd_stop0,
 
486
static int sd_querymenu(struct gspca_dev *gspca_dev,
 
487
                        struct v4l2_querymenu *menu)
 
488
{
 
489
        switch (menu->id) {
 
490
        case V4L2_CID_POWER_LINE_FREQUENCY:
 
491
                switch (menu->index) {
 
492
                case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
 
493
                        strcpy((char *) menu->name, "disable");
 
494
                        return 0;
 
495
                case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
 
496
                        strcpy((char *) menu->name, "50 Hz");
 
497
                        return 0;
 
498
                case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
 
499
                        strcpy((char *) menu->name, "60 Hz");
 
500
                        return 0;
 
501
                }
 
502
                break;
 
503
        }
 
504
        return -EINVAL;
 
505
}
 
506
 
 
507
static int sd_set_jcomp(struct gspca_dev *gspca_dev,
 
508
                        struct v4l2_jpegcompression *jcomp)
 
509
{
 
510
        struct sd *sd = (struct sd *) gspca_dev;
 
511
 
 
512
        if (jcomp->quality < QUALITY_MIN)
 
513
                sd->quality = QUALITY_MIN;
 
514
        else if (jcomp->quality > QUALITY_MAX)
 
515
                sd->quality = QUALITY_MAX;
 
516
        else
 
517
                sd->quality = jcomp->quality;
 
518
        if (gspca_dev->streaming) {
 
519
                jpeg_set_qual(sd->jpeg_hdr, sd->quality);
 
520
                setcamquality(gspca_dev);
 
521
        }
 
522
        return 0;
 
523
}
 
524
 
 
525
static int sd_get_jcomp(struct gspca_dev *gspca_dev,
 
526
                        struct v4l2_jpegcompression *jcomp)
 
527
{
 
528
        struct sd *sd = (struct sd *) gspca_dev;
 
529
 
 
530
        memset(jcomp, 0, sizeof *jcomp);
 
531
        jcomp->quality = sd->quality;
 
532
        jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
 
533
                        | V4L2_JPEG_MARKER_DQT;
 
534
        return 0;
 
535
}
 
536
 
 
537
 
 
538
/* sub-driver description */
 
539
static const struct sd_desc sd_desc_sakar_57379 = {
 
540
        .name   = MODULE_NAME,
 
541
        .config = sd_config,
 
542
        .init   = sd_init,
 
543
        .start  = sd_start,
 
544
        .stopN  = sd_stopN,
 
545
        .pkt_scan = sd_pkt_scan,
 
546
};
 
547
 
 
548
/* sub-driver description */
 
549
static const struct sd_desc sd_desc_sportscam_dv15 = {
 
550
        .name   = MODULE_NAME,
 
551
        .config = sd_config,
 
552
        .init   = sd_init,
 
553
        .start  = sd_start,
 
554
        .stopN  = sd_stopN,
 
555
        .pkt_scan = sd_pkt_scan,
 
556
        .ctrls = sd_ctrls,
 
557
        .nctrls = ARRAY_SIZE(sd_ctrls),
 
558
        .querymenu = sd_querymenu,
 
559
        .get_jcomp = sd_get_jcomp,
 
560
        .set_jcomp = sd_set_jcomp,
 
561
};
 
562
 
 
563
static const struct sd_desc *sd_desc[2] = {
 
564
        &sd_desc_sakar_57379,
 
565
        &sd_desc_sportscam_dv15
331
566
};
332
567
 
333
568
/* -- device connect -- */
335
570
                const struct usb_device_id *id)
336
571
{
337
572
        return gspca_dev_probe(intf, id,
338
 
                        &sd_desc,
 
573
                        sd_desc[id->driver_info],
339
574
                        sizeof(struct sd),
340
575
                        THIS_MODULE);
341
576
}