1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Fujifilm Finepix subdriver
5 * Copyright (C) 2008 Frank Zago
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define MODULE_NAME "finepix"
14 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
15 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
16 MODULE_LICENSE("GPL");
18 /* Default timeout, in ms */
19 #define FPIX_TIMEOUT 250
21 /* Maximum transfer size to use. The windows driver reads by chunks of
22 * 0x2000 bytes, so do the same. Note: reading more seems to work
24 #define FPIX_MAX_TRANSFER 0x2000
26 /* Structure to hold all of our device specific stuff */
28 struct gspca_dev gspca_dev
; /* !! must be the first item */
30 struct work_struct work_struct
;
33 /* Delay after which claim the next frame. If the delay is too small,
34 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
35 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
36 * 30ms is bad while 35ms is perfect. */
37 #define NEXT_FRAME_DELAY 35
39 /* These cameras only support 320x200. */
40 static const struct v4l2_pix_format fpix_mode
[1] = {
41 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
43 .sizeimage
= 320 * 240 * 3 / 8 + 590,
44 .colorspace
= V4L2_COLORSPACE_SRGB
,
48 /* send a command to the webcam */
49 static int command(struct gspca_dev
*gspca_dev
,
50 int order
) /* 0: reset, 1: frame request */
52 static u8 order_values
[2][12] = {
53 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
54 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
57 memcpy(gspca_dev
->usb_buf
, order_values
[order
], 12);
58 return usb_control_msg(gspca_dev
->dev
,
59 usb_sndctrlpipe(gspca_dev
->dev
, 0),
61 USB_DIR_OUT
| USB_TYPE_CLASS
|
62 USB_RECIP_INTERFACE
, 0, 0, gspca_dev
->usb_buf
,
67 * This function is called as a workqueue function and runs whenever the camera
68 * is streaming data. Because it is a workqueue function it is allowed to sleep
69 * so we can use synchronous USB calls. To avoid possible collisions with other
70 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
71 * performing USB operations using it. In practice we don't really need this
72 * as the camera doesn't provide any controls.
74 static void dostream(struct work_struct
*work
)
76 struct usb_fpix
*dev
= container_of(work
, struct usb_fpix
, work_struct
);
77 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
78 struct urb
*urb
= gspca_dev
->urb
[0];
79 u8
*data
= urb
->transfer_buffer
;
83 gspca_dbg(gspca_dev
, D_STREAM
, "dostream started\n");
85 /* loop reading a frame */
87 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
89 if (gspca_dev
->frozen
)
94 mutex_lock(&gspca_dev
->usb_lock
);
95 ret
= command(gspca_dev
, 1);
96 mutex_unlock(&gspca_dev
->usb_lock
);
100 if (gspca_dev
->frozen
)
103 if (!gspca_dev
->present
|| !gspca_dev
->streaming
)
106 /* the frame comes in parts */
108 ret
= usb_bulk_msg(gspca_dev
->dev
,
114 /* Most of the time we get a timeout
115 * error. Just restart. */
119 if (gspca_dev
->frozen
)
122 if (!gspca_dev
->present
|| !gspca_dev
->streaming
)
124 if (len
< FPIX_MAX_TRANSFER
||
125 (data
[len
- 2] == 0xff &&
126 data
[len
- 1] == 0xd9)) {
128 /* If the result is less than what was asked
129 * for, then it's the end of the
130 * frame. Sometimes the jpeg is not complete,
131 * but there's nothing we can do. We also end
132 * here if the the jpeg ends right at the end
134 gspca_frame_add(gspca_dev
, LAST_PACKET
,
139 /* got a partial image */
140 gspca_frame_add(gspca_dev
,
141 gspca_dev
->last_packet_type
143 ? FIRST_PACKET
: INTER_PACKET
,
147 /* We must wait before trying reading the next
148 * frame. If we don't, or if the delay is too short,
149 * the camera will disconnect. */
150 msleep(NEXT_FRAME_DELAY
);
154 gspca_dbg(gspca_dev
, D_STREAM
, "dostream stopped\n");
157 /* this function is called at probe time */
158 static int sd_config(struct gspca_dev
*gspca_dev
,
159 const struct usb_device_id
*id
)
161 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
162 struct cam
*cam
= &gspca_dev
->cam
;
164 cam
->cam_mode
= fpix_mode
;
167 cam
->bulk_size
= FPIX_MAX_TRANSFER
;
169 INIT_WORK(&dev
->work_struct
, dostream
);
174 /* this function is called at probe and resume time */
175 static int sd_init(struct gspca_dev
*gspca_dev
)
180 /* start the camera */
181 static int sd_start(struct gspca_dev
*gspca_dev
)
183 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
186 /* Init the device */
187 ret
= command(gspca_dev
, 0);
189 pr_err("init failed %d\n", ret
);
193 /* Read the result of the command. Ignore the result, for it
194 * varies with the device. */
195 ret
= usb_bulk_msg(gspca_dev
->dev
,
196 gspca_dev
->urb
[0]->pipe
,
197 gspca_dev
->urb
[0]->transfer_buffer
,
198 FPIX_MAX_TRANSFER
, &len
,
201 pr_err("usb_bulk_msg failed %d\n", ret
);
205 /* Request a frame, but don't read it */
206 ret
= command(gspca_dev
, 1);
208 pr_err("frame request failed %d\n", ret
);
212 /* Again, reset bulk in endpoint */
213 usb_clear_halt(gspca_dev
->dev
, gspca_dev
->urb
[0]->pipe
);
215 schedule_work(&dev
->work_struct
);
220 /* called on streamoff with alt==0 and on disconnect */
221 /* the usb_lock is held at entry - restore on exit */
222 static void sd_stop0(struct gspca_dev
*gspca_dev
)
224 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
226 /* wait for the work queue to terminate */
227 mutex_unlock(&gspca_dev
->usb_lock
);
228 flush_work(&dev
->work_struct
);
229 mutex_lock(&gspca_dev
->usb_lock
);
232 /* Table of supported USB devices */
233 static const struct usb_device_id device_table
[] = {
234 {USB_DEVICE(0x04cb, 0x0104)},
235 {USB_DEVICE(0x04cb, 0x0109)},
236 {USB_DEVICE(0x04cb, 0x010b)},
237 {USB_DEVICE(0x04cb, 0x010f)},
238 {USB_DEVICE(0x04cb, 0x0111)},
239 {USB_DEVICE(0x04cb, 0x0113)},
240 {USB_DEVICE(0x04cb, 0x0115)},
241 {USB_DEVICE(0x04cb, 0x0117)},
242 {USB_DEVICE(0x04cb, 0x0119)},
243 {USB_DEVICE(0x04cb, 0x011b)},
244 {USB_DEVICE(0x04cb, 0x011d)},
245 {USB_DEVICE(0x04cb, 0x0121)},
246 {USB_DEVICE(0x04cb, 0x0123)},
247 {USB_DEVICE(0x04cb, 0x0125)},
248 {USB_DEVICE(0x04cb, 0x0127)},
249 {USB_DEVICE(0x04cb, 0x0129)},
250 {USB_DEVICE(0x04cb, 0x012b)},
251 {USB_DEVICE(0x04cb, 0x012d)},
252 {USB_DEVICE(0x04cb, 0x012f)},
253 {USB_DEVICE(0x04cb, 0x0131)},
254 {USB_DEVICE(0x04cb, 0x013b)},
255 {USB_DEVICE(0x04cb, 0x013d)},
256 {USB_DEVICE(0x04cb, 0x013f)},
260 MODULE_DEVICE_TABLE(usb
, device_table
);
262 /* sub-driver description */
263 static const struct sd_desc sd_desc
= {
271 /* -- device connect -- */
272 static int sd_probe(struct usb_interface
*intf
,
273 const struct usb_device_id
*id
)
275 return gspca_dev_probe(intf
, id
,
277 sizeof(struct usb_fpix
),
281 static struct usb_driver sd_driver
= {
283 .id_table
= device_table
,
285 .disconnect
= gspca_disconnect
,
287 .suspend
= gspca_suspend
,
288 .resume
= gspca_resume
,
289 .reset_resume
= gspca_resume
,
293 module_usb_driver(sd_driver
);