]>
Commit | Line | Data |
---|---|---|
5b0ff8c4 JFM |
1 | /* |
2 | * spca1528 subdriver | |
3 | * | |
0dd82696 | 4 | * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr) |
5b0ff8c4 JFM |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | ||
133a9fe9 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
5b0ff8c4 JFM |
23 | #define MODULE_NAME "spca1528" |
24 | ||
25 | #include "gspca.h" | |
26 | #include "jpeg.h" | |
27 | ||
28 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); | |
29 | MODULE_DESCRIPTION("SPCA1528 USB Camera Driver"); | |
30 | MODULE_LICENSE("GPL"); | |
31 | ||
32 | /* specific webcam descriptor */ | |
33 | struct sd { | |
34 | struct gspca_dev gspca_dev; /* !! must be the first item */ | |
35 | ||
5b0ff8c4 JFM |
36 | u8 pkt_seq; |
37 | ||
38 | u8 jpeg_hdr[JPEG_HDR_SZ]; | |
39 | }; | |
40 | ||
5b0ff8c4 JFM |
41 | static const struct v4l2_pix_format vga_mode[] = { |
42 | /* (does not work correctly) | |
43 | {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, | |
44 | .bytesperline = 176, | |
45 | .sizeimage = 176 * 144 * 5 / 8 + 590, | |
46 | .colorspace = V4L2_COLORSPACE_JPEG, | |
47 | .priv = 3}, | |
48 | */ | |
49 | {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, | |
50 | .bytesperline = 320, | |
51 | .sizeimage = 320 * 240 * 4 / 8 + 590, | |
52 | .colorspace = V4L2_COLORSPACE_JPEG, | |
53 | .priv = 2}, | |
54 | {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, | |
55 | .bytesperline = 640, | |
56 | .sizeimage = 640 * 480 * 3 / 8 + 590, | |
57 | .colorspace = V4L2_COLORSPACE_JPEG, | |
58 | .priv = 1}, | |
59 | }; | |
60 | ||
61 | /* read <len> bytes to gspca usb_buf */ | |
62 | static void reg_r(struct gspca_dev *gspca_dev, | |
63 | u8 req, | |
64 | u16 index, | |
65 | int len) | |
66 | { | |
67 | #if USB_BUF_SZ < 64 | |
68 | #error "USB buffer too small" | |
69 | #endif | |
70 | struct usb_device *dev = gspca_dev->dev; | |
71 | int ret; | |
72 | ||
73 | if (gspca_dev->usb_err < 0) | |
74 | return; | |
75 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
76 | req, | |
77 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
78 | 0x0000, /* value */ | |
79 | index, | |
80 | gspca_dev->usb_buf, len, | |
81 | 500); | |
82 | PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index, | |
83 | gspca_dev->usb_buf[0]); | |
84 | if (ret < 0) { | |
133a9fe9 | 85 | pr_err("reg_r err %d\n", ret); |
5b0ff8c4 JFM |
86 | gspca_dev->usb_err = ret; |
87 | } | |
88 | } | |
89 | ||
90 | static void reg_w(struct gspca_dev *gspca_dev, | |
91 | u8 req, | |
92 | u16 value, | |
93 | u16 index) | |
94 | { | |
95 | struct usb_device *dev = gspca_dev->dev; | |
96 | int ret; | |
97 | ||
98 | if (gspca_dev->usb_err < 0) | |
99 | return; | |
100 | PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index); | |
101 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
102 | req, | |
103 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
104 | value, index, | |
105 | NULL, 0, 500); | |
106 | if (ret < 0) { | |
133a9fe9 | 107 | pr_err("reg_w err %d\n", ret); |
5b0ff8c4 JFM |
108 | gspca_dev->usb_err = ret; |
109 | } | |
110 | } | |
111 | ||
112 | static void reg_wb(struct gspca_dev *gspca_dev, | |
113 | u8 req, | |
114 | u16 value, | |
115 | u16 index, | |
116 | u8 byte) | |
117 | { | |
118 | struct usb_device *dev = gspca_dev->dev; | |
119 | int ret; | |
120 | ||
121 | if (gspca_dev->usb_err < 0) | |
122 | return; | |
123 | PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte); | |
124 | gspca_dev->usb_buf[0] = byte; | |
125 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
126 | req, | |
127 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
128 | value, index, | |
129 | gspca_dev->usb_buf, 1, 500); | |
130 | if (ret < 0) { | |
133a9fe9 | 131 | pr_err("reg_w err %d\n", ret); |
5b0ff8c4 JFM |
132 | gspca_dev->usb_err = ret; |
133 | } | |
134 | } | |
135 | ||
136 | static void wait_status_0(struct gspca_dev *gspca_dev) | |
137 | { | |
9ba4a4ba | 138 | int i, w; |
5b0ff8c4 | 139 | |
9ba4a4ba JFM |
140 | i = 16; |
141 | w = 0; | |
5b0ff8c4 JFM |
142 | do { |
143 | reg_r(gspca_dev, 0x21, 0x0000, 1); | |
144 | if (gspca_dev->usb_buf[0] == 0) | |
145 | return; | |
9ba4a4ba JFM |
146 | w += 15; |
147 | msleep(w); | |
5b0ff8c4 JFM |
148 | } while (--i > 0); |
149 | PDEBUG(D_ERR, "wait_status_0 timeout"); | |
150 | gspca_dev->usb_err = -ETIME; | |
151 | } | |
152 | ||
153 | static void wait_status_1(struct gspca_dev *gspca_dev) | |
154 | { | |
155 | int i; | |
156 | ||
157 | i = 10; | |
158 | do { | |
159 | reg_r(gspca_dev, 0x21, 0x0001, 1); | |
160 | msleep(10); | |
161 | if (gspca_dev->usb_buf[0] == 1) { | |
162 | reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00); | |
163 | reg_r(gspca_dev, 0x21, 0x0001, 1); | |
164 | return; | |
165 | } | |
166 | } while (--i > 0); | |
167 | PDEBUG(D_ERR, "wait_status_1 timeout"); | |
168 | gspca_dev->usb_err = -ETIME; | |
169 | } | |
170 | ||
6e937b7f | 171 | static void setbrightness(struct gspca_dev *gspca_dev, s32 val) |
5b0ff8c4 | 172 | { |
6e937b7f | 173 | reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val); |
5b0ff8c4 JFM |
174 | } |
175 | ||
6e937b7f | 176 | static void setcontrast(struct gspca_dev *gspca_dev, s32 val) |
5b0ff8c4 | 177 | { |
6e937b7f | 178 | reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val); |
5b0ff8c4 JFM |
179 | } |
180 | ||
6e937b7f | 181 | static void sethue(struct gspca_dev *gspca_dev, s32 val) |
5b0ff8c4 | 182 | { |
6e937b7f | 183 | reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val); |
5b0ff8c4 JFM |
184 | } |
185 | ||
6e937b7f | 186 | static void setcolor(struct gspca_dev *gspca_dev, s32 val) |
5b0ff8c4 | 187 | { |
6e937b7f | 188 | reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val); |
5b0ff8c4 JFM |
189 | } |
190 | ||
6e937b7f | 191 | static void setsharpness(struct gspca_dev *gspca_dev, s32 val) |
5b0ff8c4 | 192 | { |
6e937b7f | 193 | reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val); |
5b0ff8c4 JFM |
194 | } |
195 | ||
196 | /* this function is called at probe time */ | |
197 | static int sd_config(struct gspca_dev *gspca_dev, | |
198 | const struct usb_device_id *id) | |
199 | { | |
5b0ff8c4 JFM |
200 | gspca_dev->cam.cam_mode = vga_mode; |
201 | gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode); | |
202 | gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */ | |
203 | /*fixme: 256 in ms-win traces*/ | |
204 | ||
5b0ff8c4 JFM |
205 | return 0; |
206 | } | |
207 | ||
208 | /* this function is called at probe and resume time */ | |
209 | static int sd_init(struct gspca_dev *gspca_dev) | |
210 | { | |
211 | reg_w(gspca_dev, 0x00, 0x0001, 0x2067); | |
212 | reg_w(gspca_dev, 0x00, 0x00d0, 0x206b); | |
213 | reg_w(gspca_dev, 0x00, 0x0000, 0x206c); | |
214 | reg_w(gspca_dev, 0x00, 0x0001, 0x2069); | |
215 | msleep(8); | |
216 | reg_w(gspca_dev, 0x00, 0x00c0, 0x206b); | |
217 | reg_w(gspca_dev, 0x00, 0x0000, 0x206c); | |
218 | reg_w(gspca_dev, 0x00, 0x0001, 0x2069); | |
219 | ||
220 | reg_r(gspca_dev, 0x20, 0x0000, 1); | |
221 | reg_r(gspca_dev, 0x20, 0x0000, 5); | |
222 | reg_r(gspca_dev, 0x23, 0x0000, 64); | |
223 | PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c], | |
224 | &gspca_dev->usb_buf[0x30]); | |
225 | reg_r(gspca_dev, 0x23, 0x0001, 64); | |
226 | return gspca_dev->usb_err; | |
227 | } | |
228 | ||
229 | /* function called at start time before URB creation */ | |
230 | static int sd_isoc_init(struct gspca_dev *gspca_dev) | |
231 | { | |
232 | u8 mode; | |
233 | ||
234 | reg_r(gspca_dev, 0x00, 0x2520, 1); | |
235 | wait_status_0(gspca_dev); | |
236 | reg_w(gspca_dev, 0xc5, 0x0003, 0x0000); | |
237 | wait_status_1(gspca_dev); | |
238 | ||
239 | wait_status_0(gspca_dev); | |
240 | mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv; | |
241 | reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode); | |
242 | reg_r(gspca_dev, 0x25, 0x0004, 1); | |
0dd82696 | 243 | reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */ |
5b0ff8c4 | 244 | reg_r(gspca_dev, 0x27, 0x0000, 1); |
6507b423 JFM |
245 | |
246 | /* not useful.. | |
247 | gspca_dev->alt = 4; * use alternate setting 3 */ | |
248 | ||
5b0ff8c4 JFM |
249 | return gspca_dev->usb_err; |
250 | } | |
251 | ||
252 | /* -- start the camera -- */ | |
253 | static int sd_start(struct gspca_dev *gspca_dev) | |
254 | { | |
255 | struct sd *sd = (struct sd *) gspca_dev; | |
256 | ||
257 | /* initialize the JPEG header */ | |
258 | jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, | |
259 | 0x22); /* JPEG 411 */ | |
260 | ||
3e12950d JFM |
261 | /* the JPEG quality shall be 85% */ |
262 | jpeg_set_qual(sd->jpeg_hdr, 85); | |
5b0ff8c4 | 263 | |
5b0ff8c4 JFM |
264 | reg_r(gspca_dev, 0x00, 0x2520, 1); |
265 | msleep(8); | |
266 | ||
267 | /* start the capture */ | |
268 | wait_status_0(gspca_dev); | |
0dd82696 | 269 | reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */ |
5b0ff8c4 JFM |
270 | wait_status_1(gspca_dev); |
271 | wait_status_0(gspca_dev); | |
272 | msleep(200); | |
273 | ||
274 | sd->pkt_seq = 0; | |
275 | return gspca_dev->usb_err; | |
276 | } | |
277 | ||
278 | static void sd_stopN(struct gspca_dev *gspca_dev) | |
279 | { | |
280 | /* stop the capture */ | |
281 | wait_status_0(gspca_dev); | |
0dd82696 | 282 | reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */ |
5b0ff8c4 JFM |
283 | wait_status_1(gspca_dev); |
284 | wait_status_0(gspca_dev); | |
285 | } | |
286 | ||
287 | /* move a packet adding 0x00 after 0xff */ | |
288 | static void add_packet(struct gspca_dev *gspca_dev, | |
289 | u8 *data, | |
290 | int len) | |
291 | { | |
292 | int i; | |
293 | ||
294 | i = 0; | |
295 | do { | |
296 | if (data[i] == 0xff) { | |
297 | gspca_frame_add(gspca_dev, INTER_PACKET, | |
298 | data, i + 1); | |
299 | len -= i; | |
300 | data += i; | |
301 | *data = 0x00; | |
302 | i = 0; | |
303 | } | |
304 | } while (++i < len); | |
305 | gspca_frame_add(gspca_dev, INTER_PACKET, data, len); | |
306 | } | |
307 | ||
308 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, | |
309 | u8 *data, /* isoc packet */ | |
310 | int len) /* iso packet length */ | |
311 | { | |
312 | struct sd *sd = (struct sd *) gspca_dev; | |
313 | static const u8 ffd9[] = {0xff, 0xd9}; | |
314 | ||
315 | /* image packets start with: | |
316 | * 02 8n | |
317 | * with <n> bit: | |
318 | * 0x01: even (0) / odd (1) image | |
319 | * 0x02: end of image when set | |
320 | */ | |
321 | if (len < 3) | |
322 | return; /* empty packet */ | |
323 | if (*data == 0x02) { | |
324 | if (data[1] & 0x02) { | |
325 | sd->pkt_seq = !(data[1] & 1); | |
326 | add_packet(gspca_dev, data + 2, len - 2); | |
327 | gspca_frame_add(gspca_dev, LAST_PACKET, | |
328 | ffd9, 2); | |
329 | return; | |
330 | } | |
331 | if ((data[1] & 1) != sd->pkt_seq) | |
332 | goto err; | |
333 | if (gspca_dev->last_packet_type == LAST_PACKET) | |
334 | gspca_frame_add(gspca_dev, FIRST_PACKET, | |
335 | sd->jpeg_hdr, JPEG_HDR_SZ); | |
336 | add_packet(gspca_dev, data + 2, len - 2); | |
337 | return; | |
338 | } | |
339 | err: | |
340 | gspca_dev->last_packet_type = DISCARD_PACKET; | |
341 | } | |
342 | ||
6e937b7f | 343 | static int sd_s_ctrl(struct v4l2_ctrl *ctrl) |
5b0ff8c4 | 344 | { |
6e937b7f HV |
345 | struct gspca_dev *gspca_dev = |
346 | container_of(ctrl->handler, struct gspca_dev, ctrl_handler); | |
347 | ||
348 | gspca_dev->usb_err = 0; | |
349 | ||
350 | if (!gspca_dev->streaming) | |
351 | return 0; | |
352 | ||
353 | switch (ctrl->id) { | |
354 | case V4L2_CID_BRIGHTNESS: | |
355 | setbrightness(gspca_dev, ctrl->val); | |
356 | break; | |
357 | case V4L2_CID_CONTRAST: | |
358 | setcontrast(gspca_dev, ctrl->val); | |
359 | break; | |
360 | case V4L2_CID_HUE: | |
361 | sethue(gspca_dev, ctrl->val); | |
362 | break; | |
363 | case V4L2_CID_SATURATION: | |
364 | setcolor(gspca_dev, ctrl->val); | |
365 | break; | |
366 | case V4L2_CID_SHARPNESS: | |
367 | setsharpness(gspca_dev, ctrl->val); | |
368 | break; | |
369 | } | |
5b0ff8c4 JFM |
370 | return gspca_dev->usb_err; |
371 | } | |
372 | ||
6e937b7f HV |
373 | static const struct v4l2_ctrl_ops sd_ctrl_ops = { |
374 | .s_ctrl = sd_s_ctrl, | |
375 | }; | |
5b0ff8c4 | 376 | |
6e937b7f | 377 | static int sd_init_controls(struct gspca_dev *gspca_dev) |
5b0ff8c4 | 378 | { |
6e937b7f HV |
379 | struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler; |
380 | ||
381 | gspca_dev->vdev.ctrl_handler = hdl; | |
382 | v4l2_ctrl_handler_init(hdl, 5); | |
383 | v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, | |
384 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); | |
385 | v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, | |
386 | V4L2_CID_CONTRAST, 0, 8, 1, 1); | |
387 | v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, | |
388 | V4L2_CID_HUE, 0, 255, 1, 0); | |
389 | v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, | |
390 | V4L2_CID_SATURATION, 0, 8, 1, 1); | |
391 | v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, | |
392 | V4L2_CID_SHARPNESS, 0, 255, 1, 0); | |
393 | ||
394 | if (hdl->error) { | |
395 | pr_err("Could not initialize controls\n"); | |
396 | return hdl->error; | |
397 | } | |
5b0ff8c4 JFM |
398 | return 0; |
399 | } | |
400 | ||
401 | /* sub-driver description */ | |
402 | static const struct sd_desc sd_desc = { | |
403 | .name = MODULE_NAME, | |
5b0ff8c4 JFM |
404 | .config = sd_config, |
405 | .init = sd_init, | |
6e937b7f | 406 | .init_controls = sd_init_controls, |
5b0ff8c4 JFM |
407 | .isoc_init = sd_isoc_init, |
408 | .start = sd_start, | |
409 | .stopN = sd_stopN, | |
410 | .pkt_scan = sd_pkt_scan, | |
411 | }; | |
412 | ||
413 | /* -- module initialisation -- */ | |
95c967c1 | 414 | static const struct usb_device_id device_table[] = { |
5b0ff8c4 JFM |
415 | {USB_DEVICE(0x04fc, 0x1528)}, |
416 | {} | |
417 | }; | |
418 | MODULE_DEVICE_TABLE(usb, device_table); | |
419 | ||
420 | /* -- device connect -- */ | |
421 | static int sd_probe(struct usb_interface *intf, | |
422 | const struct usb_device_id *id) | |
423 | { | |
424 | /* the video interface for isochronous transfer is 1 */ | |
425 | if (intf->cur_altsetting->desc.bInterfaceNumber != 1) | |
426 | return -ENODEV; | |
427 | ||
428 | return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd), | |
429 | THIS_MODULE); | |
430 | } | |
431 | ||
432 | static struct usb_driver sd_driver = { | |
433 | .name = MODULE_NAME, | |
434 | .id_table = device_table, | |
435 | .probe = sd_probe, | |
436 | .disconnect = gspca_disconnect, | |
437 | #ifdef CONFIG_PM | |
438 | .suspend = gspca_suspend, | |
439 | .resume = gspca_resume, | |
8bb58964 | 440 | .reset_resume = gspca_resume, |
5b0ff8c4 JFM |
441 | #endif |
442 | }; | |
443 | ||
ecb3b2b3 | 444 | module_usb_driver(sd_driver); |