1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * SQ905C subdriver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2009 Theodore Kilgore
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This driver uses work done in
11*4882a593Smuzhiyun * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This driver has also used as a base the sq905c driver
14*4882a593Smuzhiyun * and may contain code fragments from it.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define MODULE_NAME "sq905c"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <linux/workqueue.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include "gspca.h"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
26*4882a593Smuzhiyun MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
27*4882a593Smuzhiyun MODULE_LICENSE("GPL");
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* Default timeouts, in ms */
30*4882a593Smuzhiyun #define SQ905C_CMD_TIMEOUT 500
31*4882a593Smuzhiyun #define SQ905C_DATA_TIMEOUT 1000
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* Maximum transfer size to use. */
34*4882a593Smuzhiyun #define SQ905C_MAX_TRANSFER 0x8000
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define FRAME_HEADER_LEN 0x50
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* Commands. These go in the "value" slot. */
39*4882a593Smuzhiyun #define SQ905C_CLEAR 0xa0 /* clear everything */
40*4882a593Smuzhiyun #define SQ905C_GET_ID 0x14f4 /* Read version number */
41*4882a593Smuzhiyun #define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
42*4882a593Smuzhiyun #define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
43*4882a593Smuzhiyun #define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* For capture, this must go in the "index" slot. */
46*4882a593Smuzhiyun #define SQ905C_CAPTURE_INDEX 0x110f
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* Structure to hold all of our device specific stuff */
49*4882a593Smuzhiyun struct sd {
50*4882a593Smuzhiyun struct gspca_dev gspca_dev; /* !! must be the first item */
51*4882a593Smuzhiyun const struct v4l2_pix_format *cap_mode;
52*4882a593Smuzhiyun /* Driver stuff */
53*4882a593Smuzhiyun struct work_struct work_struct;
54*4882a593Smuzhiyun struct workqueue_struct *work_thread;
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * Most of these cameras will do 640x480 and 320x240. 160x120 works
59*4882a593Smuzhiyun * in theory but gives very poor output. Therefore, not supported.
60*4882a593Smuzhiyun * The 0x2770:0x9050 cameras have max resolution of 320x240.
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun static struct v4l2_pix_format sq905c_mode[] = {
63*4882a593Smuzhiyun { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
64*4882a593Smuzhiyun .bytesperline = 320,
65*4882a593Smuzhiyun .sizeimage = 320 * 240,
66*4882a593Smuzhiyun .colorspace = V4L2_COLORSPACE_SRGB,
67*4882a593Smuzhiyun .priv = 0},
68*4882a593Smuzhiyun { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
69*4882a593Smuzhiyun .bytesperline = 640,
70*4882a593Smuzhiyun .sizeimage = 640 * 480,
71*4882a593Smuzhiyun .colorspace = V4L2_COLORSPACE_SRGB,
72*4882a593Smuzhiyun .priv = 0}
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Send a command to the camera. */
sq905c_command(struct gspca_dev * gspca_dev,u16 command,u16 index)76*4882a593Smuzhiyun static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun int ret;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun ret = usb_control_msg(gspca_dev->dev,
81*4882a593Smuzhiyun usb_sndctrlpipe(gspca_dev->dev, 0),
82*4882a593Smuzhiyun USB_REQ_SYNCH_FRAME, /* request */
83*4882a593Smuzhiyun USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
84*4882a593Smuzhiyun command, index, NULL, 0,
85*4882a593Smuzhiyun SQ905C_CMD_TIMEOUT);
86*4882a593Smuzhiyun if (ret < 0) {
87*4882a593Smuzhiyun pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
88*4882a593Smuzhiyun return ret;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
sq905c_read(struct gspca_dev * gspca_dev,u16 command,u16 index,int size)94*4882a593Smuzhiyun static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
95*4882a593Smuzhiyun int size)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun int ret;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun ret = usb_control_msg(gspca_dev->dev,
100*4882a593Smuzhiyun usb_rcvctrlpipe(gspca_dev->dev, 0),
101*4882a593Smuzhiyun USB_REQ_SYNCH_FRAME, /* request */
102*4882a593Smuzhiyun USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
103*4882a593Smuzhiyun command, index, gspca_dev->usb_buf, size,
104*4882a593Smuzhiyun SQ905C_CMD_TIMEOUT);
105*4882a593Smuzhiyun if (ret < 0) {
106*4882a593Smuzhiyun pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
107*4882a593Smuzhiyun return ret;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * This function is called as a workqueue function and runs whenever the camera
115*4882a593Smuzhiyun * is streaming data. Because it is a workqueue function it is allowed to sleep
116*4882a593Smuzhiyun * so we can use synchronous USB calls. To avoid possible collisions with other
117*4882a593Smuzhiyun * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
118*4882a593Smuzhiyun * performing USB operations using it. In practice we don't really need this
119*4882a593Smuzhiyun * as the camera doesn't provide any controls.
120*4882a593Smuzhiyun */
sq905c_dostream(struct work_struct * work)121*4882a593Smuzhiyun static void sq905c_dostream(struct work_struct *work)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun struct sd *dev = container_of(work, struct sd, work_struct);
124*4882a593Smuzhiyun struct gspca_dev *gspca_dev = &dev->gspca_dev;
125*4882a593Smuzhiyun int bytes_left; /* bytes remaining in current frame. */
126*4882a593Smuzhiyun int data_len; /* size to use for the next read. */
127*4882a593Smuzhiyun int act_len;
128*4882a593Smuzhiyun int packet_type;
129*4882a593Smuzhiyun int ret;
130*4882a593Smuzhiyun u8 *buffer;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL);
133*4882a593Smuzhiyun if (!buffer) {
134*4882a593Smuzhiyun pr_err("Couldn't allocate USB buffer\n");
135*4882a593Smuzhiyun goto quit_stream;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun while (gspca_dev->present && gspca_dev->streaming) {
139*4882a593Smuzhiyun #ifdef CONFIG_PM
140*4882a593Smuzhiyun if (gspca_dev->frozen)
141*4882a593Smuzhiyun break;
142*4882a593Smuzhiyun #endif
143*4882a593Smuzhiyun /* Request the header, which tells the size to download */
144*4882a593Smuzhiyun ret = usb_bulk_msg(gspca_dev->dev,
145*4882a593Smuzhiyun usb_rcvbulkpipe(gspca_dev->dev, 0x81),
146*4882a593Smuzhiyun buffer, FRAME_HEADER_LEN, &act_len,
147*4882a593Smuzhiyun SQ905C_DATA_TIMEOUT);
148*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_STREAM,
149*4882a593Smuzhiyun "Got %d bytes out of %d for header\n",
150*4882a593Smuzhiyun act_len, FRAME_HEADER_LEN);
151*4882a593Smuzhiyun if (ret < 0 || act_len < FRAME_HEADER_LEN)
152*4882a593Smuzhiyun goto quit_stream;
153*4882a593Smuzhiyun /* size is read from 4 bytes starting 0x40, little endian */
154*4882a593Smuzhiyun bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
155*4882a593Smuzhiyun |(buffer[0x43]<<24);
156*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_STREAM, "bytes_left = 0x%x\n",
157*4882a593Smuzhiyun bytes_left);
158*4882a593Smuzhiyun /* We keep the header. It has other information, too. */
159*4882a593Smuzhiyun packet_type = FIRST_PACKET;
160*4882a593Smuzhiyun gspca_frame_add(gspca_dev, packet_type,
161*4882a593Smuzhiyun buffer, FRAME_HEADER_LEN);
162*4882a593Smuzhiyun while (bytes_left > 0 && gspca_dev->present) {
163*4882a593Smuzhiyun data_len = bytes_left > SQ905C_MAX_TRANSFER ?
164*4882a593Smuzhiyun SQ905C_MAX_TRANSFER : bytes_left;
165*4882a593Smuzhiyun ret = usb_bulk_msg(gspca_dev->dev,
166*4882a593Smuzhiyun usb_rcvbulkpipe(gspca_dev->dev, 0x81),
167*4882a593Smuzhiyun buffer, data_len, &act_len,
168*4882a593Smuzhiyun SQ905C_DATA_TIMEOUT);
169*4882a593Smuzhiyun if (ret < 0 || act_len < data_len)
170*4882a593Smuzhiyun goto quit_stream;
171*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_STREAM,
172*4882a593Smuzhiyun "Got %d bytes out of %d for frame\n",
173*4882a593Smuzhiyun data_len, bytes_left);
174*4882a593Smuzhiyun bytes_left -= data_len;
175*4882a593Smuzhiyun if (bytes_left == 0)
176*4882a593Smuzhiyun packet_type = LAST_PACKET;
177*4882a593Smuzhiyun else
178*4882a593Smuzhiyun packet_type = INTER_PACKET;
179*4882a593Smuzhiyun gspca_frame_add(gspca_dev, packet_type,
180*4882a593Smuzhiyun buffer, data_len);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun quit_stream:
184*4882a593Smuzhiyun if (gspca_dev->present) {
185*4882a593Smuzhiyun mutex_lock(&gspca_dev->usb_lock);
186*4882a593Smuzhiyun sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
187*4882a593Smuzhiyun mutex_unlock(&gspca_dev->usb_lock);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun kfree(buffer);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* This function is called at probe time just before sd_init */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)193*4882a593Smuzhiyun static int sd_config(struct gspca_dev *gspca_dev,
194*4882a593Smuzhiyun const struct usb_device_id *id)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun struct cam *cam = &gspca_dev->cam;
197*4882a593Smuzhiyun struct sd *dev = (struct sd *) gspca_dev;
198*4882a593Smuzhiyun int ret;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_PROBE,
201*4882a593Smuzhiyun "SQ9050 camera detected (vid/pid 0x%04X:0x%04X)\n",
202*4882a593Smuzhiyun id->idVendor, id->idProduct);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
205*4882a593Smuzhiyun if (ret < 0) {
206*4882a593Smuzhiyun gspca_err(gspca_dev, "Get version command failed\n");
207*4882a593Smuzhiyun return ret;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
211*4882a593Smuzhiyun if (ret < 0) {
212*4882a593Smuzhiyun gspca_err(gspca_dev, "Reading version command failed\n");
213*4882a593Smuzhiyun return ret;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun /* Note we leave out the usb id and the manufacturing date */
216*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_PROBE,
217*4882a593Smuzhiyun "SQ9050 ID string: %02x - %*ph\n",
218*4882a593Smuzhiyun gspca_dev->usb_buf[3], 6, gspca_dev->usb_buf + 14);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun cam->cam_mode = sq905c_mode;
221*4882a593Smuzhiyun cam->nmodes = 2;
222*4882a593Smuzhiyun if (gspca_dev->usb_buf[15] == 0)
223*4882a593Smuzhiyun cam->nmodes = 1;
224*4882a593Smuzhiyun /* We don't use the buffer gspca allocates so make it small. */
225*4882a593Smuzhiyun cam->bulk_size = 32;
226*4882a593Smuzhiyun cam->bulk = 1;
227*4882a593Smuzhiyun INIT_WORK(&dev->work_struct, sq905c_dostream);
228*4882a593Smuzhiyun return 0;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* called on streamoff with alt==0 and on disconnect */
232*4882a593Smuzhiyun /* the usb_lock is held at entry - restore on exit */
sd_stop0(struct gspca_dev * gspca_dev)233*4882a593Smuzhiyun static void sd_stop0(struct gspca_dev *gspca_dev)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun struct sd *dev = (struct sd *) gspca_dev;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* wait for the work queue to terminate */
238*4882a593Smuzhiyun mutex_unlock(&gspca_dev->usb_lock);
239*4882a593Smuzhiyun /* This waits for sq905c_dostream to finish */
240*4882a593Smuzhiyun destroy_workqueue(dev->work_thread);
241*4882a593Smuzhiyun dev->work_thread = NULL;
242*4882a593Smuzhiyun mutex_lock(&gspca_dev->usb_lock);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)246*4882a593Smuzhiyun static int sd_init(struct gspca_dev *gspca_dev)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun /* connect to the camera and reset it. */
249*4882a593Smuzhiyun return sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Set up for getting frames. */
sd_start(struct gspca_dev * gspca_dev)253*4882a593Smuzhiyun static int sd_start(struct gspca_dev *gspca_dev)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun struct sd *dev = (struct sd *) gspca_dev;
256*4882a593Smuzhiyun int ret;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun dev->cap_mode = gspca_dev->cam.cam_mode;
259*4882a593Smuzhiyun /* "Open the shutter" and set size, to start capture */
260*4882a593Smuzhiyun switch (gspca_dev->pixfmt.width) {
261*4882a593Smuzhiyun case 640:
262*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
263*4882a593Smuzhiyun dev->cap_mode++;
264*4882a593Smuzhiyun ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
265*4882a593Smuzhiyun SQ905C_CAPTURE_INDEX);
266*4882a593Smuzhiyun break;
267*4882a593Smuzhiyun default: /* 320 */
268*4882a593Smuzhiyun gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
269*4882a593Smuzhiyun ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
270*4882a593Smuzhiyun SQ905C_CAPTURE_INDEX);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (ret < 0) {
274*4882a593Smuzhiyun gspca_err(gspca_dev, "Start streaming command failed\n");
275*4882a593Smuzhiyun return ret;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun /* Start the workqueue function to do the streaming */
278*4882a593Smuzhiyun dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
279*4882a593Smuzhiyun if (!dev->work_thread)
280*4882a593Smuzhiyun return -ENOMEM;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun queue_work(dev->work_thread, &dev->work_struct);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* Table of supported USB devices */
288*4882a593Smuzhiyun static const struct usb_device_id device_table[] = {
289*4882a593Smuzhiyun {USB_DEVICE(0x2770, 0x905c)},
290*4882a593Smuzhiyun {USB_DEVICE(0x2770, 0x9050)},
291*4882a593Smuzhiyun {USB_DEVICE(0x2770, 0x9051)},
292*4882a593Smuzhiyun {USB_DEVICE(0x2770, 0x9052)},
293*4882a593Smuzhiyun {USB_DEVICE(0x2770, 0x913d)},
294*4882a593Smuzhiyun {}
295*4882a593Smuzhiyun };
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, device_table);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /* sub-driver description */
300*4882a593Smuzhiyun static const struct sd_desc sd_desc = {
301*4882a593Smuzhiyun .name = MODULE_NAME,
302*4882a593Smuzhiyun .config = sd_config,
303*4882a593Smuzhiyun .init = sd_init,
304*4882a593Smuzhiyun .start = sd_start,
305*4882a593Smuzhiyun .stop0 = sd_stop0,
306*4882a593Smuzhiyun };
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)309*4882a593Smuzhiyun static int sd_probe(struct usb_interface *intf,
310*4882a593Smuzhiyun const struct usb_device_id *id)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun return gspca_dev_probe(intf, id,
313*4882a593Smuzhiyun &sd_desc,
314*4882a593Smuzhiyun sizeof(struct sd),
315*4882a593Smuzhiyun THIS_MODULE);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun static struct usb_driver sd_driver = {
319*4882a593Smuzhiyun .name = MODULE_NAME,
320*4882a593Smuzhiyun .id_table = device_table,
321*4882a593Smuzhiyun .probe = sd_probe,
322*4882a593Smuzhiyun .disconnect = gspca_disconnect,
323*4882a593Smuzhiyun #ifdef CONFIG_PM
324*4882a593Smuzhiyun .suspend = gspca_suspend,
325*4882a593Smuzhiyun .resume = gspca_resume,
326*4882a593Smuzhiyun .reset_resume = gspca_resume,
327*4882a593Smuzhiyun #endif
328*4882a593Smuzhiyun };
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun module_usb_driver(sd_driver);
331