1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#define MODULE_NAME "sq905"
37
38#include <linux/workqueue.h>
39#include <linux/slab.h>
40#include "gspca.h"
41
42MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
43 "Theodore Kilgore <kilgota@auburn.edu>");
44MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
45MODULE_LICENSE("GPL");
46
47
48#define SQ905_CMD_TIMEOUT 500
49#define SQ905_DATA_TIMEOUT 1000
50
51
52#define SQ905_MAX_TRANSFER 0x8000
53#define FRAME_HEADER_LEN 64
54
55
56
57
58
59#define SQ905_BULK_READ 0x03
60#define SQ905_COMMAND 0x06
61#define SQ905_PING 0x07
62#define SQ905_READ_DONE 0xc0
63
64
65
66
67
68#define SQ905_HIRES_MASK 0x00000300
69#define SQ905_ORIENTATION_MASK 0x00000100
70
71
72
73#define SQ905_ID 0xf0
74#define SQ905_CONFIG 0x20
75#define SQ905_DATA 0x30
76#define SQ905_CLEAR 0xa0
77#define SQ905_CAPTURE_LOW 0x60
78#define SQ905_CAPTURE_MED 0x61
79#define SQ905_CAPTURE_HIGH 0x62
80
81
82
83struct sd {
84 struct gspca_dev gspca_dev;
85
86
87
88
89 struct work_struct work_struct;
90 struct workqueue_struct *work_thread;
91};
92
93static struct v4l2_pix_format sq905_mode[] = {
94 { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
95 .bytesperline = 160,
96 .sizeimage = 160 * 120,
97 .colorspace = V4L2_COLORSPACE_SRGB,
98 .priv = 0},
99 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
100 .bytesperline = 320,
101 .sizeimage = 320 * 240,
102 .colorspace = V4L2_COLORSPACE_SRGB,
103 .priv = 0},
104 { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
105 .bytesperline = 640,
106 .sizeimage = 640 * 480,
107 .colorspace = V4L2_COLORSPACE_SRGB,
108 .priv = 0}
109};
110
111
112
113
114static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
115{
116 int ret;
117
118 gspca_dev->usb_buf[0] = '\0';
119 ret = usb_control_msg(gspca_dev->dev,
120 usb_sndctrlpipe(gspca_dev->dev, 0),
121 USB_REQ_SYNCH_FRAME,
122 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
123 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
124 SQ905_CMD_TIMEOUT);
125 if (ret < 0) {
126 err("%s: usb_control_msg failed (%d)",
127 __func__, ret);
128 return ret;
129 }
130
131 ret = usb_control_msg(gspca_dev->dev,
132 usb_sndctrlpipe(gspca_dev->dev, 0),
133 USB_REQ_SYNCH_FRAME,
134 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
135 SQ905_PING, 0, gspca_dev->usb_buf, 1,
136 SQ905_CMD_TIMEOUT);
137 if (ret < 0) {
138 err("%s: usb_control_msg failed 2 (%d)",
139 __func__, ret);
140 return ret;
141 }
142
143 return 0;
144}
145
146
147
148
149static int sq905_ack_frame(struct gspca_dev *gspca_dev)
150{
151 int ret;
152
153 gspca_dev->usb_buf[0] = '\0';
154 ret = usb_control_msg(gspca_dev->dev,
155 usb_sndctrlpipe(gspca_dev->dev, 0),
156 USB_REQ_SYNCH_FRAME,
157 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
158 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
159 SQ905_CMD_TIMEOUT);
160 if (ret < 0) {
161 err("%s: usb_control_msg failed (%d)", __func__, ret);
162 return ret;
163 }
164
165 return 0;
166}
167
168
169
170
171static int
172sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
173{
174 int ret;
175 int act_len;
176
177 gspca_dev->usb_buf[0] = '\0';
178 if (need_lock)
179 mutex_lock(&gspca_dev->usb_lock);
180 ret = usb_control_msg(gspca_dev->dev,
181 usb_sndctrlpipe(gspca_dev->dev, 0),
182 USB_REQ_SYNCH_FRAME,
183 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184 SQ905_BULK_READ, size, gspca_dev->usb_buf,
185 1, SQ905_CMD_TIMEOUT);
186 if (need_lock)
187 mutex_unlock(&gspca_dev->usb_lock);
188 if (ret < 0) {
189 err("%s: usb_control_msg failed (%d)", __func__, ret);
190 return ret;
191 }
192 ret = usb_bulk_msg(gspca_dev->dev,
193 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
194 data, size, &act_len, SQ905_DATA_TIMEOUT);
195
196
197 if (ret < 0 || act_len != size) {
198 err("bulk read fail (%d) len %d/%d",
199 ret, act_len, size);
200 return -EIO;
201 }
202 return 0;
203}
204
205
206
207
208
209
210
211
212
213
214static void sq905_dostream(struct work_struct *work)
215{
216 struct sd *dev = container_of(work, struct sd, work_struct);
217 struct gspca_dev *gspca_dev = &dev->gspca_dev;
218 int bytes_left;
219 int data_len;
220 int header_read;
221 int packet_type;
222 int frame_sz;
223 int ret;
224 u8 *data;
225 u8 *buffer;
226
227 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
228 if (!buffer) {
229 err("Couldn't allocate USB buffer");
230 goto quit_stream;
231 }
232
233 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
234 + FRAME_HEADER_LEN;
235
236 while (gspca_dev->present && gspca_dev->streaming) {
237
238
239 bytes_left = frame_sz;
240 header_read = 0;
241
242
243
244
245
246 while (bytes_left > 0 && gspca_dev->present) {
247 data_len = bytes_left > SQ905_MAX_TRANSFER ?
248 SQ905_MAX_TRANSFER : bytes_left;
249 ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
250 if (ret < 0)
251 goto quit_stream;
252 PDEBUG(D_PACK,
253 "Got %d bytes out of %d for frame",
254 data_len, bytes_left);
255 bytes_left -= data_len;
256 data = buffer;
257 if (!header_read) {
258 packet_type = FIRST_PACKET;
259
260
261 data += FRAME_HEADER_LEN;
262 data_len -= FRAME_HEADER_LEN;
263 header_read = 1;
264 } else if (bytes_left == 0) {
265 packet_type = LAST_PACKET;
266 } else {
267 packet_type = INTER_PACKET;
268 }
269 gspca_frame_add(gspca_dev, packet_type,
270 data, data_len);
271
272
273 if (packet_type == FIRST_PACKET &&
274 bytes_left == 0)
275 gspca_frame_add(gspca_dev, LAST_PACKET,
276 NULL, 0);
277 }
278 if (gspca_dev->present) {
279
280 mutex_lock(&gspca_dev->usb_lock);
281 ret = sq905_ack_frame(gspca_dev);
282 mutex_unlock(&gspca_dev->usb_lock);
283 if (ret < 0)
284 goto quit_stream;
285 }
286 }
287quit_stream:
288 if (gspca_dev->present) {
289 mutex_lock(&gspca_dev->usb_lock);
290 sq905_command(gspca_dev, SQ905_CLEAR);
291 mutex_unlock(&gspca_dev->usb_lock);
292 }
293 kfree(buffer);
294}
295
296
297static int sd_config(struct gspca_dev *gspca_dev,
298 const struct usb_device_id *id)
299{
300 struct cam *cam = &gspca_dev->cam;
301 struct sd *dev = (struct sd *) gspca_dev;
302
303
304 cam->bulk = 1;
305 cam->bulk_size = 64;
306
307 INIT_WORK(&dev->work_struct, sq905_dostream);
308
309 return 0;
310}
311
312
313
314static void sd_stop0(struct gspca_dev *gspca_dev)
315{
316 struct sd *dev = (struct sd *) gspca_dev;
317
318
319 mutex_unlock(&gspca_dev->usb_lock);
320
321 destroy_workqueue(dev->work_thread);
322 dev->work_thread = NULL;
323 mutex_lock(&gspca_dev->usb_lock);
324}
325
326
327static int sd_init(struct gspca_dev *gspca_dev)
328{
329 u32 ident;
330 int ret;
331
332
333
334
335 ret = sq905_command(gspca_dev, SQ905_CLEAR);
336 if (ret < 0)
337 return ret;
338 ret = sq905_command(gspca_dev, SQ905_ID);
339 if (ret < 0)
340 return ret;
341 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
342 if (ret < 0)
343 return ret;
344
345
346
347 ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
348 ret = sq905_command(gspca_dev, SQ905_CLEAR);
349 if (ret < 0)
350 return ret;
351 PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
352 gspca_dev->cam.cam_mode = sq905_mode;
353 gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
354 if (!(ident & SQ905_HIRES_MASK))
355 gspca_dev->cam.nmodes--;
356
357 if (ident & SQ905_ORIENTATION_MASK)
358 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
359 else
360 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
361 V4L2_IN_ST_HFLIP;
362 return 0;
363}
364
365
366static int sd_start(struct gspca_dev *gspca_dev)
367{
368 struct sd *dev = (struct sd *) gspca_dev;
369 int ret;
370
371
372 switch (gspca_dev->curr_mode) {
373 default:
374
375 PDEBUG(D_STREAM, "Start streaming at high resolution");
376 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
377 break;
378 case 1:
379 PDEBUG(D_STREAM, "Start streaming at medium resolution");
380 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
381 break;
382 case 0:
383 PDEBUG(D_STREAM, "Start streaming at low resolution");
384 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
385 }
386
387 if (ret < 0) {
388 PDEBUG(D_ERR, "Start streaming command failed");
389 return ret;
390 }
391
392 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
393 queue_work(dev->work_thread, &dev->work_struct);
394
395 return 0;
396}
397
398
399static const struct usb_device_id device_table[] = {
400 {USB_DEVICE(0x2770, 0x9120)},
401 {}
402};
403
404MODULE_DEVICE_TABLE(usb, device_table);
405
406
407static const struct sd_desc sd_desc = {
408 .name = MODULE_NAME,
409 .config = sd_config,
410 .init = sd_init,
411 .start = sd_start,
412 .stop0 = sd_stop0,
413};
414
415
416static int sd_probe(struct usb_interface *intf,
417 const struct usb_device_id *id)
418{
419 return gspca_dev_probe(intf, id,
420 &sd_desc,
421 sizeof(struct sd),
422 THIS_MODULE);
423}
424
425static struct usb_driver sd_driver = {
426 .name = MODULE_NAME,
427 .id_table = device_table,
428 .probe = sd_probe,
429 .disconnect = gspca_disconnect,
430#ifdef CONFIG_PM
431 .suspend = gspca_suspend,
432 .resume = gspca_resume,
433#endif
434};
435
436
437static int __init sd_mod_init(void)
438{
439 return usb_register(&sd_driver);
440}
441
442static void __exit sd_mod_exit(void)
443{
444 usb_deregister(&sd_driver);
445}
446
447module_init(sd_mod_init);
448module_exit(sd_mod_exit);
449