[media] media/usb/gspca: Add support for Scopium astro webcam (0547:7303)
[firefly-linux-kernel-4.4.55.git] / drivers / media / usb / gspca / dtcs033.c
1 /*
2  * Subdriver for Scopium astro-camera (DTCS033, 0547:7303)
3  *
4  * Copyright (C) 2014 Robert Butora (robert.butora.fi@gmail.com)
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #define MODULE_NAME "dtcs033"
19 #include "gspca.h"
20
21 MODULE_AUTHOR("Robert Butora <robert.butora.fi@gmail.com>");
22 MODULE_DESCRIPTION("Scopium DTCS033 astro-cam USB Camera Driver");
23 MODULE_LICENSE("GPL");
24
25
26 /* send a usb request */
27 static void reg_rw(struct gspca_dev *gspca_dev,
28                 u8 bRequestType, u8 bRequest,
29                 u16 wValue, u16 wIndex, u16 wLength)
30 {
31         struct usb_device *udev = gspca_dev->dev;
32         int ret;
33
34         if (gspca_dev->usb_err < 0)
35                 return;
36
37         ret = usb_control_msg(udev,
38                 usb_rcvctrlpipe(udev, 0),
39                 bRequest,
40                 bRequestType,
41                 wValue, wIndex,
42                 gspca_dev->usb_buf, wLength, 500);
43
44         if (ret < 0) {
45                 gspca_dev->usb_err = ret;
46                 pr_err("usb_control_msg error %d\n", ret);
47         }
48
49         return;
50 }
51 /* send several usb in/out requests */
52 static int reg_reqs(struct gspca_dev *gspca_dev,
53                     const struct usb_ctrlrequest *preqs, int n_reqs)
54 {
55         int i = 0;
56         const struct usb_ctrlrequest *preq;
57
58         while ((i < n_reqs) && (gspca_dev->usb_err >= 0)) {
59
60                 preq = &preqs[i];
61
62                 reg_rw(gspca_dev, preq->bRequestType, preq->bRequest,
63                         preq->wValue, preq->wIndex, preq->wLength);
64
65                 if (gspca_dev->usb_err < 0) {
66
67                         PERR("usb error request no: %d / %d\n",
68                                 i, n_reqs);
69                 } else if (preq->bRequestType & USB_DIR_IN) {
70
71                         PDEBUG(D_STREAM,
72                         "USB IN (%d) returned[%d] %02X %02X %02X %s",
73                                 i,
74                                 preq->wLength,
75                                 gspca_dev->usb_buf[0],
76                                 gspca_dev->usb_buf[1],
77                                 gspca_dev->usb_buf[2],
78                                 preq->wLength > 3 ? "...\n" : "\n");
79                 }
80
81                 i++;
82         }
83         return gspca_dev->usb_err;
84 }
85
86 /* -- subdriver interface implementation -- */
87
88 #define DT_COLS (640)
89 static const struct v4l2_pix_format dtcs033_mode[] = {
90         /* raw Bayer patterned output */
91         {DT_COLS, 480, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,
92                 .bytesperline = DT_COLS,
93                 .sizeimage = DT_COLS*480,
94                 .colorspace = V4L2_COLORSPACE_SRGB,
95         },
96         /* this mode will demosaic the Bayer pattern */
97         {DT_COLS, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
98                 .bytesperline = DT_COLS,
99                 .sizeimage = DT_COLS*480,
100                 .colorspace = V4L2_COLORSPACE_SRGB,
101         }
102 };
103
104 /* config called at probe time */
105 static int sd_config(struct gspca_dev *gspca_dev,
106                 const struct usb_device_id *id)
107 {
108         gspca_dev->cam.cam_mode = dtcs033_mode;
109         gspca_dev->cam.nmodes = ARRAY_SIZE(dtcs033_mode);
110
111         gspca_dev->cam.bulk = 1;
112         gspca_dev->cam.bulk_nurbs = 1;
113         gspca_dev->cam.bulk_size = DT_COLS*512;
114
115         return 0;
116 }
117
118 /* init called at probe and resume time */
119 static int sd_init(struct gspca_dev *gspca_dev)
120 {
121         return 0;
122 }
123
124 /* start stop the camera */
125 static int  dtcs033_start(struct gspca_dev *gspca_dev);
126 static void dtcs033_stopN(struct gspca_dev *gspca_dev);
127
128 /* intercept camera image data */
129 static void dtcs033_pkt_scan(struct gspca_dev *gspca_dev,
130                         u8 *data,  /* packet data */
131                         int len)   /* packet data length */
132 {
133         /* drop incomplete frames */
134         if (len != DT_COLS*512) {
135                 gspca_dev->last_packet_type = DISCARD_PACKET;
136                 /* gspca.c: discard invalidates the whole frame. */
137                 return;
138         }
139
140         /* forward complete frames */
141         gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
142         gspca_frame_add(gspca_dev, INTER_PACKET,
143                 data + 16*DT_COLS,
144                 len  - 32*DT_COLS); /* skip first & last 16 lines */
145         gspca_frame_add(gspca_dev, LAST_PACKET,  NULL, 0);
146
147         return;
148 }
149
150 /* -- controls: exposure and gain -- */
151
152 static void dtcs033_setexposure(struct gspca_dev *gspca_dev,
153                         s32 expo, s32 gain)
154 {
155         /* gain [dB] encoding */
156         u16 sGain   = (u16)gain;
157         u16 gainVal = 224+(sGain-14)*(768-224)/(33-14);
158         u16 wIndex =  0x0100|(0x00FF&gainVal);
159         u16 wValue = (0xFF00&gainVal)>>8;
160
161         /* exposure time [msec] encoding */
162         u16 sXTime   = (u16)expo;
163         u16 xtimeVal = (524*(150-(sXTime-1)))/150;
164
165         const u8 bRequestType =
166                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
167         const u8 bRequest = 0x18;
168
169         reg_rw(gspca_dev,
170                 bRequestType, bRequest, wValue, wIndex, 0);
171         if (gspca_dev->usb_err < 0)
172                 PERR("usb error in setexposure(gain) sequence.\n");
173
174         reg_rw(gspca_dev,
175                 bRequestType, bRequest, (xtimeVal<<4), 0x6300, 0);
176         if (gspca_dev->usb_err < 0)
177                 PERR("usb error in setexposure(time) sequence.\n");
178 }
179
180 /* specific webcam descriptor */
181 struct sd {
182         struct gspca_dev gspca_dev;/* !! must be the first item */
183
184         /* exposure & gain controls */
185         struct {
186                 struct v4l2_ctrl *exposure;
187                 struct v4l2_ctrl *gain;
188         };
189 };
190
191 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
192 {
193         struct gspca_dev *gspca_dev =
194         container_of(ctrl->handler,
195                 struct gspca_dev, ctrl_handler);
196         struct sd *sd = (struct sd *) gspca_dev;
197
198         gspca_dev->usb_err = 0;
199
200         if (!gspca_dev->streaming)
201                 return 0;
202
203         switch (ctrl->id) {
204         case V4L2_CID_EXPOSURE:
205                 dtcs033_setexposure(gspca_dev,
206                                 ctrl->val, sd->gain->val);
207                 break;
208         case V4L2_CID_GAIN:
209                 dtcs033_setexposure(gspca_dev,
210                                 sd->exposure->val, ctrl->val);
211                 break;
212         }
213         return gspca_dev->usb_err;
214 }
215
216 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
217         .s_ctrl = sd_s_ctrl,
218 };
219
220 static int dtcs033_init_controls(struct gspca_dev *gspca_dev)
221 {
222         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
223         struct sd *sd = (struct sd *) gspca_dev;
224
225         gspca_dev->vdev.ctrl_handler = hdl;
226         v4l2_ctrl_handler_init(hdl, 2);
227         /*                               min max step default */
228         sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
229                                 V4L2_CID_EXPOSURE,
230                                 1,  150,  1,  75);/* [msec] */
231         sd->gain     = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
232                                 V4L2_CID_GAIN,
233                                 14,  33,  1,  24);/* [dB] */
234         if (hdl->error) {
235                 PERR("Could not initialize controls: %d\n",
236                         hdl->error);
237                 return hdl->error;
238         }
239
240         v4l2_ctrl_cluster(2, &sd->exposure);
241         return 0;
242 }
243
244 /* sub-driver description */
245 static const struct sd_desc sd_desc = {
246         .name     = MODULE_NAME,
247         .config   = sd_config,
248         .init     = sd_init,
249         .start    = dtcs033_start,
250         .stopN    = dtcs033_stopN,
251         .pkt_scan = dtcs033_pkt_scan,
252         .init_controls = dtcs033_init_controls,
253 };
254
255 /* -- module initialisation -- */
256
257 static const struct usb_device_id device_table[] = {
258         {USB_DEVICE(0x0547, 0x7303)},
259         {}
260 };
261 MODULE_DEVICE_TABLE(usb, device_table);
262
263 /* device connect */
264 static int sd_probe(struct usb_interface *intf,
265                         const struct usb_device_id *id)
266 {
267         return gspca_dev_probe(intf, id,
268                         &sd_desc, sizeof(struct sd),
269                         THIS_MODULE);
270 }
271
272 static struct usb_driver sd_driver = {
273         .name       = MODULE_NAME,
274         .id_table   = device_table,
275         .probe      = sd_probe,
276         .disconnect   = gspca_disconnect,
277 #ifdef CONFIG_PM
278         .suspend      = gspca_suspend,
279         .resume       = gspca_resume,
280         .reset_resume = gspca_resume,
281 #endif
282 };
283 module_usb_driver(sd_driver);
284
285
286 /* ---------------------------------------------------------
287  USB requests to start/stop the camera [USB 2.0 spec Ch.9].
288
289  bRequestType :
290  0x40 =  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
291  0xC0 =  USB_DIR_IN  | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
292 */
293 static const struct usb_ctrlrequest dtcs033_start_reqs[] = {
294 /* -- bRequest,wValue,wIndex,wLength */
295 { 0x40, 0x01, 0x0001, 0x000F, 0x0000 },
296 { 0x40, 0x01, 0x0000, 0x000F, 0x0000 },
297 { 0x40, 0x01, 0x0001, 0x000F, 0x0000 },
298 { 0x40, 0x18, 0x0000, 0x7F00, 0x0000 },
299 { 0x40, 0x18, 0x0000, 0x1001, 0x0000 },
300 { 0x40, 0x18, 0x0000, 0x0004, 0x0000 },
301 { 0x40, 0x18, 0x0000, 0x7F01, 0x0000 },
302 { 0x40, 0x18, 0x30E0, 0x0009, 0x0000 },
303 { 0x40, 0x18, 0x0500, 0x012C, 0x0000 },
304 { 0x40, 0x18, 0x0380, 0x0200, 0x0000 },
305 { 0x40, 0x18, 0x0000, 0x035C, 0x0000 },
306 { 0x40, 0x18, 0x05C0, 0x0438, 0x0000 },
307 { 0x40, 0x18, 0x0440, 0x0500, 0x0000 },
308 { 0x40, 0x18, 0x0000, 0x0668, 0x0000 },
309 { 0x40, 0x18, 0x0000, 0x0700, 0x0000 },
310 { 0x40, 0x18, 0x0000, 0x0800, 0x0000 },
311 { 0x40, 0x18, 0x0000, 0x0900, 0x0000 },
312 { 0x40, 0x18, 0x0000, 0x0A00, 0x0000 },
313 { 0x40, 0x18, 0x0000, 0x0B00, 0x0000 },
314 { 0x40, 0x18, 0x30E0, 0x6009, 0x0000 },
315 { 0x40, 0x18, 0x0500, 0x612C, 0x0000 },
316 { 0x40, 0x18, 0x2090, 0x6274, 0x0000 },
317 { 0x40, 0x18, 0x05C0, 0x6338, 0x0000 },
318 { 0x40, 0x18, 0x0000, 0x6400, 0x0000 },
319 { 0x40, 0x18, 0x05C0, 0x6538, 0x0000 },
320 { 0x40, 0x18, 0x0000, 0x6600, 0x0000 },
321 { 0x40, 0x18, 0x0680, 0x6744, 0x0000 },
322 { 0x40, 0x18, 0x0000, 0x6800, 0x0000 },
323 { 0x40, 0x18, 0x0000, 0x6900, 0x0000 },
324 { 0x40, 0x18, 0x0000, 0x6A00, 0x0000 },
325 { 0x40, 0x18, 0x0000, 0x6B00, 0x0000 },
326 { 0x40, 0x18, 0x0000, 0x6C00, 0x0000 },
327 { 0x40, 0x18, 0x0000, 0x6D00, 0x0000 },
328 { 0x40, 0x18, 0x0000, 0x6E00, 0x0000 },
329 { 0x40, 0x18, 0x0000, 0x808C, 0x0000 },
330 { 0x40, 0x18, 0x0010, 0x8101, 0x0000 },
331 { 0x40, 0x18, 0x30E0, 0x8200, 0x0000 },
332 { 0x40, 0x18, 0x0810, 0x832C, 0x0000 },
333 { 0x40, 0x18, 0x0680, 0x842B, 0x0000 },
334 { 0x40, 0x18, 0x0000, 0x8500, 0x0000 },
335 { 0x40, 0x18, 0x0000, 0x8600, 0x0000 },
336 { 0x40, 0x18, 0x0280, 0x8715, 0x0000 },
337 { 0x40, 0x18, 0x0000, 0x880C, 0x0000 },
338 { 0x40, 0x18, 0x0010, 0x8901, 0x0000 },
339 { 0x40, 0x18, 0x30E0, 0x8A00, 0x0000 },
340 { 0x40, 0x18, 0x0810, 0x8B2C, 0x0000 },
341 { 0x40, 0x18, 0x0680, 0x8C2B, 0x0000 },
342 { 0x40, 0x18, 0x0000, 0x8D00, 0x0000 },
343 { 0x40, 0x18, 0x0000, 0x8E00, 0x0000 },
344 { 0x40, 0x18, 0x0280, 0x8F15, 0x0000 },
345 { 0x40, 0x18, 0x0010, 0xD040, 0x0000 },
346 { 0x40, 0x18, 0x0000, 0xD100, 0x0000 },
347 { 0x40, 0x18, 0x00B0, 0xD20A, 0x0000 },
348 { 0x40, 0x18, 0x0000, 0xD300, 0x0000 },
349 { 0x40, 0x18, 0x30E2, 0xD40D, 0x0000 },
350 { 0x40, 0x18, 0x0001, 0xD5C0, 0x0000 },
351 { 0x40, 0x18, 0x00A0, 0xD60A, 0x0000 },
352 { 0x40, 0x18, 0x0000, 0xD700, 0x0000 },
353 { 0x40, 0x18, 0x0000, 0x7F00, 0x0000 },
354 { 0x40, 0x18, 0x0000, 0x1501, 0x0000 },
355 { 0x40, 0x18, 0x0001, 0x01FF, 0x0000 },
356 { 0x40, 0x18, 0x0000, 0x0200, 0x0000 },
357 { 0x40, 0x18, 0x0000, 0x0304, 0x0000 },
358 { 0x40, 0x18, 0x0000, 0x1101, 0x0000 },
359 { 0x40, 0x18, 0x0000, 0x1201, 0x0000 },
360 { 0x40, 0x18, 0x0000, 0x1300, 0x0000 },
361 { 0x40, 0x18, 0x0000, 0x1400, 0x0000 },
362 { 0x40, 0x18, 0x0000, 0x1601, 0x0000 },
363 { 0x40, 0x18, 0x0000, 0x1800, 0x0000 },
364 { 0x40, 0x18, 0x0000, 0x1900, 0x0000 },
365 { 0x40, 0x18, 0x0000, 0x1A00, 0x0000 },
366 { 0x40, 0x18, 0x2000, 0x1B00, 0x0000 },
367 { 0x40, 0x18, 0x0000, 0x1C00, 0x0000 },
368 { 0x40, 0x18, 0x0000, 0x2100, 0x0000 },
369 { 0x40, 0x18, 0x00C0, 0x228E, 0x0000 },
370 { 0x40, 0x18, 0x0000, 0x3001, 0x0000 },
371 { 0x40, 0x18, 0x0010, 0x3101, 0x0000 },
372 { 0x40, 0x18, 0x0008, 0x3301, 0x0000 },
373 { 0x40, 0x18, 0x0000, 0x3400, 0x0000 },
374 { 0x40, 0x18, 0x0012, 0x3549, 0x0000 },
375 { 0x40, 0x18, 0x0000, 0x3620, 0x0000 },
376 { 0x40, 0x18, 0x0001, 0x3700, 0x0000 },
377 { 0x40, 0x18, 0x0000, 0x4000, 0x0000 },
378 { 0x40, 0x18, 0xFFFF, 0x41FF, 0x0000 },
379 { 0x40, 0x18, 0xFFFF, 0x42FF, 0x0000 },
380 { 0x40, 0x18, 0x0000, 0x500F, 0x0000 },
381 { 0x40, 0x18, 0x2272, 0x5108, 0x0000 },
382 { 0x40, 0x18, 0x2272, 0x5208, 0x0000 },
383 { 0x40, 0x18, 0xFFFF, 0x53FF, 0x0000 },
384 { 0x40, 0x18, 0xFFFF, 0x54FF, 0x0000 },
385 { 0x40, 0x18, 0x0000, 0x6000, 0x0000 },
386 { 0x40, 0x18, 0x0000, 0x6102, 0x0000 },
387 { 0x40, 0x18, 0x0010, 0x6214, 0x0000 },
388 { 0x40, 0x18, 0x0C80, 0x6300, 0x0000 },
389 { 0x40, 0x18, 0x0000, 0x6401, 0x0000 },
390 { 0x40, 0x18, 0x0680, 0x6551, 0x0000 },
391 { 0x40, 0x18, 0xFFFF, 0x66FF, 0x0000 },
392 { 0x40, 0x18, 0x0000, 0x6702, 0x0000 },
393 { 0x40, 0x18, 0x0010, 0x6800, 0x0000 },
394 { 0x40, 0x18, 0x0000, 0x6900, 0x0000 },
395 { 0x40, 0x18, 0x0000, 0x6A00, 0x0000 },
396 { 0x40, 0x18, 0x0000, 0x6B00, 0x0000 },
397 { 0x40, 0x18, 0x0000, 0x6C00, 0x0000 },
398 { 0x40, 0x18, 0x0000, 0x6D01, 0x0000 },
399 { 0x40, 0x18, 0x0000, 0x6E00, 0x0000 },
400 { 0x40, 0x18, 0x0000, 0x6F00, 0x0000 },
401 { 0x40, 0x18, 0x0000, 0x7000, 0x0000 },
402 { 0x40, 0x18, 0x0001, 0x7118, 0x0000 },
403 { 0x40, 0x18, 0x0000, 0x2001, 0x0000 },
404 { 0x40, 0x18, 0x0000, 0x1101, 0x0000 },
405 { 0x40, 0x18, 0x0000, 0x1301, 0x0000 },
406 { 0x40, 0x18, 0x0000, 0x1300, 0x0000 },
407 { 0x40, 0x18, 0x0000, 0x1501, 0x0000 },
408 { 0xC0, 0x11, 0x0000, 0x24C0, 0x0003 },
409 { 0x40, 0x18, 0x0000, 0x3000, 0x0000 },
410 { 0x40, 0x18, 0x0000, 0x3620, 0x0000 },
411 { 0x40, 0x18, 0x0000, 0x1501, 0x0000 },
412 { 0x40, 0x18, 0x0010, 0x6300, 0x0000 },
413 { 0x40, 0x18, 0x0002, 0x01F0, 0x0000 },
414 { 0x40, 0x01, 0x0003, 0x000F, 0x0000 }
415 };
416
417 static const struct usb_ctrlrequest dtcs033_stop_reqs[] = {
418 /* -- bRequest,wValue,wIndex,wLength */
419 { 0x40, 0x01, 0x0001, 0x000F, 0x0000 },
420 { 0x40, 0x01, 0x0000, 0x000F, 0x0000 },
421 { 0x40, 0x18, 0x0000, 0x0003, 0x0000 }
422 };
423 static int dtcs033_start(struct gspca_dev *gspca_dev)
424 {
425         return reg_reqs(gspca_dev, dtcs033_start_reqs,
426                 ARRAY_SIZE(dtcs033_start_reqs));
427 }
428
429 static void dtcs033_stopN(struct gspca_dev *gspca_dev)
430 {
431         reg_reqs(gspca_dev, dtcs033_stop_reqs,
432                 ARRAY_SIZE(dtcs033_stop_reqs));
433         return;
434 }