xref: /openbmc/linux/drivers/media/usb/gspca/sq905c.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SQ905C subdriver
4  *
5  * Copyright (C) 2009 Theodore Kilgore
6  */
7 
8 /*
9  *
10  * This driver uses work done in
11  * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
12  *
13  * This driver has also used as a base the sq905c driver
14  * and may contain code fragments from it.
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 
25 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
26 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
27 MODULE_LICENSE("GPL");
28 
29 /* Default timeouts, in ms */
30 #define SQ905C_CMD_TIMEOUT 500
31 #define SQ905C_DATA_TIMEOUT 1000
32 
33 /* Maximum transfer size to use. */
34 #define SQ905C_MAX_TRANSFER 0x8000
35 
36 #define FRAME_HEADER_LEN 0x50
37 
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 */
44 
45 /* For capture, this must go in the "index" slot. */
46 #define SQ905C_CAPTURE_INDEX 0x110f
47 
48 /* Structure to hold all of our device specific stuff */
49 struct sd {
50 	struct gspca_dev gspca_dev;	/* !! must be the first item */
51 	const struct v4l2_pix_format *cap_mode;
52 	/* Driver stuff */
53 	struct work_struct work_struct;
54 	struct workqueue_struct *work_thread;
55 };
56 
57 /*
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.
61  */
62 static 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 /* Send a command to the camera. */
76 static 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,                /* request */
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 
94 static 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,		/* request */
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  * 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.
120  */
121 static 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; /* bytes remaining in current frame. */
126 	int data_len;   /* size to use for the next read. */
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 		/* 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)
152 			goto quit_stream;
153 		/* size is read from 4 bytes starting 0x40, little endian */
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 		/* 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)
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 	}
183 quit_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 /* 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)
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 	/* 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);
219 
220 	cam->cam_mode = sq905c_mode;
221 	cam->nmodes = 2;
222 	if (gspca_dev->usb_buf[15] == 0)
223 		cam->nmodes = 1;
224 	/* We don't use the buffer gspca allocates so make it small. */
225 	cam->bulk_size = 32;
226 	cam->bulk = 1;
227 	INIT_WORK(&dev->work_struct, sq905c_dostream);
228 	return 0;
229 }
230 
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)
234 {
235 	struct sd *dev = (struct sd *) gspca_dev;
236 
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);
243 }
244 
245 /* this function is called at probe and resume time */
246 static int sd_init(struct gspca_dev *gspca_dev)
247 {
248 	/* connect to the camera and reset it. */
249 	return sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
250 }
251 
252 /* Set up for getting frames. */
253 static 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 	/* "Open the shutter" and set size, to start capture */
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: /* 320 */
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 	/* Start the workqueue function to do the streaming */
278 	dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
279 	if (!dev->work_thread)
280 		return -ENOMEM;
281 
282 	queue_work(dev->work_thread, &dev->work_struct);
283 
284 	return 0;
285 }
286 
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)},
294 	{}
295 };
296 
297 MODULE_DEVICE_TABLE(usb, device_table);
298 
299 /* sub-driver description */
300 static const struct sd_desc sd_desc = {
301 	.name   = MODULE_NAME,
302 	.config = sd_config,
303 	.init   = sd_init,
304 	.start  = sd_start,
305 	.stop0  = sd_stop0,
306 };
307 
308 /* -- device connect -- */
309 static int sd_probe(struct usb_interface *intf,
310 		const struct usb_device_id *id)
311 {
312 	return gspca_dev_probe(intf, id,
313 			&sd_desc,
314 			sizeof(struct sd),
315 			THIS_MODULE);
316 }
317 
318 static struct usb_driver sd_driver = {
319 	.name       = MODULE_NAME,
320 	.id_table   = device_table,
321 	.probe      = sd_probe,
322 	.disconnect = gspca_disconnect,
323 #ifdef CONFIG_PM
324 	.suspend = gspca_suspend,
325 	.resume  = gspca_resume,
326 	.reset_resume = gspca_resume,
327 #endif
328 };
329 
330 module_usb_driver(sd_driver);
331