]>
Commit | Line | Data |
---|---|---|
27d35fc3 AB |
1 | /* |
2 | * SQ905 subdriver | |
3 | * | |
4 | * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore | |
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. | |
27d35fc3 AB |
15 | */ |
16 | ||
17 | /* | |
18 | * History and Acknowledgments | |
19 | * | |
20 | * The original Linux driver for SQ905 based cameras was written by | |
25985edc | 21 | * Marcell Lengyel and furter developed by many other contributors |
27d35fc3 AB |
22 | * and is available from http://sourceforge.net/projects/sqcam/ |
23 | * | |
24 | * This driver takes advantage of the reverse engineering work done for | |
25 | * that driver and for libgphoto2 but shares no code with them. | |
26 | * | |
27 | * This driver has used as a base the finepix driver and other gspca | |
28 | * based drivers and may still contain code fragments taken from those | |
29 | * drivers. | |
30 | */ | |
31 | ||
133a9fe9 JP |
32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
33 | ||
27d35fc3 AB |
34 | #define MODULE_NAME "sq905" |
35 | ||
36 | #include <linux/workqueue.h> | |
5a0e3ad6 | 37 | #include <linux/slab.h> |
27d35fc3 AB |
38 | #include "gspca.h" |
39 | ||
1ddc9f75 | 40 | MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>"); |
27d35fc3 AB |
41 | MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver"); |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
44 | /* Default timeouts, in ms */ | |
45 | #define SQ905_CMD_TIMEOUT 500 | |
46 | #define SQ905_DATA_TIMEOUT 1000 | |
47 | ||
48 | /* Maximum transfer size to use. */ | |
49 | #define SQ905_MAX_TRANSFER 0x8000 | |
50 | #define FRAME_HEADER_LEN 64 | |
51 | ||
52 | /* The known modes, or registers. These go in the "value" slot. */ | |
53 | ||
54 | /* 00 is "none" obviously */ | |
55 | ||
56 | #define SQ905_BULK_READ 0x03 /* precedes any bulk read */ | |
57 | #define SQ905_COMMAND 0x06 /* precedes the command codes below */ | |
58 | #define SQ905_PING 0x07 /* when reading an "idling" command */ | |
59 | #define SQ905_READ_DONE 0xc0 /* ack bulk read completed */ | |
60 | ||
3b27591d AB |
61 | /* Any non-zero value in the bottom 2 bits of the 2nd byte of |
62 | * the ID appears to indicate the camera can do 640*480. If the | |
63 | * LSB of that byte is set the image is just upside down, otherwise | |
64 | * it is rotated 180 degrees. */ | |
65 | #define SQ905_HIRES_MASK 0x00000300 | |
66 | #define SQ905_ORIENTATION_MASK 0x00000100 | |
67 | ||
27d35fc3 AB |
68 | /* Some command codes. These go in the "index" slot. */ |
69 | ||
70 | #define SQ905_ID 0xf0 /* asks for model string */ | |
71 | #define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */ | |
72 | #define SQ905_DATA 0x30 /* accesses photo data, not used here */ | |
73 | #define SQ905_CLEAR 0xa0 /* clear everything */ | |
3b27591d AB |
74 | #define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */ |
75 | #define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */ | |
76 | #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */ | |
27d35fc3 | 77 | /* note that the capture command also controls the output dimensions */ |
27d35fc3 AB |
78 | |
79 | /* Structure to hold all of our device specific stuff */ | |
80 | struct sd { | |
81 | struct gspca_dev gspca_dev; /* !! must be the first item */ | |
82 | ||
27d35fc3 AB |
83 | /* |
84 | * Driver stuff | |
85 | */ | |
86 | struct work_struct work_struct; | |
87 | struct workqueue_struct *work_thread; | |
88 | }; | |
89 | ||
3b27591d AB |
90 | static struct v4l2_pix_format sq905_mode[] = { |
91 | { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, | |
92 | .bytesperline = 160, | |
93 | .sizeimage = 160 * 120, | |
94 | .colorspace = V4L2_COLORSPACE_SRGB, | |
95 | .priv = 0}, | |
27d35fc3 AB |
96 | { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, |
97 | .bytesperline = 320, | |
98 | .sizeimage = 320 * 240, | |
99 | .colorspace = V4L2_COLORSPACE_SRGB, | |
3b27591d AB |
100 | .priv = 0}, |
101 | { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, | |
102 | .bytesperline = 640, | |
103 | .sizeimage = 640 * 480, | |
104 | .colorspace = V4L2_COLORSPACE_SRGB, | |
27d35fc3 AB |
105 | .priv = 0} |
106 | }; | |
107 | ||
27d35fc3 AB |
108 | /* |
109 | * Send a command to the camera. | |
110 | */ | |
111 | static int sq905_command(struct gspca_dev *gspca_dev, u16 index) | |
112 | { | |
113 | int ret; | |
114 | ||
115 | gspca_dev->usb_buf[0] = '\0'; | |
116 | ret = usb_control_msg(gspca_dev->dev, | |
117 | usb_sndctrlpipe(gspca_dev->dev, 0), | |
118 | USB_REQ_SYNCH_FRAME, /* request */ | |
119 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
120 | SQ905_COMMAND, index, gspca_dev->usb_buf, 1, | |
121 | SQ905_CMD_TIMEOUT); | |
122 | if (ret < 0) { | |
133a9fe9 | 123 | pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); |
27d35fc3 AB |
124 | return ret; |
125 | } | |
126 | ||
127 | ret = usb_control_msg(gspca_dev->dev, | |
128 | usb_sndctrlpipe(gspca_dev->dev, 0), | |
129 | USB_REQ_SYNCH_FRAME, /* request */ | |
130 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
131 | SQ905_PING, 0, gspca_dev->usb_buf, 1, | |
132 | SQ905_CMD_TIMEOUT); | |
133 | if (ret < 0) { | |
133a9fe9 | 134 | pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret); |
27d35fc3 AB |
135 | return ret; |
136 | } | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | /* | |
142 | * Acknowledge the end of a frame - see warning on sq905_command. | |
143 | */ | |
144 | static int sq905_ack_frame(struct gspca_dev *gspca_dev) | |
145 | { | |
146 | int ret; | |
147 | ||
148 | gspca_dev->usb_buf[0] = '\0'; | |
149 | ret = usb_control_msg(gspca_dev->dev, | |
150 | usb_sndctrlpipe(gspca_dev->dev, 0), | |
151 | USB_REQ_SYNCH_FRAME, /* request */ | |
152 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
153 | SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1, | |
154 | SQ905_CMD_TIMEOUT); | |
155 | if (ret < 0) { | |
133a9fe9 | 156 | pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); |
27d35fc3 AB |
157 | return ret; |
158 | } | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | /* | |
164 | * request and read a block of data - see warning on sq905_command. | |
165 | */ | |
166 | static int | |
85191100 | 167 | sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock) |
27d35fc3 AB |
168 | { |
169 | int ret; | |
170 | int act_len; | |
171 | ||
172 | gspca_dev->usb_buf[0] = '\0'; | |
85191100 HG |
173 | if (need_lock) |
174 | mutex_lock(&gspca_dev->usb_lock); | |
27d35fc3 AB |
175 | ret = usb_control_msg(gspca_dev->dev, |
176 | usb_sndctrlpipe(gspca_dev->dev, 0), | |
177 | USB_REQ_SYNCH_FRAME, /* request */ | |
178 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
179 | SQ905_BULK_READ, size, gspca_dev->usb_buf, | |
180 | 1, SQ905_CMD_TIMEOUT); | |
85191100 HG |
181 | if (need_lock) |
182 | mutex_unlock(&gspca_dev->usb_lock); | |
27d35fc3 | 183 | if (ret < 0) { |
133a9fe9 | 184 | pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); |
27d35fc3 AB |
185 | return ret; |
186 | } | |
187 | ret = usb_bulk_msg(gspca_dev->dev, | |
188 | usb_rcvbulkpipe(gspca_dev->dev, 0x81), | |
189 | data, size, &act_len, SQ905_DATA_TIMEOUT); | |
190 | ||
191 | /* successful, it returns 0, otherwise negative */ | |
192 | if (ret < 0 || act_len != size) { | |
133a9fe9 | 193 | pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size); |
27d35fc3 AB |
194 | return -EIO; |
195 | } | |
196 | return 0; | |
197 | } | |
198 | ||
844db450 HG |
199 | /* |
200 | * This function is called as a workqueue function and runs whenever the camera | |
27d35fc3 AB |
201 | * is streaming data. Because it is a workqueue function it is allowed to sleep |
202 | * so we can use synchronous USB calls. To avoid possible collisions with other | |
844db450 HG |
203 | * threads attempting to use gspca_dev->usb_buf we take the usb_lock when |
204 | * performing USB operations using it. In practice we don't really need this | |
205 | * as the camera doesn't provide any controls. | |
27d35fc3 AB |
206 | */ |
207 | static void sq905_dostream(struct work_struct *work) | |
208 | { | |
209 | struct sd *dev = container_of(work, struct sd, work_struct); | |
210 | struct gspca_dev *gspca_dev = &dev->gspca_dev; | |
27d35fc3 AB |
211 | int bytes_left; /* bytes remaining in current frame. */ |
212 | int data_len; /* size to use for the next read. */ | |
213 | int header_read; /* true if we have already read the frame header. */ | |
27d35fc3 | 214 | int packet_type; |
67e45425 | 215 | int frame_sz; |
27d35fc3 AB |
216 | int ret; |
217 | u8 *data; | |
218 | u8 *buffer; | |
219 | ||
220 | buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA); | |
27d35fc3 | 221 | if (!buffer) { |
133a9fe9 | 222 | pr_err("Couldn't allocate USB buffer\n"); |
27d35fc3 AB |
223 | goto quit_stream; |
224 | } | |
225 | ||
67e45425 JFM |
226 | frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage |
227 | + FRAME_HEADER_LEN; | |
228 | ||
345321dc | 229 | while (gspca_dev->present && gspca_dev->streaming) { |
4ad34da0 HV |
230 | #ifdef CONFIG_PM |
231 | if (gspca_dev->frozen) | |
232 | break; | |
233 | #endif | |
27d35fc3 AB |
234 | /* request some data and then read it until we have |
235 | * a complete frame. */ | |
67e45425 | 236 | bytes_left = frame_sz; |
27d35fc3 | 237 | header_read = 0; |
27d35fc3 | 238 | |
85191100 HG |
239 | /* Note we do not check for gspca_dev->streaming here, as |
240 | we must finish reading an entire frame, otherwise the | |
241 | next time we stream we start reading in the middle of a | |
242 | frame. */ | |
345321dc | 243 | while (bytes_left > 0 && gspca_dev->present) { |
27d35fc3 AB |
244 | data_len = bytes_left > SQ905_MAX_TRANSFER ? |
245 | SQ905_MAX_TRANSFER : bytes_left; | |
85191100 | 246 | ret = sq905_read_data(gspca_dev, buffer, data_len, 1); |
27d35fc3 AB |
247 | if (ret < 0) |
248 | goto quit_stream; | |
85191100 | 249 | PDEBUG(D_PACK, |
27d35fc3 AB |
250 | "Got %d bytes out of %d for frame", |
251 | data_len, bytes_left); | |
252 | bytes_left -= data_len; | |
253 | data = buffer; | |
254 | if (!header_read) { | |
255 | packet_type = FIRST_PACKET; | |
256 | /* The first 64 bytes of each frame are | |
257 | * a header full of FF 00 bytes */ | |
258 | data += FRAME_HEADER_LEN; | |
259 | data_len -= FRAME_HEADER_LEN; | |
260 | header_read = 1; | |
261 | } else if (bytes_left == 0) { | |
262 | packet_type = LAST_PACKET; | |
263 | } else { | |
264 | packet_type = INTER_PACKET; | |
265 | } | |
76dd272b JFM |
266 | gspca_frame_add(gspca_dev, packet_type, |
267 | data, data_len); | |
268 | /* If entire frame fits in one packet we still | |
269 | need to add a LAST_PACKET */ | |
270 | if (packet_type == FIRST_PACKET && | |
271 | bytes_left == 0) | |
272 | gspca_frame_add(gspca_dev, LAST_PACKET, | |
273 | NULL, 0); | |
27d35fc3 | 274 | } |
345321dc | 275 | if (gspca_dev->present) { |
85191100 HG |
276 | /* acknowledge the frame */ |
277 | mutex_lock(&gspca_dev->usb_lock); | |
278 | ret = sq905_ack_frame(gspca_dev); | |
279 | mutex_unlock(&gspca_dev->usb_lock); | |
280 | if (ret < 0) | |
281 | goto quit_stream; | |
282 | } | |
27d35fc3 AB |
283 | } |
284 | quit_stream: | |
345321dc | 285 | if (gspca_dev->present) { |
85191100 | 286 | mutex_lock(&gspca_dev->usb_lock); |
27d35fc3 | 287 | sq905_command(gspca_dev, SQ905_CLEAR); |
85191100 HG |
288 | mutex_unlock(&gspca_dev->usb_lock); |
289 | } | |
27d35fc3 AB |
290 | kfree(buffer); |
291 | } | |
292 | ||
293 | /* This function is called at probe time just before sd_init */ | |
294 | static int sd_config(struct gspca_dev *gspca_dev, | |
295 | const struct usb_device_id *id) | |
296 | { | |
297 | struct cam *cam = &gspca_dev->cam; | |
298 | struct sd *dev = (struct sd *) gspca_dev; | |
299 | ||
27d35fc3 | 300 | /* We don't use the buffer gspca allocates so make it small. */ |
6929dc6b | 301 | cam->bulk = 1; |
27d35fc3 AB |
302 | cam->bulk_size = 64; |
303 | ||
304 | INIT_WORK(&dev->work_struct, sq905_dostream); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | /* called on streamoff with alt==0 and on disconnect */ | |
310 | /* the usb_lock is held at entry - restore on exit */ | |
311 | static void sd_stop0(struct gspca_dev *gspca_dev) | |
312 | { | |
313 | struct sd *dev = (struct sd *) gspca_dev; | |
314 | ||
315 | /* wait for the work queue to terminate */ | |
316 | mutex_unlock(&gspca_dev->usb_lock); | |
317 | /* This waits for sq905_dostream to finish */ | |
318 | destroy_workqueue(dev->work_thread); | |
319 | dev->work_thread = NULL; | |
320 | mutex_lock(&gspca_dev->usb_lock); | |
321 | } | |
322 | ||
323 | /* this function is called at probe and resume time */ | |
324 | static int sd_init(struct gspca_dev *gspca_dev) | |
325 | { | |
27d35fc3 AB |
326 | u32 ident; |
327 | int ret; | |
328 | ||
329 | /* connect to the camera and read | |
330 | * the model ID and process that and put it away. | |
331 | */ | |
332 | ret = sq905_command(gspca_dev, SQ905_CLEAR); | |
333 | if (ret < 0) | |
334 | return ret; | |
335 | ret = sq905_command(gspca_dev, SQ905_ID); | |
336 | if (ret < 0) | |
337 | return ret; | |
85191100 | 338 | ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0); |
27d35fc3 AB |
339 | if (ret < 0) |
340 | return ret; | |
3b27591d AB |
341 | /* usb_buf is allocated with kmalloc so is aligned. |
342 | * Camera model number is the right way round if we assume this | |
343 | * reverse engineered ID is supposed to be big endian. */ | |
344 | ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf); | |
27d35fc3 AB |
345 | ret = sq905_command(gspca_dev, SQ905_CLEAR); |
346 | if (ret < 0) | |
347 | return ret; | |
3b27591d AB |
348 | PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident); |
349 | gspca_dev->cam.cam_mode = sq905_mode; | |
350 | gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode); | |
351 | if (!(ident & SQ905_HIRES_MASK)) | |
352 | gspca_dev->cam.nmodes--; | |
dfa76fa2 AB |
353 | |
354 | if (ident & SQ905_ORIENTATION_MASK) | |
355 | gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP; | |
356 | else | |
357 | gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP | | |
358 | V4L2_IN_ST_HFLIP; | |
27d35fc3 AB |
359 | return 0; |
360 | } | |
361 | ||
362 | /* Set up for getting frames. */ | |
363 | static int sd_start(struct gspca_dev *gspca_dev) | |
364 | { | |
365 | struct sd *dev = (struct sd *) gspca_dev; | |
366 | int ret; | |
367 | ||
368 | /* "Open the shutter" and set size, to start capture */ | |
67e45425 JFM |
369 | switch (gspca_dev->curr_mode) { |
370 | default: | |
371 | /* case 2: */ | |
3b27591d | 372 | PDEBUG(D_STREAM, "Start streaming at high resolution"); |
3b27591d AB |
373 | ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH); |
374 | break; | |
67e45425 | 375 | case 1: |
3b27591d | 376 | PDEBUG(D_STREAM, "Start streaming at medium resolution"); |
3b27591d AB |
377 | ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED); |
378 | break; | |
67e45425 | 379 | case 0: |
3b27591d AB |
380 | PDEBUG(D_STREAM, "Start streaming at low resolution"); |
381 | ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW); | |
382 | } | |
383 | ||
27d35fc3 | 384 | if (ret < 0) { |
c93396e1 | 385 | PERR("Start streaming command failed"); |
27d35fc3 AB |
386 | return ret; |
387 | } | |
27d35fc3 AB |
388 | /* Start the workqueue function to do the streaming */ |
389 | dev->work_thread = create_singlethread_workqueue(MODULE_NAME); | |
390 | queue_work(dev->work_thread, &dev->work_struct); | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | /* Table of supported USB devices */ | |
95c967c1 | 396 | static const struct usb_device_id device_table[] = { |
27d35fc3 AB |
397 | {USB_DEVICE(0x2770, 0x9120)}, |
398 | {} | |
399 | }; | |
400 | ||
401 | MODULE_DEVICE_TABLE(usb, device_table); | |
402 | ||
403 | /* sub-driver description */ | |
404 | static const struct sd_desc sd_desc = { | |
405 | .name = MODULE_NAME, | |
406 | .config = sd_config, | |
407 | .init = sd_init, | |
408 | .start = sd_start, | |
409 | .stop0 = sd_stop0, | |
410 | }; | |
411 | ||
412 | /* -- device connect -- */ | |
413 | static int sd_probe(struct usb_interface *intf, | |
414 | const struct usb_device_id *id) | |
415 | { | |
416 | return gspca_dev_probe(intf, id, | |
417 | &sd_desc, | |
418 | sizeof(struct sd), | |
419 | THIS_MODULE); | |
420 | } | |
421 | ||
422 | static struct usb_driver sd_driver = { | |
423 | .name = MODULE_NAME, | |
424 | .id_table = device_table, | |
425 | .probe = sd_probe, | |
426 | .disconnect = gspca_disconnect, | |
427 | #ifdef CONFIG_PM | |
428 | .suspend = gspca_suspend, | |
429 | .resume = gspca_resume, | |
8bb58964 | 430 | .reset_resume = gspca_resume, |
27d35fc3 AB |
431 | #endif |
432 | }; | |
433 | ||
ecb3b2b3 | 434 | module_usb_driver(sd_driver); |