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