Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / gspca / finepix.c
blob987b4b69d7ab2a71a06d7dc0c39b235646ef47a2
1 /*
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
9 * any later version.
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 MODULE_NAME "finepix"
23 #include "gspca.h"
25 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
26 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
27 MODULE_LICENSE("GPL");
29 /* Default timeout, in ms */
30 #define FPIX_TIMEOUT 250
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
37 /* Structure to hold all of our device specific stuff */
38 struct usb_fpix {
39 struct gspca_dev gspca_dev; /* !! must be the first item */
41 struct work_struct work_struct;
42 struct workqueue_struct *work_thread;
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
47 * 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
51 /* These cameras only support 320x200. */
52 static const struct v4l2_pix_format fpix_mode[1] = {
53 { 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}
60 /* send a command to the webcam */
61 static int command(struct gspca_dev *gspca_dev,
62 int order) /* 0: reset, 1: frame request */
64 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 */
69 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);
78 /* workqueue */
79 static void dostream(struct work_struct *work)
81 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 int ret = 0;
86 int len;
88 /* synchronize with the main driver */
89 mutex_lock(&gspca_dev->usb_lock);
90 mutex_unlock(&gspca_dev->usb_lock);
91 PDEBUG(D_STREAM, "dostream started");
93 /* loop reading a frame */
94 again:
95 while (gspca_dev->present && gspca_dev->streaming) {
97 /* request a frame */
98 mutex_lock(&gspca_dev->usb_lock);
99 ret = command(gspca_dev, 1);
100 mutex_unlock(&gspca_dev->usb_lock);
101 if (ret < 0)
102 break;
103 if (!gspca_dev->present || !gspca_dev->streaming)
104 break;
106 /* the frame comes in parts */
107 for (;;) {
108 ret = usb_bulk_msg(gspca_dev->dev,
109 urb->pipe,
110 data,
111 FPIX_MAX_TRANSFER,
112 &len, FPIX_TIMEOUT);
113 if (ret < 0) {
114 /* Most of the time we get a timeout
115 * error. Just restart. */
116 goto again;
118 if (!gspca_dev->present || !gspca_dev->streaming)
119 goto out;
120 if (len < FPIX_MAX_TRANSFER ||
121 (data[len - 2] == 0xff &&
122 data[len - 1] == 0xd9)) {
124 /* If the result is less than what was asked
125 * for, then it's the end of the
126 * frame. Sometimes the jpeg is not complete,
127 * but there's nothing we can do. We also end
128 * here if the the jpeg ends right at the end
129 * of the frame. */
130 gspca_frame_add(gspca_dev, LAST_PACKET,
131 data, len);
132 break;
135 /* got a partial image */
136 gspca_frame_add(gspca_dev,
137 gspca_dev->last_packet_type
138 == LAST_PACKET
139 ? FIRST_PACKET : INTER_PACKET,
140 data, len);
143 /* We must wait before trying reading the next
144 * frame. If we don't, or if the delay is too short,
145 * the camera will disconnect. */
146 msleep(NEXT_FRAME_DELAY);
149 out:
150 PDEBUG(D_STREAM, "dostream stopped");
153 /* this function is called at probe time */
154 static int sd_config(struct gspca_dev *gspca_dev,
155 const struct usb_device_id *id)
157 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
158 struct cam *cam = &gspca_dev->cam;
160 cam->cam_mode = fpix_mode;
161 cam->nmodes = 1;
162 cam->bulk = 1;
163 cam->bulk_size = FPIX_MAX_TRANSFER;
165 INIT_WORK(&dev->work_struct, dostream);
167 return 0;
170 /* this function is called at probe and resume time */
171 static int sd_init(struct gspca_dev *gspca_dev)
173 return 0;
176 /* start the camera */
177 static int sd_start(struct gspca_dev *gspca_dev)
179 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
180 int ret, len;
182 /* Init the device */
183 ret = command(gspca_dev, 0);
184 if (ret < 0) {
185 err("init failed %d", ret);
186 return ret;
189 /* Read the result of the command. Ignore the result, for it
190 * varies with the device. */
191 ret = usb_bulk_msg(gspca_dev->dev,
192 gspca_dev->urb[0]->pipe,
193 gspca_dev->urb[0]->transfer_buffer,
194 FPIX_MAX_TRANSFER, &len,
195 FPIX_TIMEOUT);
196 if (ret < 0) {
197 err("usb_bulk_msg failed %d", ret);
198 return ret;
201 /* Request a frame, but don't read it */
202 ret = command(gspca_dev, 1);
203 if (ret < 0) {
204 err("frame request failed %d", ret);
205 return ret;
208 /* Again, reset bulk in endpoint */
209 usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
211 /* Start the workqueue function to do the streaming */
212 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
213 queue_work(dev->work_thread, &dev->work_struct);
215 return 0;
218 /* called on streamoff with alt==0 and on disconnect */
219 /* the usb_lock is held at entry - restore on exit */
220 static void sd_stop0(struct gspca_dev *gspca_dev)
222 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
224 /* wait for the work queue to terminate */
225 mutex_unlock(&gspca_dev->usb_lock);
226 destroy_workqueue(dev->work_thread);
227 mutex_lock(&gspca_dev->usb_lock);
228 dev->work_thread = NULL;
231 /* Table of supported USB devices */
232 static const struct usb_device_id device_table[] = {
233 {USB_DEVICE(0x04cb, 0x0104)},
234 {USB_DEVICE(0x04cb, 0x0109)},
235 {USB_DEVICE(0x04cb, 0x010b)},
236 {USB_DEVICE(0x04cb, 0x010f)},
237 {USB_DEVICE(0x04cb, 0x0111)},
238 {USB_DEVICE(0x04cb, 0x0113)},
239 {USB_DEVICE(0x04cb, 0x0115)},
240 {USB_DEVICE(0x04cb, 0x0117)},
241 {USB_DEVICE(0x04cb, 0x0119)},
242 {USB_DEVICE(0x04cb, 0x011b)},
243 {USB_DEVICE(0x04cb, 0x011d)},
244 {USB_DEVICE(0x04cb, 0x0121)},
245 {USB_DEVICE(0x04cb, 0x0123)},
246 {USB_DEVICE(0x04cb, 0x0125)},
247 {USB_DEVICE(0x04cb, 0x0127)},
248 {USB_DEVICE(0x04cb, 0x0129)},
249 {USB_DEVICE(0x04cb, 0x012b)},
250 {USB_DEVICE(0x04cb, 0x012d)},
251 {USB_DEVICE(0x04cb, 0x012f)},
252 {USB_DEVICE(0x04cb, 0x0131)},
253 {USB_DEVICE(0x04cb, 0x013b)},
254 {USB_DEVICE(0x04cb, 0x013d)},
255 {USB_DEVICE(0x04cb, 0x013f)},
259 MODULE_DEVICE_TABLE(usb, device_table);
261 /* sub-driver description */
262 static const struct sd_desc sd_desc = {
263 .name = MODULE_NAME,
264 .config = sd_config,
265 .init = sd_init,
266 .start = sd_start,
267 .stop0 = sd_stop0,
270 /* -- device connect -- */
271 static int sd_probe(struct usb_interface *intf,
272 const struct usb_device_id *id)
274 return gspca_dev_probe(intf, id,
275 &sd_desc,
276 sizeof(struct usb_fpix),
277 THIS_MODULE);
280 static struct usb_driver sd_driver = {
281 .name = MODULE_NAME,
282 .id_table = device_table,
283 .probe = sd_probe,
284 .disconnect = gspca_disconnect,
285 #ifdef CONFIG_PM
286 .suspend = gspca_suspend,
287 .resume = gspca_resume,
288 #endif
291 /* -- module insert / remove -- */
292 static int __init sd_mod_init(void)
294 return usb_register(&sd_driver);
297 static void __exit sd_mod_exit(void)
299 usb_deregister(&sd_driver);
302 module_init(sd_mod_init);
303 module_exit(sd_mod_exit);