1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#define MODULE_NAME "konica"
30
31#include <linux/input.h>
32#include "gspca.h"
33
34MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
35MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
36MODULE_LICENSE("GPL");
37
38#define WHITEBAL_REG 0x01
39#define BRIGHTNESS_REG 0x02
40#define SHARPNESS_REG 0x03
41#define CONTRAST_REG 0x04
42#define SATURATION_REG 0x05
43
44
45struct sd {
46 struct gspca_dev gspca_dev;
47 struct urb *last_data_urb;
48 u8 snapshot_pressed;
49};
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66static const struct v4l2_pix_format vga_mode[] = {
67 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
68 .bytesperline = 160,
69 .sizeimage = 160 * 136 * 3 / 2 + 960,
70 .colorspace = V4L2_COLORSPACE_SRGB,
71 .priv = 0x0a},
72 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
73 .bytesperline = 176,
74 .sizeimage = 176 * 144 * 3 / 2 + 960,
75 .colorspace = V4L2_COLORSPACE_SRGB,
76 .priv = 0x04},
77 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
78 .bytesperline = 320,
79 .sizeimage = 320 * 240 * 3 / 2 + 960,
80 .colorspace = V4L2_COLORSPACE_SRGB,
81 .priv = 0x05},
82};
83
84static void sd_isoc_irq(struct urb *urb);
85
86static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
87{
88 struct usb_device *dev = gspca_dev->dev;
89 int ret;
90
91 if (gspca_dev->usb_err < 0)
92 return;
93 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
94 0x02,
95 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
96 value,
97 index,
98 NULL,
99 0,
100 1000);
101 if (ret < 0) {
102 pr_err("reg_w err writing %02x to %02x: %d\n",
103 value, index, ret);
104 gspca_dev->usb_err = ret;
105 }
106}
107
108static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
109{
110 struct usb_device *dev = gspca_dev->dev;
111 int ret;
112
113 if (gspca_dev->usb_err < 0)
114 return;
115 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
116 0x03,
117 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118 value,
119 index,
120 gspca_dev->usb_buf,
121 2,
122 1000);
123 if (ret < 0) {
124 pr_err("reg_r err %d\n", ret);
125 gspca_dev->usb_err = ret;
126 }
127}
128
129static void konica_stream_on(struct gspca_dev *gspca_dev)
130{
131 reg_w(gspca_dev, 1, 0x0b);
132}
133
134static void konica_stream_off(struct gspca_dev *gspca_dev)
135{
136 reg_w(gspca_dev, 0, 0x0b);
137}
138
139
140static int sd_config(struct gspca_dev *gspca_dev,
141 const struct usb_device_id *id)
142{
143 gspca_dev->cam.cam_mode = vga_mode;
144 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
145 gspca_dev->cam.no_urb_create = 1;
146
147 return 0;
148}
149
150
151static int sd_init(struct gspca_dev *gspca_dev)
152{
153 int i;
154
155
156
157
158
159
160 msleep(6000);
161 for (i = 0; i < 20; i++) {
162 reg_r(gspca_dev, 0, 0x10);
163 if (gspca_dev->usb_buf[0] == 3)
164 break;
165 msleep(100);
166 }
167 reg_w(gspca_dev, 0, 0x0d);
168
169 return gspca_dev->usb_err;
170}
171
172static int sd_start(struct gspca_dev *gspca_dev)
173{
174 struct sd *sd = (struct sd *) gspca_dev;
175 struct urb *urb;
176 int i, n, packet_size;
177 struct usb_host_interface *alt;
178 struct usb_interface *intf;
179
180 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
181 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
182 if (!alt) {
183 pr_err("Couldn't get altsetting\n");
184 return -EIO;
185 }
186
187 if (alt->desc.bNumEndpoints < 2)
188 return -ENODEV;
189
190 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
191
192 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
193 reg_w(gspca_dev, n, 0x08);
194
195 konica_stream_on(gspca_dev);
196
197 if (gspca_dev->usb_err)
198 return gspca_dev->usb_err;
199
200
201#if MAX_NURBS < 4
202#error "Not enough URBs in the gspca table"
203#endif
204#define SD_NPKT 32
205 for (n = 0; n < 4; n++) {
206 i = n & 1 ? 0 : 1;
207 packet_size =
208 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
209 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
210 if (!urb)
211 return -ENOMEM;
212 gspca_dev->urb[n] = urb;
213 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
214 packet_size * SD_NPKT,
215 GFP_KERNEL,
216 &urb->transfer_dma);
217 if (urb->transfer_buffer == NULL) {
218 pr_err("usb_buffer_alloc failed\n");
219 return -ENOMEM;
220 }
221
222 urb->dev = gspca_dev->dev;
223 urb->context = gspca_dev;
224 urb->transfer_buffer_length = packet_size * SD_NPKT;
225 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
226 n & 1 ? 0x81 : 0x82);
227 urb->transfer_flags = URB_ISO_ASAP
228 | URB_NO_TRANSFER_DMA_MAP;
229 urb->interval = 1;
230 urb->complete = sd_isoc_irq;
231 urb->number_of_packets = SD_NPKT;
232 for (i = 0; i < SD_NPKT; i++) {
233 urb->iso_frame_desc[i].length = packet_size;
234 urb->iso_frame_desc[i].offset = packet_size * i;
235 }
236 }
237
238 return 0;
239}
240
241static void sd_stopN(struct gspca_dev *gspca_dev)
242{
243 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
244
245 konica_stream_off(gspca_dev);
246#if IS_ENABLED(CONFIG_INPUT)
247
248
249 if (sd->snapshot_pressed) {
250 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
251 input_sync(gspca_dev->input_dev);
252 sd->snapshot_pressed = 0;
253 }
254#endif
255}
256
257
258static void sd_isoc_irq(struct urb *urb)
259{
260 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
261 struct sd *sd = (struct sd *) gspca_dev;
262 struct urb *data_urb, *status_urb;
263 u8 *data;
264 int i, st;
265
266 PDEBUG(D_PACK, "sd isoc irq");
267 if (!gspca_dev->streaming)
268 return;
269
270 if (urb->status != 0) {
271 if (urb->status == -ESHUTDOWN)
272 return;
273#ifdef CONFIG_PM
274 if (gspca_dev->frozen)
275 return;
276#endif
277 PERR("urb status: %d", urb->status);
278 st = usb_submit_urb(urb, GFP_ATOMIC);
279 if (st < 0)
280 pr_err("resubmit urb error %d\n", st);
281 return;
282 }
283
284
285 if (urb->transfer_buffer_length > 32) {
286 sd->last_data_urb = urb;
287 return;
288 }
289
290 status_urb = urb;
291 data_urb = sd->last_data_urb;
292 sd->last_data_urb = NULL;
293
294 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
295 PERR("lost sync on frames");
296 goto resubmit;
297 }
298
299 if (data_urb->number_of_packets != status_urb->number_of_packets) {
300 PERR("no packets does not match, data: %d, status: %d",
301 data_urb->number_of_packets,
302 status_urb->number_of_packets);
303 goto resubmit;
304 }
305
306 for (i = 0; i < status_urb->number_of_packets; i++) {
307 if (data_urb->iso_frame_desc[i].status ||
308 status_urb->iso_frame_desc[i].status) {
309 PERR("pkt %d data-status %d, status-status %d", i,
310 data_urb->iso_frame_desc[i].status,
311 status_urb->iso_frame_desc[i].status);
312 gspca_dev->last_packet_type = DISCARD_PACKET;
313 continue;
314 }
315
316 if (status_urb->iso_frame_desc[i].actual_length != 1) {
317 PERR("bad status packet length %d",
318 status_urb->iso_frame_desc[i].actual_length);
319 gspca_dev->last_packet_type = DISCARD_PACKET;
320 continue;
321 }
322
323 st = *((u8 *)status_urb->transfer_buffer
324 + status_urb->iso_frame_desc[i].offset);
325
326 data = (u8 *)data_urb->transfer_buffer
327 + data_urb->iso_frame_desc[i].offset;
328
329
330
331
332
333
334
335
336
337
338 if (st & 0x80) {
339 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
340 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
341 } else {
342#if IS_ENABLED(CONFIG_INPUT)
343 u8 button_state = st & 0x40 ? 1 : 0;
344 if (sd->snapshot_pressed != button_state) {
345 input_report_key(gspca_dev->input_dev,
346 KEY_CAMERA,
347 button_state);
348 input_sync(gspca_dev->input_dev);
349 sd->snapshot_pressed = button_state;
350 }
351#endif
352 if (st & 0x01)
353 continue;
354 }
355 gspca_frame_add(gspca_dev, INTER_PACKET, data,
356 data_urb->iso_frame_desc[i].actual_length);
357 }
358
359resubmit:
360 if (data_urb) {
361 st = usb_submit_urb(data_urb, GFP_ATOMIC);
362 if (st < 0)
363 PERR("usb_submit_urb(data_urb) ret %d", st);
364 }
365 st = usb_submit_urb(status_urb, GFP_ATOMIC);
366 if (st < 0)
367 PERR("usb_submit_urb(status_urb) ret %d\n", st);
368}
369
370static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
371{
372 struct gspca_dev *gspca_dev =
373 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
374
375 gspca_dev->usb_err = 0;
376
377 if (!gspca_dev->streaming)
378 return 0;
379
380 switch (ctrl->id) {
381 case V4L2_CID_BRIGHTNESS:
382 konica_stream_off(gspca_dev);
383 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
384 konica_stream_on(gspca_dev);
385 break;
386 case V4L2_CID_CONTRAST:
387 konica_stream_off(gspca_dev);
388 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
389 konica_stream_on(gspca_dev);
390 break;
391 case V4L2_CID_SATURATION:
392 konica_stream_off(gspca_dev);
393 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
394 konica_stream_on(gspca_dev);
395 break;
396 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
397 konica_stream_off(gspca_dev);
398 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
399 konica_stream_on(gspca_dev);
400 break;
401 case V4L2_CID_SHARPNESS:
402 konica_stream_off(gspca_dev);
403 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
404 konica_stream_on(gspca_dev);
405 break;
406 }
407 return gspca_dev->usb_err;
408}
409
410static const struct v4l2_ctrl_ops sd_ctrl_ops = {
411 .s_ctrl = sd_s_ctrl,
412};
413
414static int sd_init_controls(struct gspca_dev *gspca_dev)
415{
416 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
417
418 gspca_dev->vdev.ctrl_handler = hdl;
419 v4l2_ctrl_handler_init(hdl, 5);
420 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
421 V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
422
423 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
424 V4L2_CID_CONTRAST, 0, 9, 1, 4);
425 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
426 V4L2_CID_SATURATION, 0, 9, 1, 4);
427 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
428 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
429 0, 33, 1, 25);
430 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
431 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
432
433 if (hdl->error) {
434 pr_err("Could not initialize controls\n");
435 return hdl->error;
436 }
437 return 0;
438}
439
440
441static const struct sd_desc sd_desc = {
442 .name = MODULE_NAME,
443 .config = sd_config,
444 .init = sd_init,
445 .init_controls = sd_init_controls,
446 .start = sd_start,
447 .stopN = sd_stopN,
448#if IS_ENABLED(CONFIG_INPUT)
449 .other_input = 1,
450#endif
451};
452
453
454static const struct usb_device_id device_table[] = {
455 {USB_DEVICE(0x04c8, 0x0720)},
456 {}
457};
458MODULE_DEVICE_TABLE(usb, device_table);
459
460
461static int sd_probe(struct usb_interface *intf,
462 const struct usb_device_id *id)
463{
464 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
465 THIS_MODULE);
466}
467
468static struct usb_driver sd_driver = {
469 .name = MODULE_NAME,
470 .id_table = device_table,
471 .probe = sd_probe,
472 .disconnect = gspca_disconnect,
473#ifdef CONFIG_PM
474 .suspend = gspca_suspend,
475 .resume = gspca_resume,
476 .reset_resume = gspca_resume,
477#endif
478};
479
480module_usb_driver(sd_driver);
481