GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / usb / gspca / sq905c.c
1 /*
2  * SQ905C subdriver
3  *
4  * Copyright (C) 2009 Theodore Kilgore
5  *
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.
10  *
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.
15  */
16
17 /*
18  *
19  * This driver uses work done in
20  * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
21  *
22  * This driver has also used as a base the sq905c driver
23  * and may contain code fragments from it.
24  */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #define MODULE_NAME "sq905c"
29
30 #include <linux/workqueue.h>
31 #include <linux/slab.h>
32 #include "gspca.h"
33
34 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
35 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
36 MODULE_LICENSE("GPL");
37
38 /* Default timeouts, in ms */
39 #define SQ905C_CMD_TIMEOUT 500
40 #define SQ905C_DATA_TIMEOUT 1000
41
42 /* Maximum transfer size to use. */
43 #define SQ905C_MAX_TRANSFER 0x8000
44
45 #define FRAME_HEADER_LEN 0x50
46
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 */
53
54 /* For capture, this must go in the "index" slot. */
55 #define SQ905C_CAPTURE_INDEX 0x110f
56
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;
64 };
65
66 /*
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.
70  */
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}
82 };
83
84 /* Send a command to the camera. */
85 static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
86 {
87         int ret;
88
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;
98         }
99
100         return 0;
101 }
102
103 static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
104                        int size)
105 {
106         int ret;
107
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;
117         }
118
119         return 0;
120 }
121
122 /*
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.
129  */
130 static void sq905c_dostream(struct work_struct *work)
131 {
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;
140
141         buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL);
142         if (!buffer) {
143                 pr_err("Couldn't allocate USB buffer\n");
144                 goto quit_stream;
145         }
146
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                 gspca_dbg(gspca_dev, D_STREAM,
158                           "Got %d bytes out of %d for header\n",
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                 gspca_dbg(gspca_dev, D_STREAM, "bytes_left = 0x%x\n",
166                           bytes_left);
167                 /* We keep the header. It has other information, too. */
168                 packet_type = FIRST_PACKET;
169                 gspca_frame_add(gspca_dev, packet_type,
170                                 buffer, FRAME_HEADER_LEN);
171                 while (bytes_left > 0 && gspca_dev->present) {
172                         data_len = bytes_left > SQ905C_MAX_TRANSFER ?
173                                 SQ905C_MAX_TRANSFER : bytes_left;
174                         ret = usb_bulk_msg(gspca_dev->dev,
175                                 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
176                                 buffer, data_len, &act_len,
177                                 SQ905C_DATA_TIMEOUT);
178                         if (ret < 0 || act_len < data_len)
179                                 goto quit_stream;
180                         gspca_dbg(gspca_dev, D_STREAM,
181                                   "Got %d bytes out of %d for frame\n",
182                                   data_len, bytes_left);
183                         bytes_left -= data_len;
184                         if (bytes_left == 0)
185                                 packet_type = LAST_PACKET;
186                         else
187                                 packet_type = INTER_PACKET;
188                         gspca_frame_add(gspca_dev, packet_type,
189                                         buffer, data_len);
190                 }
191         }
192 quit_stream:
193         if (gspca_dev->present) {
194                 mutex_lock(&gspca_dev->usb_lock);
195                 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
196                 mutex_unlock(&gspca_dev->usb_lock);
197         }
198         kfree(buffer);
199 }
200
201 /* This function is called at probe time just before sd_init */
202 static int sd_config(struct gspca_dev *gspca_dev,
203                 const struct usb_device_id *id)
204 {
205         struct cam *cam = &gspca_dev->cam;
206         struct sd *dev = (struct sd *) gspca_dev;
207         int ret;
208
209         gspca_dbg(gspca_dev, D_PROBE,
210                   "SQ9050 camera detected (vid/pid 0x%04X:0x%04X)\n",
211                   id->idVendor, id->idProduct);
212
213         ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
214         if (ret < 0) {
215                 gspca_err(gspca_dev, "Get version command failed\n");
216                 return ret;
217         }
218
219         ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
220         if (ret < 0) {
221                 gspca_err(gspca_dev, "Reading version command failed\n");
222                 return ret;
223         }
224         /* Note we leave out the usb id and the manufacturing date */
225         gspca_dbg(gspca_dev, D_PROBE,
226                   "SQ9050 ID string: %02x - %*ph\n",
227                   gspca_dev->usb_buf[3], 6, gspca_dev->usb_buf + 14);
228
229         cam->cam_mode = sq905c_mode;
230         cam->nmodes = 2;
231         if (gspca_dev->usb_buf[15] == 0)
232                 cam->nmodes = 1;
233         /* We don't use the buffer gspca allocates so make it small. */
234         cam->bulk_size = 32;
235         cam->bulk = 1;
236         INIT_WORK(&dev->work_struct, sq905c_dostream);
237         return 0;
238 }
239
240 /* called on streamoff with alt==0 and on disconnect */
241 /* the usb_lock is held at entry - restore on exit */
242 static void sd_stop0(struct gspca_dev *gspca_dev)
243 {
244         struct sd *dev = (struct sd *) gspca_dev;
245
246         /* wait for the work queue to terminate */
247         mutex_unlock(&gspca_dev->usb_lock);
248         /* This waits for sq905c_dostream to finish */
249         destroy_workqueue(dev->work_thread);
250         dev->work_thread = NULL;
251         mutex_lock(&gspca_dev->usb_lock);
252 }
253
254 /* this function is called at probe and resume time */
255 static int sd_init(struct gspca_dev *gspca_dev)
256 {
257         /* connect to the camera and reset it. */
258         return sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
259 }
260
261 /* Set up for getting frames. */
262 static int sd_start(struct gspca_dev *gspca_dev)
263 {
264         struct sd *dev = (struct sd *) gspca_dev;
265         int ret;
266
267         dev->cap_mode = gspca_dev->cam.cam_mode;
268         /* "Open the shutter" and set size, to start capture */
269         switch (gspca_dev->pixfmt.width) {
270         case 640:
271                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
272                 dev->cap_mode++;
273                 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
274                                                 SQ905C_CAPTURE_INDEX);
275                 break;
276         default: /* 320 */
277                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
278                 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
279                                                 SQ905C_CAPTURE_INDEX);
280         }
281
282         if (ret < 0) {
283                 gspca_err(gspca_dev, "Start streaming command failed\n");
284                 return ret;
285         }
286         /* Start the workqueue function to do the streaming */
287         dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
288         queue_work(dev->work_thread, &dev->work_struct);
289
290         return 0;
291 }
292
293 /* Table of supported USB devices */
294 static const struct usb_device_id device_table[] = {
295         {USB_DEVICE(0x2770, 0x905c)},
296         {USB_DEVICE(0x2770, 0x9050)},
297         {USB_DEVICE(0x2770, 0x9051)},
298         {USB_DEVICE(0x2770, 0x9052)},
299         {USB_DEVICE(0x2770, 0x913d)},
300         {}
301 };
302
303 MODULE_DEVICE_TABLE(usb, device_table);
304
305 /* sub-driver description */
306 static const struct sd_desc sd_desc = {
307         .name   = MODULE_NAME,
308         .config = sd_config,
309         .init   = sd_init,
310         .start  = sd_start,
311         .stop0  = sd_stop0,
312 };
313
314 /* -- device connect -- */
315 static int sd_probe(struct usb_interface *intf,
316                 const struct usb_device_id *id)
317 {
318         return gspca_dev_probe(intf, id,
319                         &sd_desc,
320                         sizeof(struct sd),
321                         THIS_MODULE);
322 }
323
324 static struct usb_driver sd_driver = {
325         .name       = MODULE_NAME,
326         .id_table   = device_table,
327         .probe      = sd_probe,
328         .disconnect = gspca_disconnect,
329 #ifdef CONFIG_PM
330         .suspend = gspca_suspend,
331         .resume  = gspca_resume,
332         .reset_resume = gspca_resume,
333 #endif
334 };
335
336 module_usb_driver(sd_driver);