1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#define MODULE_NAME "sq905c"
20
21#include <linux/workqueue.h>
22#include <linux/slab.h>
23#include "gspca.h"
24
25MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
26MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
27MODULE_LICENSE("GPL");
28
29
30#define SQ905C_CMD_TIMEOUT 500
31#define SQ905C_DATA_TIMEOUT 1000
32
33
34#define SQ905C_MAX_TRANSFER 0x8000
35
36#define FRAME_HEADER_LEN 0x50
37
38
39#define SQ905C_CLEAR 0xa0
40#define SQ905C_GET_ID 0x14f4
41#define SQ905C_CAPTURE_LOW 0xa040
42#define SQ905C_CAPTURE_MED 0x1440
43#define SQ905C_CAPTURE_HI 0x2840
44
45
46#define SQ905C_CAPTURE_INDEX 0x110f
47
48
49struct sd {
50 struct gspca_dev gspca_dev;
51 const struct v4l2_pix_format *cap_mode;
52
53 struct work_struct work_struct;
54 struct workqueue_struct *work_thread;
55};
56
57
58
59
60
61
62static struct v4l2_pix_format sq905c_mode[] = {
63 { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
64 .bytesperline = 320,
65 .sizeimage = 320 * 240,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 .priv = 0},
68 { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
69 .bytesperline = 640,
70 .sizeimage = 640 * 480,
71 .colorspace = V4L2_COLORSPACE_SRGB,
72 .priv = 0}
73};
74
75
76static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
77{
78 int ret;
79
80 ret = usb_control_msg(gspca_dev->dev,
81 usb_sndctrlpipe(gspca_dev->dev, 0),
82 USB_REQ_SYNCH_FRAME,
83 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
84 command, index, NULL, 0,
85 SQ905C_CMD_TIMEOUT);
86 if (ret < 0) {
87 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
88 return ret;
89 }
90
91 return 0;
92}
93
94static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
95 int size)
96{
97 int ret;
98
99 ret = usb_control_msg(gspca_dev->dev,
100 usb_rcvctrlpipe(gspca_dev->dev, 0),
101 USB_REQ_SYNCH_FRAME,
102 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
103 command, index, gspca_dev->usb_buf, size,
104 SQ905C_CMD_TIMEOUT);
105 if (ret < 0) {
106 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
107 return ret;
108 }
109
110 return 0;
111}
112
113
114
115
116
117
118
119
120
121static void sq905c_dostream(struct work_struct *work)
122{
123 struct sd *dev = container_of(work, struct sd, work_struct);
124 struct gspca_dev *gspca_dev = &dev->gspca_dev;
125 int bytes_left;
126 int data_len;
127 int act_len;
128 int packet_type;
129 int ret;
130 u8 *buffer;
131
132 buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL);
133 if (!buffer) {
134 pr_err("Couldn't allocate USB buffer\n");
135 goto quit_stream;
136 }
137
138 while (gspca_dev->present && gspca_dev->streaming) {
139#ifdef CONFIG_PM
140 if (gspca_dev->frozen)
141 break;
142#endif
143
144 ret = usb_bulk_msg(gspca_dev->dev,
145 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
146 buffer, FRAME_HEADER_LEN, &act_len,
147 SQ905C_DATA_TIMEOUT);
148 gspca_dbg(gspca_dev, D_STREAM,
149 "Got %d bytes out of %d for header\n",
150 act_len, FRAME_HEADER_LEN);
151 if (ret < 0 || act_len < FRAME_HEADER_LEN)
152 goto quit_stream;
153
154 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
155 |(buffer[0x43]<<24);
156 gspca_dbg(gspca_dev, D_STREAM, "bytes_left = 0x%x\n",
157 bytes_left);
158
159 packet_type = FIRST_PACKET;
160 gspca_frame_add(gspca_dev, packet_type,
161 buffer, FRAME_HEADER_LEN);
162 while (bytes_left > 0 && gspca_dev->present) {
163 data_len = bytes_left > SQ905C_MAX_TRANSFER ?
164 SQ905C_MAX_TRANSFER : bytes_left;
165 ret = usb_bulk_msg(gspca_dev->dev,
166 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
167 buffer, data_len, &act_len,
168 SQ905C_DATA_TIMEOUT);
169 if (ret < 0 || act_len < data_len)
170 goto quit_stream;
171 gspca_dbg(gspca_dev, D_STREAM,
172 "Got %d bytes out of %d for frame\n",
173 data_len, bytes_left);
174 bytes_left -= data_len;
175 if (bytes_left == 0)
176 packet_type = LAST_PACKET;
177 else
178 packet_type = INTER_PACKET;
179 gspca_frame_add(gspca_dev, packet_type,
180 buffer, data_len);
181 }
182 }
183quit_stream:
184 if (gspca_dev->present) {
185 mutex_lock(&gspca_dev->usb_lock);
186 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
187 mutex_unlock(&gspca_dev->usb_lock);
188 }
189 kfree(buffer);
190}
191
192
193static int sd_config(struct gspca_dev *gspca_dev,
194 const struct usb_device_id *id)
195{
196 struct cam *cam = &gspca_dev->cam;
197 struct sd *dev = (struct sd *) gspca_dev;
198 int ret;
199
200 gspca_dbg(gspca_dev, D_PROBE,
201 "SQ9050 camera detected (vid/pid 0x%04X:0x%04X)\n",
202 id->idVendor, id->idProduct);
203
204 ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
205 if (ret < 0) {
206 gspca_err(gspca_dev, "Get version command failed\n");
207 return ret;
208 }
209
210 ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
211 if (ret < 0) {
212 gspca_err(gspca_dev, "Reading version command failed\n");
213 return ret;
214 }
215
216 gspca_dbg(gspca_dev, D_PROBE,
217 "SQ9050 ID string: %02x - %*ph\n",
218 gspca_dev->usb_buf[3], 6, gspca_dev->usb_buf + 14);
219
220 cam->cam_mode = sq905c_mode;
221 cam->nmodes = 2;
222 if (gspca_dev->usb_buf[15] == 0)
223 cam->nmodes = 1;
224
225 cam->bulk_size = 32;
226 cam->bulk = 1;
227 INIT_WORK(&dev->work_struct, sq905c_dostream);
228 return 0;
229}
230
231
232
233static void sd_stop0(struct gspca_dev *gspca_dev)
234{
235 struct sd *dev = (struct sd *) gspca_dev;
236
237
238 mutex_unlock(&gspca_dev->usb_lock);
239
240 destroy_workqueue(dev->work_thread);
241 dev->work_thread = NULL;
242 mutex_lock(&gspca_dev->usb_lock);
243}
244
245
246static int sd_init(struct gspca_dev *gspca_dev)
247{
248
249 return sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
250}
251
252
253static int sd_start(struct gspca_dev *gspca_dev)
254{
255 struct sd *dev = (struct sd *) gspca_dev;
256 int ret;
257
258 dev->cap_mode = gspca_dev->cam.cam_mode;
259
260 switch (gspca_dev->pixfmt.width) {
261 case 640:
262 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
263 dev->cap_mode++;
264 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
265 SQ905C_CAPTURE_INDEX);
266 break;
267 default:
268 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
269 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
270 SQ905C_CAPTURE_INDEX);
271 }
272
273 if (ret < 0) {
274 gspca_err(gspca_dev, "Start streaming command failed\n");
275 return ret;
276 }
277
278 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
279 queue_work(dev->work_thread, &dev->work_struct);
280
281 return 0;
282}
283
284
285static const struct usb_device_id device_table[] = {
286 {USB_DEVICE(0x2770, 0x905c)},
287 {USB_DEVICE(0x2770, 0x9050)},
288 {USB_DEVICE(0x2770, 0x9051)},
289 {USB_DEVICE(0x2770, 0x9052)},
290 {USB_DEVICE(0x2770, 0x913d)},
291 {}
292};
293
294MODULE_DEVICE_TABLE(usb, device_table);
295
296
297static const struct sd_desc sd_desc = {
298 .name = MODULE_NAME,
299 .config = sd_config,
300 .init = sd_init,
301 .start = sd_start,
302 .stop0 = sd_stop0,
303};
304
305
306static int sd_probe(struct usb_interface *intf,
307 const struct usb_device_id *id)
308{
309 return gspca_dev_probe(intf, id,
310 &sd_desc,
311 sizeof(struct sd),
312 THIS_MODULE);
313}
314
315static struct usb_driver sd_driver = {
316 .name = MODULE_NAME,
317 .id_table = device_table,
318 .probe = sd_probe,
319 .disconnect = gspca_disconnect,
320#ifdef CONFIG_PM
321 .suspend = gspca_suspend,
322 .resume = gspca_resume,
323 .reset_resume = gspca_resume,
324#endif
325};
326
327module_usb_driver(sd_driver);
328