1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2009 Theodore Kilgore
10 * This driver uses work done in
11 * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
13 * This driver has also used as a base the sq905c driver
14 * and may contain code fragments from it.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #define MODULE_NAME "sq905c"
21 #include <linux/workqueue.h>
22 #include <linux/slab.h>
25 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
26 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
27 MODULE_LICENSE("GPL");
29 /* Default timeouts, in ms */
30 #define SQ905C_CMD_TIMEOUT 500
31 #define SQ905C_DATA_TIMEOUT 1000
33 /* Maximum transfer size to use. */
34 #define SQ905C_MAX_TRANSFER 0x8000
36 #define FRAME_HEADER_LEN 0x50
38 /* Commands. These go in the "value" slot. */
39 #define SQ905C_CLEAR 0xa0 /* clear everything */
40 #define SQ905C_GET_ID 0x14f4 /* Read version number */
41 #define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
42 #define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
43 #define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
45 /* For capture, this must go in the "index" slot. */
46 #define SQ905C_CAPTURE_INDEX 0x110f
48 /* Structure to hold all of our device specific stuff */
50 struct gspca_dev gspca_dev
; /* !! must be the first item */
51 const struct v4l2_pix_format
*cap_mode
;
53 struct work_struct work_struct
;
54 struct workqueue_struct
*work_thread
;
58 * Most of these cameras will do 640x480 and 320x240. 160x120 works
59 * in theory but gives very poor output. Therefore, not supported.
60 * The 0x2770:0x9050 cameras have max resolution of 320x240.
62 static struct v4l2_pix_format sq905c_mode
[] = {
63 { 320, 240, V4L2_PIX_FMT_SQ905C
, V4L2_FIELD_NONE
,
65 .sizeimage
= 320 * 240,
66 .colorspace
= V4L2_COLORSPACE_SRGB
,
68 { 640, 480, V4L2_PIX_FMT_SQ905C
, V4L2_FIELD_NONE
,
70 .sizeimage
= 640 * 480,
71 .colorspace
= V4L2_COLORSPACE_SRGB
,
75 /* Send a command to the camera. */
76 static int sq905c_command(struct gspca_dev
*gspca_dev
, u16 command
, u16 index
)
80 ret
= usb_control_msg(gspca_dev
->dev
,
81 usb_sndctrlpipe(gspca_dev
->dev
, 0),
82 USB_REQ_SYNCH_FRAME
, /* request */
83 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
84 command
, index
, NULL
, 0,
87 pr_err("%s: usb_control_msg failed (%d)\n", __func__
, ret
);
94 static int sq905c_read(struct gspca_dev
*gspca_dev
, u16 command
, u16 index
,
99 ret
= usb_control_msg(gspca_dev
->dev
,
100 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
101 USB_REQ_SYNCH_FRAME
, /* request */
102 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
103 command
, index
, gspca_dev
->usb_buf
, size
,
106 pr_err("%s: usb_control_msg failed (%d)\n", __func__
, ret
);
114 * This function is called as a workqueue function and runs whenever the camera
115 * is streaming data. Because it is a workqueue function it is allowed to sleep
116 * so we can use synchronous USB calls. To avoid possible collisions with other
117 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
118 * performing USB operations using it. In practice we don't really need this
119 * as the camera doesn't provide any controls.
121 static void sq905c_dostream(struct work_struct
*work
)
123 struct sd
*dev
= container_of(work
, struct sd
, work_struct
);
124 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
125 int bytes_left
; /* bytes remaining in current frame. */
126 int data_len
; /* size to use for the next read. */
132 buffer
= kmalloc(SQ905C_MAX_TRANSFER
, GFP_KERNEL
);
134 pr_err("Couldn't allocate USB buffer\n");
138 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
140 if (gspca_dev
->frozen
)
143 /* Request the header, which tells the size to download */
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
)
153 /* size is read from 4 bytes starting 0x40, little endian */
154 bytes_left
= buffer
[0x40]|(buffer
[0x41]<<8)|(buffer
[0x42]<<16)
156 gspca_dbg(gspca_dev
, D_STREAM
, "bytes_left = 0x%x\n",
158 /* We keep the header. It has other information, too. */
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
)
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
;
176 packet_type
= LAST_PACKET
;
178 packet_type
= INTER_PACKET
;
179 gspca_frame_add(gspca_dev
, packet_type
,
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
);
192 /* This function is called at probe time just before sd_init */
193 static int sd_config(struct gspca_dev
*gspca_dev
,
194 const struct usb_device_id
*id
)
196 struct cam
*cam
= &gspca_dev
->cam
;
197 struct sd
*dev
= (struct sd
*) gspca_dev
;
200 gspca_dbg(gspca_dev
, D_PROBE
,
201 "SQ9050 camera detected (vid/pid 0x%04X:0x%04X)\n",
202 id
->idVendor
, id
->idProduct
);
204 ret
= sq905c_command(gspca_dev
, SQ905C_GET_ID
, 0);
206 gspca_err(gspca_dev
, "Get version command failed\n");
210 ret
= sq905c_read(gspca_dev
, 0xf5, 0, 20);
212 gspca_err(gspca_dev
, "Reading version command failed\n");
215 /* Note we leave out the usb id and the manufacturing date */
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);
220 cam
->cam_mode
= sq905c_mode
;
222 if (gspca_dev
->usb_buf
[15] == 0)
224 /* We don't use the buffer gspca allocates so make it small. */
227 INIT_WORK(&dev
->work_struct
, sq905c_dostream
);
231 /* called on streamoff with alt==0 and on disconnect */
232 /* the usb_lock is held at entry - restore on exit */
233 static void sd_stop0(struct gspca_dev
*gspca_dev
)
235 struct sd
*dev
= (struct sd
*) gspca_dev
;
237 /* wait for the work queue to terminate */
238 mutex_unlock(&gspca_dev
->usb_lock
);
239 /* This waits for sq905c_dostream to finish */
240 destroy_workqueue(dev
->work_thread
);
241 dev
->work_thread
= NULL
;
242 mutex_lock(&gspca_dev
->usb_lock
);
245 /* this function is called at probe and resume time */
246 static int sd_init(struct gspca_dev
*gspca_dev
)
248 /* connect to the camera and reset it. */
249 return sq905c_command(gspca_dev
, SQ905C_CLEAR
, 0);
252 /* Set up for getting frames. */
253 static int sd_start(struct gspca_dev
*gspca_dev
)
255 struct sd
*dev
= (struct sd
*) gspca_dev
;
258 dev
->cap_mode
= gspca_dev
->cam
.cam_mode
;
259 /* "Open the shutter" and set size, to start capture */
260 switch (gspca_dev
->pixfmt
.width
) {
262 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at high resolution\n");
264 ret
= sq905c_command(gspca_dev
, SQ905C_CAPTURE_HI
,
265 SQ905C_CAPTURE_INDEX
);
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
);
274 gspca_err(gspca_dev
, "Start streaming command failed\n");
277 /* Start the workqueue function to do the streaming */
278 dev
->work_thread
= create_singlethread_workqueue(MODULE_NAME
);
279 if (!dev
->work_thread
)
282 queue_work(dev
->work_thread
, &dev
->work_struct
);
287 /* Table of supported USB devices */
288 static const struct usb_device_id device_table
[] = {
289 {USB_DEVICE(0x2770, 0x905c)},
290 {USB_DEVICE(0x2770, 0x9050)},
291 {USB_DEVICE(0x2770, 0x9051)},
292 {USB_DEVICE(0x2770, 0x9052)},
293 {USB_DEVICE(0x2770, 0x913d)},
297 MODULE_DEVICE_TABLE(usb
, device_table
);
299 /* sub-driver description */
300 static const struct sd_desc sd_desc
= {
308 /* -- device connect -- */
309 static int sd_probe(struct usb_interface
*intf
,
310 const struct usb_device_id
*id
)
312 return gspca_dev_probe(intf
, id
,
318 static struct usb_driver sd_driver
= {
320 .id_table
= device_table
,
322 .disconnect
= gspca_disconnect
,
324 .suspend
= gspca_suspend
,
325 .resume
= gspca_resume
,
326 .reset_resume
= gspca_resume
,
330 module_usb_driver(sd_driver
);