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 JFM |
43 | struct work_struct work_struct; |
44 | struct workqueue_struct *work_thread; | |
97076859 FZ |
45 | }; |
46 | ||
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 | |
c0c4c893 JFM |
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 | |
97076859 FZ |
52 | |
53 | /* These cameras only support 320x200. */ | |
cc611b8a | 54 | static const struct v4l2_pix_format fpix_mode[1] = { |
97076859 FZ |
55 | { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, |
56 | .bytesperline = 320, | |
57 | .sizeimage = 320 * 240 * 3 / 8 + 590, | |
58 | .colorspace = V4L2_COLORSPACE_SRGB, | |
59 | .priv = 0} | |
60 | }; | |
61 | ||
c0c4c893 JFM |
62 | /* send a command to the webcam */ |
63 | static int command(struct gspca_dev *gspca_dev, | |
64 | int order) /* 0: reset, 1: frame request */ | |
97076859 | 65 | { |
c0c4c893 JFM |
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 */ | |
69 | }; | |
97076859 | 70 | |
c0c4c893 JFM |
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), | |
74 | USB_REQ_GET_STATUS, | |
75 | USB_DIR_OUT | USB_TYPE_CLASS | | |
76 | USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf, | |
77 | 12, FPIX_TIMEOUT); | |
97076859 FZ |
78 | } |
79 | ||
844db450 HG |
80 | /* |
81 | * This function is called as a workqueue function and runs whenever the camera | |
82 | * is streaming data. Because it is a workqueue function it is allowed to sleep | |
83 | * so we can use synchronous USB calls. To avoid possible collisions with other | |
84 | * threads attempting to use gspca_dev->usb_buf we take the usb_lock when | |
85 | * performing USB operations using it. In practice we don't really need this | |
86 | * as the camera doesn't provide any controls. | |
87 | */ | |
c0c4c893 | 88 | static void dostream(struct work_struct *work) |
97076859 | 89 | { |
c0c4c893 JFM |
90 | struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct); |
91 | struct gspca_dev *gspca_dev = &dev->gspca_dev; | |
92 | struct urb *urb = gspca_dev->urb[0]; | |
93 | u8 *data = urb->transfer_buffer; | |
c0c4c893 JFM |
94 | int ret = 0; |
95 | int len; | |
96 | ||
c0c4c893 JFM |
97 | PDEBUG(D_STREAM, "dostream started"); |
98 | ||
99 | /* loop reading a frame */ | |
100 | again: | |
345321dc | 101 | while (gspca_dev->present && gspca_dev->streaming) { |
4ad34da0 HV |
102 | #ifdef CONFIG_PM |
103 | if (gspca_dev->frozen) | |
104 | break; | |
105 | #endif | |
c0c4c893 JFM |
106 | |
107 | /* request a frame */ | |
108 | mutex_lock(&gspca_dev->usb_lock); | |
109 | ret = command(gspca_dev, 1); | |
110 | mutex_unlock(&gspca_dev->usb_lock); | |
111 | if (ret < 0) | |
112 | break; | |
4ad34da0 HV |
113 | #ifdef CONFIG_PM |
114 | if (gspca_dev->frozen) | |
115 | break; | |
116 | #endif | |
345321dc | 117 | if (!gspca_dev->present || !gspca_dev->streaming) |
c0c4c893 JFM |
118 | break; |
119 | ||
120 | /* the frame comes in parts */ | |
121 | for (;;) { | |
122 | ret = usb_bulk_msg(gspca_dev->dev, | |
123 | urb->pipe, | |
124 | data, | |
125 | FPIX_MAX_TRANSFER, | |
126 | &len, FPIX_TIMEOUT); | |
127 | if (ret < 0) { | |
128 | /* Most of the time we get a timeout | |
129 | * error. Just restart. */ | |
130 | goto again; | |
131 | } | |
4ad34da0 HV |
132 | #ifdef CONFIG_PM |
133 | if (gspca_dev->frozen) | |
134 | goto out; | |
135 | #endif | |
345321dc | 136 | if (!gspca_dev->present || !gspca_dev->streaming) |
c0c4c893 | 137 | goto out; |
c0c4c893 JFM |
138 | if (len < FPIX_MAX_TRANSFER || |
139 | (data[len - 2] == 0xff && | |
140 | data[len - 1] == 0xd9)) { | |
141 | ||
142 | /* If the result is less than what was asked | |
143 | * for, then it's the end of the | |
144 | * frame. Sometimes the jpeg is not complete, | |
145 | * but there's nothing we can do. We also end | |
146 | * here if the the jpeg ends right at the end | |
147 | * of the frame. */ | |
76dd272b JFM |
148 | gspca_frame_add(gspca_dev, LAST_PACKET, |
149 | data, len); | |
c0c4c893 JFM |
150 | break; |
151 | } | |
97076859 FZ |
152 | |
153 | /* got a partial image */ | |
76dd272b JFM |
154 | gspca_frame_add(gspca_dev, |
155 | gspca_dev->last_packet_type | |
156 | == LAST_PACKET | |
157 | ? FIRST_PACKET : INTER_PACKET, | |
158 | data, len); | |
97076859 | 159 | } |
97076859 | 160 | |
c0c4c893 JFM |
161 | /* We must wait before trying reading the next |
162 | * frame. If we don't, or if the delay is too short, | |
163 | * the camera will disconnect. */ | |
164 | msleep(NEXT_FRAME_DELAY); | |
97076859 | 165 | } |
97076859 | 166 | |
c0c4c893 JFM |
167 | out: |
168 | PDEBUG(D_STREAM, "dostream stopped"); | |
97076859 FZ |
169 | } |
170 | ||
171 | /* this function is called at probe time */ | |
172 | static int sd_config(struct gspca_dev *gspca_dev, | |
173 | const struct usb_device_id *id) | |
174 | { | |
c0c4c893 | 175 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
97076859 FZ |
176 | struct cam *cam = &gspca_dev->cam; |
177 | ||
178 | cam->cam_mode = fpix_mode; | |
179 | cam->nmodes = 1; | |
6929dc6b | 180 | cam->bulk = 1; |
97076859 FZ |
181 | cam->bulk_size = FPIX_MAX_TRANSFER; |
182 | ||
c0c4c893 | 183 | INIT_WORK(&dev->work_struct, dostream); |
97076859 | 184 | |
c0c4c893 | 185 | return 0; |
97076859 FZ |
186 | } |
187 | ||
188 | /* this function is called at probe and resume time */ | |
189 | static int sd_init(struct gspca_dev *gspca_dev) | |
190 | { | |
97076859 FZ |
191 | return 0; |
192 | } | |
193 | ||
c0c4c893 | 194 | /* start the camera */ |
97076859 FZ |
195 | static int sd_start(struct gspca_dev *gspca_dev) |
196 | { | |
197 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; | |
c0c4c893 | 198 | int ret, len; |
97076859 | 199 | |
97076859 | 200 | /* Init the device */ |
c0c4c893 JFM |
201 | ret = command(gspca_dev, 0); |
202 | if (ret < 0) { | |
70a42995 | 203 | pr_err("init failed %d\n", ret); |
c0c4c893 | 204 | return ret; |
97076859 FZ |
205 | } |
206 | ||
207 | /* Read the result of the command. Ignore the result, for it | |
208 | * varies with the device. */ | |
209 | ret = usb_bulk_msg(gspca_dev->dev, | |
50e06dee | 210 | gspca_dev->urb[0]->pipe, |
c0c4c893 JFM |
211 | gspca_dev->urb[0]->transfer_buffer, |
212 | FPIX_MAX_TRANSFER, &len, | |
97076859 | 213 | FPIX_TIMEOUT); |
c0c4c893 | 214 | if (ret < 0) { |
70a42995 | 215 | pr_err("usb_bulk_msg failed %d\n", ret); |
c0c4c893 | 216 | return ret; |
97076859 FZ |
217 | } |
218 | ||
219 | /* Request a frame, but don't read it */ | |
c0c4c893 JFM |
220 | ret = command(gspca_dev, 1); |
221 | if (ret < 0) { | |
70a42995 | 222 | pr_err("frame request failed %d\n", ret); |
c0c4c893 | 223 | return ret; |
97076859 FZ |
224 | } |
225 | ||
226 | /* Again, reset bulk in endpoint */ | |
50e06dee | 227 | usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe); |
97076859 | 228 | |
c0c4c893 JFM |
229 | /* Start the workqueue function to do the streaming */ |
230 | dev->work_thread = create_singlethread_workqueue(MODULE_NAME); | |
231 | queue_work(dev->work_thread, &dev->work_struct); | |
97076859 FZ |
232 | |
233 | return 0; | |
c0c4c893 JFM |
234 | } |
235 | ||
236 | /* called on streamoff with alt==0 and on disconnect */ | |
237 | /* the usb_lock is held at entry - restore on exit */ | |
238 | static void sd_stop0(struct gspca_dev *gspca_dev) | |
239 | { | |
240 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; | |
97076859 | 241 | |
c0c4c893 JFM |
242 | /* wait for the work queue to terminate */ |
243 | mutex_unlock(&gspca_dev->usb_lock); | |
244 | destroy_workqueue(dev->work_thread); | |
245 | mutex_lock(&gspca_dev->usb_lock); | |
246 | dev->work_thread = NULL; | |
97076859 FZ |
247 | } |
248 | ||
249 | /* Table of supported USB devices */ | |
95c967c1 | 250 | static const struct usb_device_id device_table[] = { |
97076859 FZ |
251 | {USB_DEVICE(0x04cb, 0x0104)}, |
252 | {USB_DEVICE(0x04cb, 0x0109)}, | |
253 | {USB_DEVICE(0x04cb, 0x010b)}, | |
254 | {USB_DEVICE(0x04cb, 0x010f)}, | |
255 | {USB_DEVICE(0x04cb, 0x0111)}, | |
256 | {USB_DEVICE(0x04cb, 0x0113)}, | |
257 | {USB_DEVICE(0x04cb, 0x0115)}, | |
258 | {USB_DEVICE(0x04cb, 0x0117)}, | |
259 | {USB_DEVICE(0x04cb, 0x0119)}, | |
260 | {USB_DEVICE(0x04cb, 0x011b)}, | |
261 | {USB_DEVICE(0x04cb, 0x011d)}, | |
262 | {USB_DEVICE(0x04cb, 0x0121)}, | |
263 | {USB_DEVICE(0x04cb, 0x0123)}, | |
264 | {USB_DEVICE(0x04cb, 0x0125)}, | |
265 | {USB_DEVICE(0x04cb, 0x0127)}, | |
266 | {USB_DEVICE(0x04cb, 0x0129)}, | |
267 | {USB_DEVICE(0x04cb, 0x012b)}, | |
268 | {USB_DEVICE(0x04cb, 0x012d)}, | |
269 | {USB_DEVICE(0x04cb, 0x012f)}, | |
270 | {USB_DEVICE(0x04cb, 0x0131)}, | |
271 | {USB_DEVICE(0x04cb, 0x013b)}, | |
272 | {USB_DEVICE(0x04cb, 0x013d)}, | |
273 | {USB_DEVICE(0x04cb, 0x013f)}, | |
274 | {} | |
275 | }; | |
276 | ||
277 | MODULE_DEVICE_TABLE(usb, device_table); | |
278 | ||
279 | /* sub-driver description */ | |
280 | static const struct sd_desc sd_desc = { | |
c0c4c893 | 281 | .name = MODULE_NAME, |
97076859 | 282 | .config = sd_config, |
c0c4c893 JFM |
283 | .init = sd_init, |
284 | .start = sd_start, | |
285 | .stop0 = sd_stop0, | |
97076859 FZ |
286 | }; |
287 | ||
288 | /* -- device connect -- */ | |
289 | static int sd_probe(struct usb_interface *intf, | |
290 | const struct usb_device_id *id) | |
291 | { | |
292 | return gspca_dev_probe(intf, id, | |
293 | &sd_desc, | |
294 | sizeof(struct usb_fpix), | |
295 | THIS_MODULE); | |
296 | } | |
297 | ||
298 | static struct usb_driver sd_driver = { | |
c0c4c893 JFM |
299 | .name = MODULE_NAME, |
300 | .id_table = device_table, | |
301 | .probe = sd_probe, | |
97076859 FZ |
302 | .disconnect = gspca_disconnect, |
303 | #ifdef CONFIG_PM | |
304 | .suspend = gspca_suspend, | |
c0c4c893 | 305 | .resume = gspca_resume, |
8bb58964 | 306 | .reset_resume = gspca_resume, |
97076859 FZ |
307 | #endif |
308 | }; | |
309 | ||
ecb3b2b3 | 310 | module_usb_driver(sd_driver); |