2 * Fujifilm Finepix subdriver
4 * Copyright (C) 2008 Frank Zago
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
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.
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
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #define MODULE_NAME "finepix"
27 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
28 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
29 MODULE_LICENSE("GPL");
31 /* Default timeout, in ms */
32 #define FPIX_TIMEOUT 250
34 /* Maximum transfer size to use. The windows driver reads by chunks of
35 * 0x2000 bytes, so do the same. Note: reading more seems to work
37 #define FPIX_MAX_TRANSFER 0x2000
39 /* Structure to hold all of our device specific stuff */
41 struct gspca_dev gspca_dev; /* !! must be the first item */
43 struct work_struct work_struct;
44 struct workqueue_struct *work_thread;
47 /* Delay after which claim the next frame. If the delay is too small,
48 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
49 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
50 * 30ms is bad while 35ms is perfect. */
51 #define NEXT_FRAME_DELAY 35
53 /* These cameras only support 320x200. */
54 static const struct v4l2_pix_format fpix_mode[1] = {
55 { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
57 .sizeimage = 320 * 240 * 3 / 8 + 590,
58 .colorspace = V4L2_COLORSPACE_SRGB,
62 /* send a command to the webcam */
63 static int command(struct gspca_dev *gspca_dev,
64 int order) /* 0: reset, 1: frame request */
66 static u8 order_values[2][12] = {
67 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
68 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
71 memcpy(gspca_dev->usb_buf, order_values[order], 12);
72 return usb_control_msg(gspca_dev->dev,
73 usb_sndctrlpipe(gspca_dev->dev, 0),
75 USB_DIR_OUT | USB_TYPE_CLASS |
76 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
81 static void dostream(struct work_struct *work)
83 struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
84 struct gspca_dev *gspca_dev = &dev->gspca_dev;
85 struct urb *urb = gspca_dev->urb[0];
86 u8 *data = urb->transfer_buffer;
90 /* synchronize with the main driver */
91 mutex_lock(&gspca_dev->usb_lock);
92 mutex_unlock(&gspca_dev->usb_lock);
93 PDEBUG(D_STREAM, "dostream started");
95 /* loop reading a frame */
97 while (gspca_dev->dev && gspca_dev->streaming) {
99 if (gspca_dev->frozen)
103 /* request a frame */
104 mutex_lock(&gspca_dev->usb_lock);
105 ret = command(gspca_dev, 1);
106 mutex_unlock(&gspca_dev->usb_lock);
110 if (gspca_dev->frozen)
113 if (!gspca_dev->dev || !gspca_dev->streaming)
116 /* the frame comes in parts */
118 ret = usb_bulk_msg(gspca_dev->dev,
124 /* Most of the time we get a timeout
125 * error. Just restart. */
129 if (gspca_dev->frozen)
132 if (!gspca_dev->dev || !gspca_dev->streaming)
134 if (len < FPIX_MAX_TRANSFER ||
135 (data[len - 2] == 0xff &&
136 data[len - 1] == 0xd9)) {
138 /* If the result is less than what was asked
139 * for, then it's the end of the
140 * frame. Sometimes the jpeg is not complete,
141 * but there's nothing we can do. We also end
142 * here if the the jpeg ends right at the end
144 gspca_frame_add(gspca_dev, LAST_PACKET,
149 /* got a partial image */
150 gspca_frame_add(gspca_dev,
151 gspca_dev->last_packet_type
153 ? FIRST_PACKET : INTER_PACKET,
157 /* We must wait before trying reading the next
158 * frame. If we don't, or if the delay is too short,
159 * the camera will disconnect. */
160 msleep(NEXT_FRAME_DELAY);
164 PDEBUG(D_STREAM, "dostream stopped");
167 /* this function is called at probe time */
168 static int sd_config(struct gspca_dev *gspca_dev,
169 const struct usb_device_id *id)
171 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
172 struct cam *cam = &gspca_dev->cam;
174 cam->cam_mode = fpix_mode;
177 cam->bulk_size = FPIX_MAX_TRANSFER;
179 INIT_WORK(&dev->work_struct, dostream);
184 /* this function is called at probe and resume time */
185 static int sd_init(struct gspca_dev *gspca_dev)
190 /* start the camera */
191 static int sd_start(struct gspca_dev *gspca_dev)
193 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
196 /* Init the device */
197 ret = command(gspca_dev, 0);
199 pr_err("init failed %d\n", ret);
203 /* Read the result of the command. Ignore the result, for it
204 * varies with the device. */
205 ret = usb_bulk_msg(gspca_dev->dev,
206 gspca_dev->urb[0]->pipe,
207 gspca_dev->urb[0]->transfer_buffer,
208 FPIX_MAX_TRANSFER, &len,
211 pr_err("usb_bulk_msg failed %d\n", ret);
215 /* Request a frame, but don't read it */
216 ret = command(gspca_dev, 1);
218 pr_err("frame request failed %d\n", ret);
222 /* Again, reset bulk in endpoint */
223 usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
225 /* Start the workqueue function to do the streaming */
226 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
227 queue_work(dev->work_thread, &dev->work_struct);
232 /* called on streamoff with alt==0 and on disconnect */
233 /* the usb_lock is held at entry - restore on exit */
234 static void sd_stop0(struct gspca_dev *gspca_dev)
236 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
238 /* wait for the work queue to terminate */
239 mutex_unlock(&gspca_dev->usb_lock);
240 destroy_workqueue(dev->work_thread);
241 mutex_lock(&gspca_dev->usb_lock);
242 dev->work_thread = NULL;
245 /* Table of supported USB devices */
246 static const struct usb_device_id device_table[] = {
247 {USB_DEVICE(0x04cb, 0x0104)},
248 {USB_DEVICE(0x04cb, 0x0109)},
249 {USB_DEVICE(0x04cb, 0x010b)},
250 {USB_DEVICE(0x04cb, 0x010f)},
251 {USB_DEVICE(0x04cb, 0x0111)},
252 {USB_DEVICE(0x04cb, 0x0113)},
253 {USB_DEVICE(0x04cb, 0x0115)},
254 {USB_DEVICE(0x04cb, 0x0117)},
255 {USB_DEVICE(0x04cb, 0x0119)},
256 {USB_DEVICE(0x04cb, 0x011b)},
257 {USB_DEVICE(0x04cb, 0x011d)},
258 {USB_DEVICE(0x04cb, 0x0121)},
259 {USB_DEVICE(0x04cb, 0x0123)},
260 {USB_DEVICE(0x04cb, 0x0125)},
261 {USB_DEVICE(0x04cb, 0x0127)},
262 {USB_DEVICE(0x04cb, 0x0129)},
263 {USB_DEVICE(0x04cb, 0x012b)},
264 {USB_DEVICE(0x04cb, 0x012d)},
265 {USB_DEVICE(0x04cb, 0x012f)},
266 {USB_DEVICE(0x04cb, 0x0131)},
267 {USB_DEVICE(0x04cb, 0x013b)},
268 {USB_DEVICE(0x04cb, 0x013d)},
269 {USB_DEVICE(0x04cb, 0x013f)},
273 MODULE_DEVICE_TABLE(usb, device_table);
275 /* sub-driver description */
276 static const struct sd_desc sd_desc = {
284 /* -- device connect -- */
285 static int sd_probe(struct usb_interface *intf,
286 const struct usb_device_id *id)
288 return gspca_dev_probe(intf, id,
290 sizeof(struct usb_fpix),
294 static struct usb_driver sd_driver = {
296 .id_table = device_table,
298 .disconnect = gspca_disconnect,
300 .suspend = gspca_suspend,
301 .resume = gspca_resume,
302 .reset_resume = gspca_resume,
306 module_usb_driver(sd_driver);