1 /*
2 * Fujifilm Finepix subdriver
3 *
4 * Copyright (C) 2008 Frank Zago
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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #define MODULE_NAME "finepix"
20
21 #include "gspca.h"
22
23 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
24 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
25 MODULE_LICENSE("GPL");
26
27 /* Default timeout, in ms */
28 #define FPIX_TIMEOUT 250
29
30 /* Maximum transfer size to use. The windows driver reads by chunks of
31 * 0x2000 bytes, so do the same. Note: reading more seems to work
32 * too. */
33 #define FPIX_MAX_TRANSFER 0x2000
34
35 /* Structure to hold all of our device specific stuff */
36 struct usb_fpix {
37 struct gspca_dev gspca_dev; /* !! must be the first item */
38
39 struct work_struct work_struct;
40 };
41
42 /* Delay after which claim the next frame. If the delay is too small,
43 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
44 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
45 * 30ms is bad while 35ms is perfect. */
46 #define NEXT_FRAME_DELAY 35
47
48 /* These cameras only support 320x200. */
49 static const struct v4l2_pix_format fpix_mode[1] = {
50 { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 .bytesperline = 320,
52 .sizeimage = 320 * 240 * 3 / 8 + 590,
53 .colorspace = V4L2_COLORSPACE_SRGB,
54 .priv = 0}
55 };
56
57 /* send a command to the webcam */
command(struct gspca_dev * gspca_dev,int order)58 static int command(struct gspca_dev *gspca_dev,
59 int order) /* 0: reset, 1: frame request */
60 {
61 static u8 order_values[2][12] = {
62 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
63 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
64 };
65
66 memcpy(gspca_dev->usb_buf, order_values[order], 12);
67 return usb_control_msg(gspca_dev->dev,
68 usb_sndctrlpipe(gspca_dev->dev, 0),
69 USB_REQ_GET_STATUS,
70 USB_DIR_OUT | USB_TYPE_CLASS |
71 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
72 12, FPIX_TIMEOUT);
73 }
74
75 /*
76 * This function is called as a workqueue function and runs whenever the camera
77 * is streaming data. Because it is a workqueue function it is allowed to sleep
78 * so we can use synchronous USB calls. To avoid possible collisions with other
79 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
80 * performing USB operations using it. In practice we don't really need this
81 * as the camera doesn't provide any controls.
82 */
dostream(struct work_struct * work)83 static void dostream(struct work_struct *work)
84 {
85 struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
86 struct gspca_dev *gspca_dev = &dev->gspca_dev;
87 struct urb *urb = gspca_dev->urb[0];
88 u8 *data = urb->transfer_buffer;
89 int ret = 0;
90 int len;
91
92 gspca_dbg(gspca_dev, D_STREAM, "dostream started\n");
93
94 /* loop reading a frame */
95 again:
96 while (gspca_dev->present && gspca_dev->streaming) {
97 #ifdef CONFIG_PM
98 if (gspca_dev->frozen)
99 break;
100 #endif
101
102 /* request a frame */
103 mutex_lock(&gspca_dev->usb_lock);
104 ret = command(gspca_dev, 1);
105 mutex_unlock(&gspca_dev->usb_lock);
106 if (ret < 0)
107 break;
108 #ifdef CONFIG_PM
109 if (gspca_dev->frozen)
110 break;
111 #endif
112 if (!gspca_dev->present || !gspca_dev->streaming)
113 break;
114
115 /* the frame comes in parts */
116 for (;;) {
117 ret = usb_bulk_msg(gspca_dev->dev,
118 urb->pipe,
119 data,
120 FPIX_MAX_TRANSFER,
121 &len, FPIX_TIMEOUT);
122 if (ret < 0) {
123 /* Most of the time we get a timeout
124 * error. Just restart. */
125 goto again;
126 }
127 #ifdef CONFIG_PM
128 if (gspca_dev->frozen)
129 goto out;
130 #endif
131 if (!gspca_dev->present || !gspca_dev->streaming)
132 goto out;
133 if (len < FPIX_MAX_TRANSFER ||
134 (data[len - 2] == 0xff &&
135 data[len - 1] == 0xd9)) {
136
137 /* If the result is less than what was asked
138 * for, then it's the end of the
139 * frame. Sometimes the jpeg is not complete,
140 * but there's nothing we can do. We also end
141 * here if the the jpeg ends right at the end
142 * of the frame. */
143 gspca_frame_add(gspca_dev, LAST_PACKET,
144 data, len);
145 break;
146 }
147
148 /* got a partial image */
149 gspca_frame_add(gspca_dev,
150 gspca_dev->last_packet_type
151 == LAST_PACKET
152 ? FIRST_PACKET : INTER_PACKET,
153 data, len);
154 }
155
156 /* We must wait before trying reading the next
157 * frame. If we don't, or if the delay is too short,
158 * the camera will disconnect. */
159 msleep(NEXT_FRAME_DELAY);
160 }
161
162 out:
163 gspca_dbg(gspca_dev, D_STREAM, "dostream stopped\n");
164 }
165
166 /* this function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)167 static int sd_config(struct gspca_dev *gspca_dev,
168 const struct usb_device_id *id)
169 {
170 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
171 struct cam *cam = &gspca_dev->cam;
172
173 cam->cam_mode = fpix_mode;
174 cam->nmodes = 1;
175 cam->bulk = 1;
176 cam->bulk_size = FPIX_MAX_TRANSFER;
177
178 INIT_WORK(&dev->work_struct, dostream);
179
180 return 0;
181 }
182
183 /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)184 static int sd_init(struct gspca_dev *gspca_dev)
185 {
186 return 0;
187 }
188
189 /* start the camera */
sd_start(struct gspca_dev * gspca_dev)190 static int sd_start(struct gspca_dev *gspca_dev)
191 {
192 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
193 int ret, len;
194
195 /* Init the device */
196 ret = command(gspca_dev, 0);
197 if (ret < 0) {
198 pr_err("init failed %d\n", ret);
199 return ret;
200 }
201
202 /* Read the result of the command. Ignore the result, for it
203 * varies with the device. */
204 ret = usb_bulk_msg(gspca_dev->dev,
205 gspca_dev->urb[0]->pipe,
206 gspca_dev->urb[0]->transfer_buffer,
207 FPIX_MAX_TRANSFER, &len,
208 FPIX_TIMEOUT);
209 if (ret < 0) {
210 pr_err("usb_bulk_msg failed %d\n", ret);
211 return ret;
212 }
213
214 /* Request a frame, but don't read it */
215 ret = command(gspca_dev, 1);
216 if (ret < 0) {
217 pr_err("frame request failed %d\n", ret);
218 return ret;
219 }
220
221 /* Again, reset bulk in endpoint */
222 usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
223
224 schedule_work(&dev->work_struct);
225
226 return 0;
227 }
228
229 /* called on streamoff with alt==0 and on disconnect */
230 /* the usb_lock is held at entry - restore on exit */
sd_stop0(struct gspca_dev * gspca_dev)231 static void sd_stop0(struct gspca_dev *gspca_dev)
232 {
233 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
234
235 /* wait for the work queue to terminate */
236 mutex_unlock(&gspca_dev->usb_lock);
237 flush_work(&dev->work_struct);
238 mutex_lock(&gspca_dev->usb_lock);
239 }
240
241 /* Table of supported USB devices */
242 static const struct usb_device_id device_table[] = {
243 {USB_DEVICE(0x04cb, 0x0104)},
244 {USB_DEVICE(0x04cb, 0x0109)},
245 {USB_DEVICE(0x04cb, 0x010b)},
246 {USB_DEVICE(0x04cb, 0x010f)},
247 {USB_DEVICE(0x04cb, 0x0111)},
248 {USB_DEVICE(0x04cb, 0x0113)},
249 {USB_DEVICE(0x04cb, 0x0115)},
250 {USB_DEVICE(0x04cb, 0x0117)},
251 {USB_DEVICE(0x04cb, 0x0119)},
252 {USB_DEVICE(0x04cb, 0x011b)},
253 {USB_DEVICE(0x04cb, 0x011d)},
254 {USB_DEVICE(0x04cb, 0x0121)},
255 {USB_DEVICE(0x04cb, 0x0123)},
256 {USB_DEVICE(0x04cb, 0x0125)},
257 {USB_DEVICE(0x04cb, 0x0127)},
258 {USB_DEVICE(0x04cb, 0x0129)},
259 {USB_DEVICE(0x04cb, 0x012b)},
260 {USB_DEVICE(0x04cb, 0x012d)},
261 {USB_DEVICE(0x04cb, 0x012f)},
262 {USB_DEVICE(0x04cb, 0x0131)},
263 {USB_DEVICE(0x04cb, 0x013b)},
264 {USB_DEVICE(0x04cb, 0x013d)},
265 {USB_DEVICE(0x04cb, 0x013f)},
266 {}
267 };
268
269 MODULE_DEVICE_TABLE(usb, device_table);
270
271 /* sub-driver description */
272 static const struct sd_desc sd_desc = {
273 .name = MODULE_NAME,
274 .config = sd_config,
275 .init = sd_init,
276 .start = sd_start,
277 .stop0 = sd_stop0,
278 };
279
280 /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)281 static int sd_probe(struct usb_interface *intf,
282 const struct usb_device_id *id)
283 {
284 return gspca_dev_probe(intf, id,
285 &sd_desc,
286 sizeof(struct usb_fpix),
287 THIS_MODULE);
288 }
289
290 static struct usb_driver sd_driver = {
291 .name = MODULE_NAME,
292 .id_table = device_table,
293 .probe = sd_probe,
294 .disconnect = gspca_disconnect,
295 #ifdef CONFIG_PM
296 .suspend = gspca_suspend,
297 .resume = gspca_resume,
298 .reset_resume = gspca_resume,
299 #endif
300 };
301
302 module_usb_driver(sd_driver);
303