4 * Copyright (C) 2009 Theodore Kilgore
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
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.
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
23 * This driver uses work done in
24 * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
26 * This driver has also used as a base the sq905c driver
27 * and may contain code fragments from it.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #define MODULE_NAME "sq905c"
34 #include <linux/workqueue.h>
35 #include <linux/slab.h>
38 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
39 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
40 MODULE_LICENSE("GPL");
42 /* Default timeouts, in ms */
43 #define SQ905C_CMD_TIMEOUT 500
44 #define SQ905C_DATA_TIMEOUT 1000
46 /* Maximum transfer size to use. */
47 #define SQ905C_MAX_TRANSFER 0x8000
49 #define FRAME_HEADER_LEN 0x50
51 /* Commands. These go in the "value" slot. */
52 #define SQ905C_CLEAR 0xa0 /* clear everything */
53 #define SQ905C_GET_ID 0x14f4 /* Read version number */
54 #define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
55 #define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
56 #define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
58 /* For capture, this must go in the "index" slot. */
59 #define SQ905C_CAPTURE_INDEX 0x110f
61 /* Structure to hold all of our device specific stuff */
63 struct gspca_dev gspca_dev; /* !! must be the first item */
64 const struct v4l2_pix_format *cap_mode;
66 struct work_struct work_struct;
67 struct workqueue_struct *work_thread;
71 * Most of these cameras will do 640x480 and 320x240. 160x120 works
72 * in theory but gives very poor output. Therefore, not supported.
73 * The 0x2770:0x9050 cameras have max resolution of 320x240.
75 static struct v4l2_pix_format sq905c_mode[] = {
76 { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
78 .sizeimage = 320 * 240,
79 .colorspace = V4L2_COLORSPACE_SRGB,
81 { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
83 .sizeimage = 640 * 480,
84 .colorspace = V4L2_COLORSPACE_SRGB,
88 /* Send a command to the camera. */
89 static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
93 ret = usb_control_msg(gspca_dev->dev,
94 usb_sndctrlpipe(gspca_dev->dev, 0),
95 USB_REQ_SYNCH_FRAME, /* request */
96 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 command, index, NULL, 0,
100 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
107 static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
112 ret = usb_control_msg(gspca_dev->dev,
113 usb_rcvctrlpipe(gspca_dev->dev, 0),
114 USB_REQ_SYNCH_FRAME, /* request */
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 command, index, gspca_dev->usb_buf, size,
119 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
127 * This function is called as a workqueue function and runs whenever the camera
128 * is streaming data. Because it is a workqueue function it is allowed to sleep
129 * so we can use synchronous USB calls. To avoid possible collisions with other
130 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
131 * performing USB operations using it. In practice we don't really need this
132 * as the camera doesn't provide any controls.
134 static void sq905c_dostream(struct work_struct *work)
136 struct sd *dev = container_of(work, struct sd, work_struct);
137 struct gspca_dev *gspca_dev = &dev->gspca_dev;
138 int bytes_left; /* bytes remaining in current frame. */
139 int data_len; /* size to use for the next read. */
145 buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
147 pr_err("Couldn't allocate USB buffer\n");
151 while (gspca_dev->present && gspca_dev->streaming) {
153 if (gspca_dev->frozen)
156 /* Request the header, which tells the size to download */
157 ret = usb_bulk_msg(gspca_dev->dev,
158 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
159 buffer, FRAME_HEADER_LEN, &act_len,
160 SQ905C_DATA_TIMEOUT);
162 "Got %d bytes out of %d for header",
163 act_len, FRAME_HEADER_LEN);
164 if (ret < 0 || act_len < FRAME_HEADER_LEN)
166 /* size is read from 4 bytes starting 0x40, little endian */
167 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
169 PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
170 /* We keep the header. It has other information, too. */
171 packet_type = FIRST_PACKET;
172 gspca_frame_add(gspca_dev, packet_type,
173 buffer, FRAME_HEADER_LEN);
174 while (bytes_left > 0 && gspca_dev->present) {
175 data_len = bytes_left > SQ905C_MAX_TRANSFER ?
176 SQ905C_MAX_TRANSFER : bytes_left;
177 ret = usb_bulk_msg(gspca_dev->dev,
178 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
179 buffer, data_len, &act_len,
180 SQ905C_DATA_TIMEOUT);
181 if (ret < 0 || act_len < data_len)
184 "Got %d bytes out of %d for frame",
185 data_len, bytes_left);
186 bytes_left -= data_len;
188 packet_type = LAST_PACKET;
190 packet_type = INTER_PACKET;
191 gspca_frame_add(gspca_dev, packet_type,
196 if (gspca_dev->present) {
197 mutex_lock(&gspca_dev->usb_lock);
198 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
199 mutex_unlock(&gspca_dev->usb_lock);
204 /* This function is called at probe time just before sd_init */
205 static int sd_config(struct gspca_dev *gspca_dev,
206 const struct usb_device_id *id)
208 struct cam *cam = &gspca_dev->cam;
209 struct sd *dev = (struct sd *) gspca_dev;
213 "SQ9050 camera detected"
214 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
216 ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
218 PERR("Get version command failed");
222 ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
224 PERR("Reading version command failed");
227 /* Note we leave out the usb id and the manufacturing date */
229 "SQ9050 ID string: %02x - %*ph",
230 gspca_dev->usb_buf[3], 6, gspca_dev->usb_buf + 14);
232 cam->cam_mode = sq905c_mode;
234 if (gspca_dev->usb_buf[15] == 0)
236 /* We don't use the buffer gspca allocates so make it small. */
239 INIT_WORK(&dev->work_struct, sq905c_dostream);
243 /* called on streamoff with alt==0 and on disconnect */
244 /* the usb_lock is held at entry - restore on exit */
245 static void sd_stop0(struct gspca_dev *gspca_dev)
247 struct sd *dev = (struct sd *) gspca_dev;
249 /* wait for the work queue to terminate */
250 mutex_unlock(&gspca_dev->usb_lock);
251 /* This waits for sq905c_dostream to finish */
252 destroy_workqueue(dev->work_thread);
253 dev->work_thread = NULL;
254 mutex_lock(&gspca_dev->usb_lock);
257 /* this function is called at probe and resume time */
258 static int sd_init(struct gspca_dev *gspca_dev)
262 /* connect to the camera and reset it. */
263 ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
267 /* Set up for getting frames. */
268 static int sd_start(struct gspca_dev *gspca_dev)
270 struct sd *dev = (struct sd *) gspca_dev;
273 dev->cap_mode = gspca_dev->cam.cam_mode;
274 /* "Open the shutter" and set size, to start capture */
275 switch (gspca_dev->pixfmt.width) {
277 PDEBUG(D_STREAM, "Start streaming at high resolution");
279 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
280 SQ905C_CAPTURE_INDEX);
283 PDEBUG(D_STREAM, "Start streaming at medium resolution");
284 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
285 SQ905C_CAPTURE_INDEX);
289 PERR("Start streaming command failed");
292 /* Start the workqueue function to do the streaming */
293 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
294 queue_work(dev->work_thread, &dev->work_struct);
299 /* Table of supported USB devices */
300 static const struct usb_device_id device_table[] = {
301 {USB_DEVICE(0x2770, 0x905c)},
302 {USB_DEVICE(0x2770, 0x9050)},
303 {USB_DEVICE(0x2770, 0x9051)},
304 {USB_DEVICE(0x2770, 0x9052)},
305 {USB_DEVICE(0x2770, 0x913d)},
309 MODULE_DEVICE_TABLE(usb, device_table);
311 /* sub-driver description */
312 static const struct sd_desc sd_desc = {
320 /* -- device connect -- */
321 static int sd_probe(struct usb_interface *intf,
322 const struct usb_device_id *id)
324 return gspca_dev_probe(intf, id,
330 static struct usb_driver sd_driver = {
332 .id_table = device_table,
334 .disconnect = gspca_disconnect,
336 .suspend = gspca_suspend,
337 .resume = gspca_resume,
338 .reset_resume = gspca_resume,
342 module_usb_driver(sd_driver);