]> Pileus Git - ~andy/linux/blob - drivers/media/usb/gspca/finepix.c
[media] gspca_finepix: Remove unnecessary lock/unlock call
[~andy/linux] / drivers / media / usb / gspca / finepix.c
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
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
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 */
32 #define FPIX_TIMEOUT 250
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
43         struct work_struct work_struct;
44         struct workqueue_struct *work_thread;
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
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
52
53 /* These cameras only support 320x200. */
54 static const struct v4l2_pix_format fpix_mode[1] = {
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
62 /* send a command to the webcam */
63 static int command(struct gspca_dev *gspca_dev,
64                 int order)      /* 0: reset, 1: frame request */
65 {
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         };
70
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);
78 }
79
80 /* workqueue */
81 static void dostream(struct work_struct *work)
82 {
83         struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
84         struct gspca_dev *gspca_dev = &dev->gspca_dev;
85         struct urb *urb = gspca_dev->urb[0];
86         u8 *data = urb->transfer_buffer;
87         int ret = 0;
88         int len;
89
90         PDEBUG(D_STREAM, "dostream started");
91
92         /* loop reading a frame */
93 again:
94         while (gspca_dev->present && gspca_dev->streaming) {
95 #ifdef CONFIG_PM
96                 if (gspca_dev->frozen)
97                         break;
98 #endif
99
100                 /* request a frame */
101                 mutex_lock(&gspca_dev->usb_lock);
102                 ret = command(gspca_dev, 1);
103                 mutex_unlock(&gspca_dev->usb_lock);
104                 if (ret < 0)
105                         break;
106 #ifdef CONFIG_PM
107                 if (gspca_dev->frozen)
108                         break;
109 #endif
110                 if (!gspca_dev->present || !gspca_dev->streaming)
111                         break;
112
113                 /* the frame comes in parts */
114                 for (;;) {
115                         ret = usb_bulk_msg(gspca_dev->dev,
116                                         urb->pipe,
117                                         data,
118                                         FPIX_MAX_TRANSFER,
119                                         &len, FPIX_TIMEOUT);
120                         if (ret < 0) {
121                                 /* Most of the time we get a timeout
122                                  * error. Just restart. */
123                                 goto again;
124                         }
125 #ifdef CONFIG_PM
126                         if (gspca_dev->frozen)
127                                 goto out;
128 #endif
129                         if (!gspca_dev->present || !gspca_dev->streaming)
130                                 goto out;
131                         if (len < FPIX_MAX_TRANSFER ||
132                                 (data[len - 2] == 0xff &&
133                                         data[len - 1] == 0xd9)) {
134
135                                 /* If the result is less than what was asked
136                                  * for, then it's the end of the
137                                  * frame. Sometimes the jpeg is not complete,
138                                  * but there's nothing we can do. We also end
139                                  * here if the the jpeg ends right at the end
140                                  * of the frame. */
141                                 gspca_frame_add(gspca_dev, LAST_PACKET,
142                                                 data, len);
143                                 break;
144                         }
145
146                         /* got a partial image */
147                         gspca_frame_add(gspca_dev,
148                                         gspca_dev->last_packet_type
149                                                 == LAST_PACKET
150                                         ? FIRST_PACKET : INTER_PACKET,
151                                         data, len);
152                 }
153
154                 /* We must wait before trying reading the next
155                  * frame. If we don't, or if the delay is too short,
156                  * the camera will disconnect. */
157                 msleep(NEXT_FRAME_DELAY);
158         }
159
160 out:
161         PDEBUG(D_STREAM, "dostream stopped");
162 }
163
164 /* this function is called at probe time */
165 static int sd_config(struct gspca_dev *gspca_dev,
166                 const struct usb_device_id *id)
167 {
168         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
169         struct cam *cam = &gspca_dev->cam;
170
171         cam->cam_mode = fpix_mode;
172         cam->nmodes = 1;
173         cam->bulk = 1;
174         cam->bulk_size = FPIX_MAX_TRANSFER;
175
176         INIT_WORK(&dev->work_struct, dostream);
177
178         return 0;
179 }
180
181 /* this function is called at probe and resume time */
182 static int sd_init(struct gspca_dev *gspca_dev)
183 {
184         return 0;
185 }
186
187 /* start the camera */
188 static int sd_start(struct gspca_dev *gspca_dev)
189 {
190         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
191         int ret, len;
192
193         /* Init the device */
194         ret = command(gspca_dev, 0);
195         if (ret < 0) {
196                 pr_err("init failed %d\n", ret);
197                 return ret;
198         }
199
200         /* Read the result of the command. Ignore the result, for it
201          * varies with the device. */
202         ret = usb_bulk_msg(gspca_dev->dev,
203                         gspca_dev->urb[0]->pipe,
204                         gspca_dev->urb[0]->transfer_buffer,
205                         FPIX_MAX_TRANSFER, &len,
206                         FPIX_TIMEOUT);
207         if (ret < 0) {
208                 pr_err("usb_bulk_msg failed %d\n", ret);
209                 return ret;
210         }
211
212         /* Request a frame, but don't read it */
213         ret = command(gspca_dev, 1);
214         if (ret < 0) {
215                 pr_err("frame request failed %d\n", ret);
216                 return ret;
217         }
218
219         /* Again, reset bulk in endpoint */
220         usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
221
222         /* Start the workqueue function to do the streaming */
223         dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
224         queue_work(dev->work_thread, &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 */
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         destroy_workqueue(dev->work_thread);
238         mutex_lock(&gspca_dev->usb_lock);
239         dev->work_thread = NULL;
240 }
241
242 /* Table of supported USB devices */
243 static const struct usb_device_id device_table[] = {
244         {USB_DEVICE(0x04cb, 0x0104)},
245         {USB_DEVICE(0x04cb, 0x0109)},
246         {USB_DEVICE(0x04cb, 0x010b)},
247         {USB_DEVICE(0x04cb, 0x010f)},
248         {USB_DEVICE(0x04cb, 0x0111)},
249         {USB_DEVICE(0x04cb, 0x0113)},
250         {USB_DEVICE(0x04cb, 0x0115)},
251         {USB_DEVICE(0x04cb, 0x0117)},
252         {USB_DEVICE(0x04cb, 0x0119)},
253         {USB_DEVICE(0x04cb, 0x011b)},
254         {USB_DEVICE(0x04cb, 0x011d)},
255         {USB_DEVICE(0x04cb, 0x0121)},
256         {USB_DEVICE(0x04cb, 0x0123)},
257         {USB_DEVICE(0x04cb, 0x0125)},
258         {USB_DEVICE(0x04cb, 0x0127)},
259         {USB_DEVICE(0x04cb, 0x0129)},
260         {USB_DEVICE(0x04cb, 0x012b)},
261         {USB_DEVICE(0x04cb, 0x012d)},
262         {USB_DEVICE(0x04cb, 0x012f)},
263         {USB_DEVICE(0x04cb, 0x0131)},
264         {USB_DEVICE(0x04cb, 0x013b)},
265         {USB_DEVICE(0x04cb, 0x013d)},
266         {USB_DEVICE(0x04cb, 0x013f)},
267         {}
268 };
269
270 MODULE_DEVICE_TABLE(usb, device_table);
271
272 /* sub-driver description */
273 static const struct sd_desc sd_desc = {
274         .name   = MODULE_NAME,
275         .config = sd_config,
276         .init   = sd_init,
277         .start  = sd_start,
278         .stop0  = sd_stop0,
279 };
280
281 /* -- device connect -- */
282 static int sd_probe(struct usb_interface *intf,
283                 const struct usb_device_id *id)
284 {
285         return gspca_dev_probe(intf, id,
286                         &sd_desc,
287                         sizeof(struct usb_fpix),
288                         THIS_MODULE);
289 }
290
291 static struct usb_driver sd_driver = {
292         .name       = MODULE_NAME,
293         .id_table   = device_table,
294         .probe      = sd_probe,
295         .disconnect = gspca_disconnect,
296 #ifdef CONFIG_PM
297         .suspend = gspca_suspend,
298         .resume  = gspca_resume,
299         .reset_resume = gspca_resume,
300 #endif
301 };
302
303 module_usb_driver(sd_driver);