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