blob: 00e6863ed66692d20581b88559041084fed917ad [file] [log] [blame]
Frank Zago97076852008-09-29 06:59:36 -03001/*
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
21#define MODULE_NAME "finepix"
22
23#include "gspca.h"
24
25MODULE_AUTHOR("Frank Zago <frank@zago.net>");
26MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
27MODULE_LICENSE("GPL");
28
29/* Default timeout, in ms */
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030030#define FPIX_TIMEOUT 250
Frank Zago97076852008-09-29 06:59:36 -030031
32/* Maximum transfer size to use. The windows driver reads by chunks of
33 * 0x2000 bytes, so do the same. Note: reading more seems to work
34 * too. */
35#define FPIX_MAX_TRANSFER 0x2000
36
37/* Structure to hold all of our device specific stuff */
38struct usb_fpix {
39 struct gspca_dev gspca_dev; /* !! must be the first item */
40
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030041 struct work_struct work_struct;
42 struct workqueue_struct *work_thread;
Frank Zago97076852008-09-29 06:59:36 -030043};
44
45/* Delay after which claim the next frame. If the delay is too small,
46 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030047 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
48 * 30ms is bad while 35ms is perfect. */
49#define NEXT_FRAME_DELAY 35
Frank Zago97076852008-09-29 06:59:36 -030050
51/* These cameras only support 320x200. */
Jean-Francois Moinecc611b82008-12-29 07:49:41 -030052static const struct v4l2_pix_format fpix_mode[1] = {
Frank Zago97076852008-09-29 06:59:36 -030053 { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
54 .bytesperline = 320,
55 .sizeimage = 320 * 240 * 3 / 8 + 590,
56 .colorspace = V4L2_COLORSPACE_SRGB,
57 .priv = 0}
58};
59
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030060/* send a command to the webcam */
61static int command(struct gspca_dev *gspca_dev,
62 int order) /* 0: reset, 1: frame request */
Frank Zago97076852008-09-29 06:59:36 -030063{
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030064 static u8 order_values[2][12] = {
65 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
66 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
67 };
Frank Zago97076852008-09-29 06:59:36 -030068
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030069 memcpy(gspca_dev->usb_buf, order_values[order], 12);
70 return usb_control_msg(gspca_dev->dev,
71 usb_sndctrlpipe(gspca_dev->dev, 0),
72 USB_REQ_GET_STATUS,
73 USB_DIR_OUT | USB_TYPE_CLASS |
74 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
75 12, FPIX_TIMEOUT);
Frank Zago97076852008-09-29 06:59:36 -030076}
77
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030078/* workqueue */
79static void dostream(struct work_struct *work)
Frank Zago97076852008-09-29 06:59:36 -030080{
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030081 struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
82 struct gspca_dev *gspca_dev = &dev->gspca_dev;
83 struct urb *urb = gspca_dev->urb[0];
84 u8 *data = urb->transfer_buffer;
85 struct gspca_frame *frame;
86 int ret = 0;
87 int len;
Frank Zago97076852008-09-29 06:59:36 -030088
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030089 /* synchronize with the main driver */
90 mutex_lock(&gspca_dev->usb_lock);
91 mutex_unlock(&gspca_dev->usb_lock);
92 PDEBUG(D_STREAM, "dostream started");
Frank Zago97076852008-09-29 06:59:36 -030093
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030094 /* loop reading a frame */
95again:
96 while (gspca_dev->present && gspca_dev->streaming) {
Frank Zago97076852008-09-29 06:59:36 -030097
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -030098 /* request a frame */
99 mutex_lock(&gspca_dev->usb_lock);
100 ret = command(gspca_dev, 1);
101 mutex_unlock(&gspca_dev->usb_lock);
102 if (ret < 0)
103 break;
104 if (!gspca_dev->present || !gspca_dev->streaming)
105 break;
Frank Zago97076852008-09-29 06:59:36 -0300106
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300107 /* the frame comes in parts */
108 for (;;) {
109 ret = usb_bulk_msg(gspca_dev->dev,
110 urb->pipe,
111 data,
112 FPIX_MAX_TRANSFER,
113 &len, FPIX_TIMEOUT);
114 if (ret < 0) {
115 /* Most of the time we get a timeout
116 * error. Just restart. */
117 goto again;
118 }
119 if (!gspca_dev->present || !gspca_dev->streaming)
120 goto out;
121 frame = gspca_get_i_frame(&dev->gspca_dev);
122 if (frame == NULL)
123 gspca_dev->last_packet_type = DISCARD_PACKET;
Frank Zago97076852008-09-29 06:59:36 -0300124
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300125 if (len < FPIX_MAX_TRANSFER ||
126 (data[len - 2] == 0xff &&
127 data[len - 1] == 0xd9)) {
Frank Zago97076852008-09-29 06:59:36 -0300128
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300129 /* If the result is less than what was asked
130 * for, then it's the end of the
131 * frame. Sometimes the jpeg is not complete,
132 * but there's nothing we can do. We also end
133 * here if the the jpeg ends right at the end
134 * of the frame. */
135 if (frame)
136 frame = gspca_frame_add(gspca_dev,
137 LAST_PACKET,
138 frame,
139 data, len);
140 break;
141 }
Frank Zago97076852008-09-29 06:59:36 -0300142
143 /* got a partial image */
Frank Zagoa3a58462008-09-30 03:55:33 -0300144 if (frame)
145 gspca_frame_add(gspca_dev,
146 gspca_dev->last_packet_type
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300147 == LAST_PACKET
Frank Zagoa3a58462008-09-30 03:55:33 -0300148 ? FIRST_PACKET : INTER_PACKET,
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300149 frame, data, len);
Frank Zago97076852008-09-29 06:59:36 -0300150 }
Frank Zago97076852008-09-29 06:59:36 -0300151
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300152 /* We must wait before trying reading the next
153 * frame. If we don't, or if the delay is too short,
154 * the camera will disconnect. */
155 msleep(NEXT_FRAME_DELAY);
Frank Zago97076852008-09-29 06:59:36 -0300156 }
157
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300158out:
159 PDEBUG(D_STREAM, "dostream stopped");
Frank Zago97076852008-09-29 06:59:36 -0300160}
161
162/* this function is called at probe time */
163static int sd_config(struct gspca_dev *gspca_dev,
164 const struct usb_device_id *id)
165{
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300166 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
Frank Zago97076852008-09-29 06:59:36 -0300167 struct cam *cam = &gspca_dev->cam;
168
169 cam->cam_mode = fpix_mode;
170 cam->nmodes = 1;
Frank Zago97076852008-09-29 06:59:36 -0300171 cam->bulk_size = FPIX_MAX_TRANSFER;
172
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300173 INIT_WORK(&dev->work_struct, dostream);
174
Frank Zago97076852008-09-29 06:59:36 -0300175 return 0;
176}
177
Frank Zago97076852008-09-29 06:59:36 -0300178/* this function is called at probe and resume time */
179static int sd_init(struct gspca_dev *gspca_dev)
180{
Frank Zago97076852008-09-29 06:59:36 -0300181 return 0;
182}
183
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300184/* start the camera */
Frank Zago97076852008-09-29 06:59:36 -0300185static int sd_start(struct gspca_dev *gspca_dev)
186{
187 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300188 int ret, len;
Frank Zago97076852008-09-29 06:59:36 -0300189
Frank Zago97076852008-09-29 06:59:36 -0300190 /* Init the device */
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300191 ret = command(gspca_dev, 0);
192 if (ret < 0) {
193 PDEBUG(D_STREAM, "init failed %d", ret);
194 return ret;
Frank Zago97076852008-09-29 06:59:36 -0300195 }
196
197 /* Read the result of the command. Ignore the result, for it
198 * varies with the device. */
199 ret = usb_bulk_msg(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300200 gspca_dev->urb[0]->pipe,
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300201 gspca_dev->urb[0]->transfer_buffer,
202 FPIX_MAX_TRANSFER, &len,
Frank Zago97076852008-09-29 06:59:36 -0300203 FPIX_TIMEOUT);
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300204 if (ret < 0) {
205 PDEBUG(D_STREAM, "usb_bulk_msg failed %d", ret);
206 return ret;
Frank Zago97076852008-09-29 06:59:36 -0300207 }
208
209 /* Request a frame, but don't read it */
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300210 ret = command(gspca_dev, 1);
211 if (ret < 0) {
212 PDEBUG(D_STREAM, "frame request failed %d", ret);
213 return ret;
Frank Zago97076852008-09-29 06:59:36 -0300214 }
215
216 /* Again, reset bulk in endpoint */
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300217 usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
Frank Zago97076852008-09-29 06:59:36 -0300218
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300219 /* Start the workqueue function to do the streaming */
220 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
221 queue_work(dev->work_thread, &dev->work_struct);
Frank Zago97076852008-09-29 06:59:36 -0300222
223 return 0;
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300224}
Frank Zago97076852008-09-29 06:59:36 -0300225
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300226/* called on streamoff with alt==0 and on disconnect */
227/* the usb_lock is held at entry - restore on exit */
228static void sd_stop0(struct gspca_dev *gspca_dev)
229{
230 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
231
232 /* wait for the work queue to terminate */
233 mutex_unlock(&gspca_dev->usb_lock);
234 destroy_workqueue(dev->work_thread);
235 mutex_lock(&gspca_dev->usb_lock);
236 dev->work_thread = NULL;
Frank Zago97076852008-09-29 06:59:36 -0300237}
238
239/* Table of supported USB devices */
240static const __devinitdata struct usb_device_id device_table[] = {
241 {USB_DEVICE(0x04cb, 0x0104)},
242 {USB_DEVICE(0x04cb, 0x0109)},
243 {USB_DEVICE(0x04cb, 0x010b)},
244 {USB_DEVICE(0x04cb, 0x010f)},
245 {USB_DEVICE(0x04cb, 0x0111)},
246 {USB_DEVICE(0x04cb, 0x0113)},
247 {USB_DEVICE(0x04cb, 0x0115)},
248 {USB_DEVICE(0x04cb, 0x0117)},
249 {USB_DEVICE(0x04cb, 0x0119)},
250 {USB_DEVICE(0x04cb, 0x011b)},
251 {USB_DEVICE(0x04cb, 0x011d)},
252 {USB_DEVICE(0x04cb, 0x0121)},
253 {USB_DEVICE(0x04cb, 0x0123)},
254 {USB_DEVICE(0x04cb, 0x0125)},
255 {USB_DEVICE(0x04cb, 0x0127)},
256 {USB_DEVICE(0x04cb, 0x0129)},
257 {USB_DEVICE(0x04cb, 0x012b)},
258 {USB_DEVICE(0x04cb, 0x012d)},
259 {USB_DEVICE(0x04cb, 0x012f)},
260 {USB_DEVICE(0x04cb, 0x0131)},
261 {USB_DEVICE(0x04cb, 0x013b)},
262 {USB_DEVICE(0x04cb, 0x013d)},
263 {USB_DEVICE(0x04cb, 0x013f)},
264 {}
265};
266
267MODULE_DEVICE_TABLE(usb, device_table);
268
269/* sub-driver description */
270static const struct sd_desc sd_desc = {
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300271 .name = MODULE_NAME,
Frank Zago97076852008-09-29 06:59:36 -0300272 .config = sd_config,
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300273 .init = sd_init,
274 .start = sd_start,
275 .stop0 = sd_stop0,
Frank Zago97076852008-09-29 06:59:36 -0300276};
277
278/* -- device connect -- */
279static int sd_probe(struct usb_interface *intf,
280 const struct usb_device_id *id)
281{
282 return gspca_dev_probe(intf, id,
283 &sd_desc,
284 sizeof(struct usb_fpix),
285 THIS_MODULE);
286}
287
288static struct usb_driver sd_driver = {
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300289 .name = MODULE_NAME,
290 .id_table = device_table,
291 .probe = sd_probe,
Frank Zago97076852008-09-29 06:59:36 -0300292 .disconnect = gspca_disconnect,
293#ifdef CONFIG_PM
294 .suspend = gspca_suspend,
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300295 .resume = gspca_resume,
Frank Zago97076852008-09-29 06:59:36 -0300296#endif
297};
298
299/* -- module insert / remove -- */
300static int __init sd_mod_init(void)
301{
Alexey Klimovf69e9522009-01-01 13:02:07 -0300302 int ret;
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300303
Alexey Klimovf69e9522009-01-01 13:02:07 -0300304 ret = usb_register(&sd_driver);
305 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -0300306 return ret;
Frank Zago97076852008-09-29 06:59:36 -0300307 PDEBUG(D_PROBE, "registered");
308 return 0;
309}
Jean-Francois Moinec0c4c892009-03-26 05:06:50 -0300310
Frank Zago97076852008-09-29 06:59:36 -0300311static void __exit sd_mod_exit(void)
312{
313 usb_deregister(&sd_driver);
314 PDEBUG(D_PROBE, "deregistered");
315}
316
317module_init(sd_mod_init);
318module_exit(sd_mod_exit);