1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define MODULE_NAME "spca1528"
15 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
16 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
17 MODULE_LICENSE("GPL");
19 /* specific webcam descriptor */
21 struct gspca_dev gspca_dev
; /* !! must be the first item */
25 u8 jpeg_hdr
[JPEG_HDR_SZ
];
28 static const struct v4l2_pix_format vga_mode
[] = {
29 /* (does not work correctly)
30 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
32 .sizeimage = 176 * 144 * 5 / 8 + 590,
33 .colorspace = V4L2_COLORSPACE_JPEG,
36 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
38 .sizeimage
= 320 * 240 * 4 / 8 + 590,
39 .colorspace
= V4L2_COLORSPACE_JPEG
,
41 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
43 .sizeimage
= 640 * 480 * 3 / 8 + 590,
44 .colorspace
= V4L2_COLORSPACE_JPEG
,
48 /* read <len> bytes to gspca usb_buf */
49 static void reg_r(struct gspca_dev
*gspca_dev
,
55 #error "USB buffer too small"
57 struct usb_device
*dev
= gspca_dev
->dev
;
60 if (gspca_dev
->usb_err
< 0)
62 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
64 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
67 gspca_dev
->usb_buf
, len
,
69 gspca_dbg(gspca_dev
, D_USBI
, "GET %02x 0000 %04x %02x\n", req
, index
,
70 gspca_dev
->usb_buf
[0]);
72 pr_err("reg_r err %d\n", ret
);
73 gspca_dev
->usb_err
= ret
;
75 * Make sure the buffer is zeroed to avoid uninitialized
78 memset(gspca_dev
->usb_buf
, 0, USB_BUF_SZ
);
82 static void reg_w(struct gspca_dev
*gspca_dev
,
87 struct usb_device
*dev
= gspca_dev
->dev
;
90 if (gspca_dev
->usb_err
< 0)
92 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x\n", req
, value
, index
);
93 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
95 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
99 pr_err("reg_w err %d\n", ret
);
100 gspca_dev
->usb_err
= ret
;
104 static void reg_wb(struct gspca_dev
*gspca_dev
,
110 struct usb_device
*dev
= gspca_dev
->dev
;
113 if (gspca_dev
->usb_err
< 0)
115 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x %02x\n",
116 req
, value
, index
, byte
);
117 gspca_dev
->usb_buf
[0] = byte
;
118 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
120 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
122 gspca_dev
->usb_buf
, 1, 500);
124 pr_err("reg_w err %d\n", ret
);
125 gspca_dev
->usb_err
= ret
;
129 static void wait_status_0(struct gspca_dev
*gspca_dev
)
136 reg_r(gspca_dev
, 0x21, 0x0000, 1);
137 if (gspca_dev
->usb_buf
[0] == 0)
142 gspca_err(gspca_dev
, "wait_status_0 timeout\n");
143 gspca_dev
->usb_err
= -ETIME
;
146 static void wait_status_1(struct gspca_dev
*gspca_dev
)
152 reg_r(gspca_dev
, 0x21, 0x0001, 1);
154 if (gspca_dev
->usb_buf
[0] == 1) {
155 reg_wb(gspca_dev
, 0x21, 0x0000, 0x0001, 0x00);
156 reg_r(gspca_dev
, 0x21, 0x0001, 1);
160 gspca_err(gspca_dev
, "wait_status_1 timeout\n");
161 gspca_dev
->usb_err
= -ETIME
;
164 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
166 reg_wb(gspca_dev
, 0xc0, 0x0000, 0x00c0, val
);
169 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
171 reg_wb(gspca_dev
, 0xc1, 0x0000, 0x00c1, val
);
174 static void sethue(struct gspca_dev
*gspca_dev
, s32 val
)
176 reg_wb(gspca_dev
, 0xc2, 0x0000, 0x0000, val
);
179 static void setcolor(struct gspca_dev
*gspca_dev
, s32 val
)
181 reg_wb(gspca_dev
, 0xc3, 0x0000, 0x00c3, val
);
184 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
186 reg_wb(gspca_dev
, 0xc4, 0x0000, 0x00c4, val
);
189 /* this function is called at probe time */
190 static int sd_config(struct gspca_dev
*gspca_dev
,
191 const struct usb_device_id
*id
)
193 gspca_dev
->cam
.cam_mode
= vga_mode
;
194 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
195 gspca_dev
->cam
.npkt
= 128; /* number of packets per ISOC message */
196 /*fixme: 256 in ms-win traces*/
201 /* this function is called at probe and resume time */
202 static int sd_init(struct gspca_dev
*gspca_dev
)
204 reg_w(gspca_dev
, 0x00, 0x0001, 0x2067);
205 reg_w(gspca_dev
, 0x00, 0x00d0, 0x206b);
206 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
207 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
209 reg_w(gspca_dev
, 0x00, 0x00c0, 0x206b);
210 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
211 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
213 reg_r(gspca_dev
, 0x20, 0x0000, 1);
214 reg_r(gspca_dev
, 0x20, 0x0000, 5);
215 reg_r(gspca_dev
, 0x23, 0x0000, 64);
216 gspca_dbg(gspca_dev
, D_PROBE
, "%s%s\n", &gspca_dev
->usb_buf
[0x1c],
217 &gspca_dev
->usb_buf
[0x30]);
218 reg_r(gspca_dev
, 0x23, 0x0001, 64);
219 return gspca_dev
->usb_err
;
222 /* function called at start time before URB creation */
223 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
227 reg_r(gspca_dev
, 0x00, 0x2520, 1);
228 wait_status_0(gspca_dev
);
229 reg_w(gspca_dev
, 0xc5, 0x0003, 0x0000);
230 wait_status_1(gspca_dev
);
232 wait_status_0(gspca_dev
);
233 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
234 reg_wb(gspca_dev
, 0x25, 0x0000, 0x0004, mode
);
235 reg_r(gspca_dev
, 0x25, 0x0004, 1);
236 reg_wb(gspca_dev
, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
237 reg_r(gspca_dev
, 0x27, 0x0000, 1);
240 gspca_dev->alt = 4; * use alternate setting 3 */
242 return gspca_dev
->usb_err
;
245 /* -- start the camera -- */
246 static int sd_start(struct gspca_dev
*gspca_dev
)
248 struct sd
*sd
= (struct sd
*) gspca_dev
;
250 /* initialize the JPEG header */
251 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
252 gspca_dev
->pixfmt
.width
,
253 0x22); /* JPEG 411 */
255 /* the JPEG quality shall be 85% */
256 jpeg_set_qual(sd
->jpeg_hdr
, 85);
258 reg_r(gspca_dev
, 0x00, 0x2520, 1);
261 /* start the capture */
262 wait_status_0(gspca_dev
);
263 reg_w(gspca_dev
, 0x31, 0x0000, 0x0004); /* start request */
264 wait_status_1(gspca_dev
);
265 wait_status_0(gspca_dev
);
269 return gspca_dev
->usb_err
;
272 static void sd_stopN(struct gspca_dev
*gspca_dev
)
274 /* stop the capture */
275 wait_status_0(gspca_dev
);
276 reg_w(gspca_dev
, 0x31, 0x0000, 0x0000); /* stop request */
277 wait_status_1(gspca_dev
);
278 wait_status_0(gspca_dev
);
281 /* move a packet adding 0x00 after 0xff */
282 static void add_packet(struct gspca_dev
*gspca_dev
,
290 if (data
[i
] == 0xff) {
291 gspca_frame_add(gspca_dev
, INTER_PACKET
,
299 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
302 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
303 u8
*data
, /* isoc packet */
304 int len
) /* iso packet length */
306 struct sd
*sd
= (struct sd
*) gspca_dev
;
307 static const u8 ffd9
[] = {0xff, 0xd9};
309 /* image packets start with:
312 * 0x01: even (0) / odd (1) image
313 * 0x02: end of image when set
316 return; /* empty packet */
318 if (data
[1] & 0x02) {
319 sd
->pkt_seq
= !(data
[1] & 1);
320 add_packet(gspca_dev
, data
+ 2, len
- 2);
321 gspca_frame_add(gspca_dev
, LAST_PACKET
,
325 if ((data
[1] & 1) != sd
->pkt_seq
)
327 if (gspca_dev
->last_packet_type
== LAST_PACKET
)
328 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
329 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
330 add_packet(gspca_dev
, data
+ 2, len
- 2);
334 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
337 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
339 struct gspca_dev
*gspca_dev
=
340 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
342 gspca_dev
->usb_err
= 0;
344 if (!gspca_dev
->streaming
)
348 case V4L2_CID_BRIGHTNESS
:
349 setbrightness(gspca_dev
, ctrl
->val
);
351 case V4L2_CID_CONTRAST
:
352 setcontrast(gspca_dev
, ctrl
->val
);
355 sethue(gspca_dev
, ctrl
->val
);
357 case V4L2_CID_SATURATION
:
358 setcolor(gspca_dev
, ctrl
->val
);
360 case V4L2_CID_SHARPNESS
:
361 setsharpness(gspca_dev
, ctrl
->val
);
364 return gspca_dev
->usb_err
;
367 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
371 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
373 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
375 gspca_dev
->vdev
.ctrl_handler
= hdl
;
376 v4l2_ctrl_handler_init(hdl
, 5);
377 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
378 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
379 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
380 V4L2_CID_CONTRAST
, 0, 8, 1, 1);
381 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
382 V4L2_CID_HUE
, 0, 255, 1, 0);
383 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
384 V4L2_CID_SATURATION
, 0, 8, 1, 1);
385 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
386 V4L2_CID_SHARPNESS
, 0, 255, 1, 0);
389 pr_err("Could not initialize controls\n");
395 /* sub-driver description */
396 static const struct sd_desc sd_desc
= {
400 .init_controls
= sd_init_controls
,
401 .isoc_init
= sd_isoc_init
,
404 .pkt_scan
= sd_pkt_scan
,
407 /* -- module initialisation -- */
408 static const struct usb_device_id device_table
[] = {
409 {USB_DEVICE(0x04fc, 0x1528)},
412 MODULE_DEVICE_TABLE(usb
, device_table
);
414 /* -- device connect -- */
415 static int sd_probe(struct usb_interface
*intf
,
416 const struct usb_device_id
*id
)
418 /* the video interface for isochronous transfer is 1 */
419 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
422 return gspca_dev_probe2(intf
, id
, &sd_desc
, sizeof(struct sd
),
426 static struct usb_driver sd_driver
= {
428 .id_table
= device_table
,
430 .disconnect
= gspca_disconnect
,
432 .suspend
= gspca_suspend
,
433 .resume
= gspca_resume
,
434 .reset_resume
= gspca_resume
,
438 module_usb_driver(sd_driver
);