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