]> Git Repo - J-linux.git/blob - drivers/media/usb/gspca/jl2005bcd.c
Merge tag 'trace-v5.13-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[J-linux.git] / drivers / media / usb / gspca / jl2005bcd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Jeilin JL2005B/C/D library
4  *
5  * Copyright (C) 2011 Theodore Kilgore <[email protected]>
6  */
7
8 #define MODULE_NAME "jl2005bcd"
9
10 #include <linux/workqueue.h>
11 #include <linux/slab.h>
12 #include "gspca.h"
13
14
15 MODULE_AUTHOR("Theodore Kilgore <[email protected]>");
16 MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
17 MODULE_LICENSE("GPL");
18
19 /* Default timeouts, in ms */
20 #define JL2005C_CMD_TIMEOUT 500
21 #define JL2005C_DATA_TIMEOUT 1000
22
23 /* Maximum transfer size to use. */
24 #define JL2005C_MAX_TRANSFER 0x200
25 #define FRAME_HEADER_LEN 16
26
27
28 /* specific webcam descriptor */
29 struct sd {
30         struct gspca_dev gspca_dev;  /* !! must be the first item */
31         unsigned char firmware_id[6];
32         const struct v4l2_pix_format *cap_mode;
33         /* Driver stuff */
34         struct work_struct work_struct;
35         u8 frame_brightness;
36         int block_size; /* block size of camera */
37         int vga;        /* 1 if vga cam, 0 if cif cam */
38 };
39
40
41 /* Camera has two resolution settings. What they are depends on model. */
42 static const struct v4l2_pix_format cif_mode[] = {
43         {176, 144, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
44                 .bytesperline = 176,
45                 .sizeimage = 176 * 144,
46                 .colorspace = V4L2_COLORSPACE_SRGB,
47                 .priv = 0},
48         {352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
49                 .bytesperline = 352,
50                 .sizeimage = 352 * 288,
51                 .colorspace = V4L2_COLORSPACE_SRGB,
52                 .priv = 0},
53 };
54
55 static const struct v4l2_pix_format vga_mode[] = {
56         {320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
57                 .bytesperline = 320,
58                 .sizeimage = 320 * 240,
59                 .colorspace = V4L2_COLORSPACE_SRGB,
60                 .priv = 0},
61         {640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
62                 .bytesperline = 640,
63                 .sizeimage = 640 * 480,
64                 .colorspace = V4L2_COLORSPACE_SRGB,
65                 .priv = 0},
66 };
67
68 /*
69  * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
70  * and 0x82 for bulk data transfer.
71  */
72
73 /* All commands are two bytes only */
74 static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command)
75 {
76         int retval;
77
78         memcpy(gspca_dev->usb_buf, command, 2);
79         retval = usb_bulk_msg(gspca_dev->dev,
80                         usb_sndbulkpipe(gspca_dev->dev, 3),
81                         gspca_dev->usb_buf, 2, NULL, 500);
82         if (retval < 0)
83                 pr_err("command write [%02x] error %d\n",
84                        gspca_dev->usb_buf[0], retval);
85         return retval;
86 }
87
88 /* Response to a command is one byte in usb_buf[0], only if requested. */
89 static int jl2005c_read1(struct gspca_dev *gspca_dev)
90 {
91         int retval;
92
93         retval = usb_bulk_msg(gspca_dev->dev,
94                                 usb_rcvbulkpipe(gspca_dev->dev, 0x84),
95                                 gspca_dev->usb_buf, 1, NULL, 500);
96         if (retval < 0)
97                 pr_err("read command [0x%02x] error %d\n",
98                        gspca_dev->usb_buf[0], retval);
99         return retval;
100 }
101
102 /* Response appears in gspca_dev->usb_buf[0] */
103 static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg)
104 {
105         int retval;
106
107         static u8 instruction[2] = {0x95, 0x00};
108         /* put register to read in byte 1 */
109         instruction[1] = reg;
110         /* Send the read request */
111         retval = jl2005c_write2(gspca_dev, instruction);
112         if (retval < 0)
113                 return retval;
114         retval = jl2005c_read1(gspca_dev);
115
116         return retval;
117 }
118
119 static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev)
120 {
121         int i;
122         int retval;
123         int frame_brightness = 0;
124
125         static u8 instruction[2] = {0x7f, 0x01};
126
127         retval = jl2005c_write2(gspca_dev, instruction);
128         if (retval < 0)
129                 return retval;
130
131         i = 0;
132         while (i < 20 && !frame_brightness) {
133                 /* If we tried 20 times, give up. */
134                 retval = jl2005c_read_reg(gspca_dev, 0x7e);
135                 if (retval < 0)
136                         return retval;
137                 frame_brightness = gspca_dev->usb_buf[0];
138                 retval = jl2005c_read_reg(gspca_dev, 0x7d);
139                 if (retval < 0)
140                         return retval;
141                 i++;
142         }
143         gspca_dbg(gspca_dev, D_FRAM, "frame_brightness is 0x%02x\n",
144                   gspca_dev->usb_buf[0]);
145         return retval;
146 }
147
148 static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
149                                                     unsigned char value)
150 {
151         int retval;
152         u8 instruction[2];
153
154         instruction[0] = reg;
155         instruction[1] = value;
156
157         retval = jl2005c_write2(gspca_dev, instruction);
158         if (retval < 0)
159                         return retval;
160
161         return retval;
162 }
163
164 static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
165 {
166         struct sd *sd = (struct sd *)gspca_dev;
167         int i = 0;
168         int retval;
169         unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
170
171         gspca_dbg(gspca_dev, D_PROBE, "Running jl2005c_get_firmware_id\n");
172         /* Read the first ID byte once for warmup */
173         retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
174         gspca_dbg(gspca_dev, D_PROBE, "response is %02x\n",
175                   gspca_dev->usb_buf[0]);
176         if (retval < 0)
177                 return retval;
178         /* Now actually get the ID string */
179         for (i = 0; i < 6; i++) {
180                 retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
181                 if (retval < 0)
182                         return retval;
183                 sd->firmware_id[i] = gspca_dev->usb_buf[0];
184         }
185         gspca_dbg(gspca_dev, D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x\n",
186                   sd->firmware_id[0],
187                   sd->firmware_id[1],
188                   sd->firmware_id[2],
189                   sd->firmware_id[3],
190                   sd->firmware_id[4],
191                   sd->firmware_id[5]);
192         return 0;
193 }
194
195 static int jl2005c_stream_start_vga_lg
196                     (struct gspca_dev *gspca_dev)
197 {
198         int i;
199         int retval = -1;
200         static u8 instruction[][2] = {
201                 {0x05, 0x00},
202                 {0x7c, 0x00},
203                 {0x7d, 0x18},
204                 {0x02, 0x00},
205                 {0x01, 0x00},
206                 {0x04, 0x52},
207         };
208
209         for (i = 0; i < ARRAY_SIZE(instruction); i++) {
210                 msleep(60);
211                 retval = jl2005c_write2(gspca_dev, instruction[i]);
212                 if (retval < 0)
213                         return retval;
214         }
215         msleep(60);
216         return retval;
217 }
218
219 static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
220 {
221         int i;
222         int retval = -1;
223         static u8 instruction[][2] = {
224                 {0x06, 0x00},
225                 {0x7c, 0x00},
226                 {0x7d, 0x1a},
227                 {0x02, 0x00},
228                 {0x01, 0x00},
229                 {0x04, 0x52},
230         };
231
232         for (i = 0; i < ARRAY_SIZE(instruction); i++) {
233                 msleep(60);
234                 retval = jl2005c_write2(gspca_dev, instruction[i]);
235                 if (retval < 0)
236                         return retval;
237         }
238         msleep(60);
239         return retval;
240 }
241
242 static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
243 {
244         int i;
245         int retval = -1;
246         static u8 instruction[][2] = {
247                 {0x05, 0x00},
248                 {0x7c, 0x00},
249                 {0x7d, 0x30},
250                 {0x02, 0x00},
251                 {0x01, 0x00},
252                 {0x04, 0x42},
253         };
254
255         for (i = 0; i < ARRAY_SIZE(instruction); i++) {
256                 msleep(60);
257                 retval = jl2005c_write2(gspca_dev, instruction[i]);
258                 if (retval < 0)
259                         return retval;
260         }
261         msleep(60);
262         return retval;
263 }
264
265 static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
266 {
267         int i;
268         int retval = -1;
269         static u8 instruction[][2] = {
270                 {0x06, 0x00},
271                 {0x7c, 0x00},
272                 {0x7d, 0x32},
273                 {0x02, 0x00},
274                 {0x01, 0x00},
275                 {0x04, 0x42},
276         };
277
278         for (i = 0; i < ARRAY_SIZE(instruction); i++) {
279                 msleep(60);
280                 retval = jl2005c_write2(gspca_dev, instruction[i]);
281                 if (retval < 0)
282                         return retval;
283         }
284         msleep(60);
285         return retval;
286 }
287
288
289 static int jl2005c_stop(struct gspca_dev *gspca_dev)
290 {
291         return jl2005c_write_reg(gspca_dev, 0x07, 0x00);
292 }
293
294 /*
295  * This function is called as a workqueue function and runs whenever the camera
296  * is streaming data. Because it is a workqueue function it is allowed to sleep
297  * so we can use synchronous USB calls. To avoid possible collisions with other
298  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
299  * performing USB operations using it. In practice we don't really need this
300  * as the camera doesn't provide any controls.
301  */
302 static void jl2005c_dostream(struct work_struct *work)
303 {
304         struct sd *dev = container_of(work, struct sd, work_struct);
305         struct gspca_dev *gspca_dev = &dev->gspca_dev;
306         int bytes_left = 0; /* bytes remaining in current frame. */
307         int data_len;   /* size to use for the next read. */
308         int header_read = 0;
309         unsigned char header_sig[2] = {0x4a, 0x4c};
310         int act_len;
311         int packet_type;
312         int ret;
313         u8 *buffer;
314
315         buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL);
316         if (!buffer) {
317                 pr_err("Couldn't allocate USB buffer\n");
318                 goto quit_stream;
319         }
320
321         while (gspca_dev->present && gspca_dev->streaming) {
322 #ifdef CONFIG_PM
323                 if (gspca_dev->frozen)
324                         break;
325 #endif
326                 /* Check if this is a new frame. If so, start the frame first */
327                 if (!header_read) {
328                         mutex_lock(&gspca_dev->usb_lock);
329                         ret = jl2005c_start_new_frame(gspca_dev);
330                         mutex_unlock(&gspca_dev->usb_lock);
331                         if (ret < 0)
332                                 goto quit_stream;
333                         ret = usb_bulk_msg(gspca_dev->dev,
334                                 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
335                                 buffer, JL2005C_MAX_TRANSFER, &act_len,
336                                 JL2005C_DATA_TIMEOUT);
337                         gspca_dbg(gspca_dev, D_PACK,
338                                   "Got %d bytes out of %d for header\n",
339                                   act_len, JL2005C_MAX_TRANSFER);
340                         if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
341                                 goto quit_stream;
342                         /* Check whether we actually got the first blodk */
343                         if (memcmp(header_sig, buffer, 2) != 0) {
344                                 pr_err("First block is not the first block\n");
345                                 goto quit_stream;
346                         }
347                         /* total size to fetch is byte 7, times blocksize
348                          * of which we already got act_len */
349                         bytes_left = buffer[0x07] * dev->block_size - act_len;
350                         gspca_dbg(gspca_dev, D_PACK, "bytes_left = 0x%x\n",
351                                   bytes_left);
352                         /* We keep the header. It has other information, too.*/
353                         packet_type = FIRST_PACKET;
354                         gspca_frame_add(gspca_dev, packet_type,
355                                         buffer, act_len);
356                         header_read = 1;
357                 }
358                 while (bytes_left > 0 && gspca_dev->present) {
359                         data_len = bytes_left > JL2005C_MAX_TRANSFER ?
360                                 JL2005C_MAX_TRANSFER : bytes_left;
361                         ret = usb_bulk_msg(gspca_dev->dev,
362                                 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
363                                 buffer, data_len, &act_len,
364                                 JL2005C_DATA_TIMEOUT);
365                         if (ret < 0 || act_len < data_len)
366                                 goto quit_stream;
367                         gspca_dbg(gspca_dev, D_PACK,
368                                   "Got %d bytes out of %d for frame\n",
369                                   data_len, bytes_left);
370                         bytes_left -= data_len;
371                         if (bytes_left == 0) {
372                                 packet_type = LAST_PACKET;
373                                 header_read = 0;
374                         } else
375                                 packet_type = INTER_PACKET;
376                         gspca_frame_add(gspca_dev, packet_type,
377                                         buffer, data_len);
378                 }
379         }
380 quit_stream:
381         if (gspca_dev->present) {
382                 mutex_lock(&gspca_dev->usb_lock);
383                 jl2005c_stop(gspca_dev);
384                 mutex_unlock(&gspca_dev->usb_lock);
385         }
386         kfree(buffer);
387 }
388
389
390
391
392 /* This function is called at probe time */
393 static int sd_config(struct gspca_dev *gspca_dev,
394                         const struct usb_device_id *id)
395 {
396         struct cam *cam;
397         struct sd *sd = (struct sd *) gspca_dev;
398
399         cam = &gspca_dev->cam;
400         /* We don't use the buffer gspca allocates so make it small. */
401         cam->bulk_size = 64;
402         cam->bulk = 1;
403         /* For the rest, the camera needs to be detected */
404         jl2005c_get_firmware_id(gspca_dev);
405         /* Here are some known firmware IDs
406          * First some JL2005B cameras
407          * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
408          * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
409          * JL2005C cameras
410          * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
411          * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
412          * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
413          *
414          * Based upon this scanty evidence, we can detect a CIF camera by
415          * testing byte 0 for 0x4x.
416          */
417         if ((sd->firmware_id[0] & 0xf0) == 0x40) {
418                 cam->cam_mode   = cif_mode;
419                 cam->nmodes     = ARRAY_SIZE(cif_mode);
420                 sd->block_size  = 0x80;
421         } else {
422                 cam->cam_mode   = vga_mode;
423                 cam->nmodes     = ARRAY_SIZE(vga_mode);
424                 sd->block_size  = 0x200;
425         }
426
427         INIT_WORK(&sd->work_struct, jl2005c_dostream);
428
429         return 0;
430 }
431
432 /* this function is called at probe and resume time */
433 static int sd_init(struct gspca_dev *gspca_dev)
434 {
435         return 0;
436 }
437
438 static int sd_start(struct gspca_dev *gspca_dev)
439 {
440
441         struct sd *sd = (struct sd *) gspca_dev;
442         sd->cap_mode = gspca_dev->cam.cam_mode;
443
444         switch (gspca_dev->pixfmt.width) {
445         case 640:
446                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at vga resolution\n");
447                 jl2005c_stream_start_vga_lg(gspca_dev);
448                 break;
449         case 320:
450                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qvga resolution\n");
451                 jl2005c_stream_start_vga_small(gspca_dev);
452                 break;
453         case 352:
454                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at cif resolution\n");
455                 jl2005c_stream_start_cif_lg(gspca_dev);
456                 break;
457         case 176:
458                 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qcif resolution\n");
459                 jl2005c_stream_start_cif_small(gspca_dev);
460                 break;
461         default:
462                 pr_err("Unknown resolution specified\n");
463                 return -1;
464         }
465
466         schedule_work(&sd->work_struct);
467
468         return 0;
469 }
470
471 /* called on streamoff with alt==0 and on disconnect */
472 /* the usb_lock is held at entry - restore on exit */
473 static void sd_stop0(struct gspca_dev *gspca_dev)
474 {
475         struct sd *dev = (struct sd *) gspca_dev;
476
477         /* wait for the work queue to terminate */
478         mutex_unlock(&gspca_dev->usb_lock);
479         /* This waits for sq905c_dostream to finish */
480         flush_work(&dev->work_struct);
481         mutex_lock(&gspca_dev->usb_lock);
482 }
483
484
485
486 /* sub-driver description */
487 static const struct sd_desc sd_desc = {
488         .name = MODULE_NAME,
489         .config = sd_config,
490         .init = sd_init,
491         .start = sd_start,
492         .stop0 = sd_stop0,
493 };
494
495 /* -- module initialisation -- */
496 static const struct usb_device_id device_table[] = {
497         {USB_DEVICE(0x0979, 0x0227)},
498         {}
499 };
500 MODULE_DEVICE_TABLE(usb, device_table);
501
502 /* -- device connect -- */
503 static int sd_probe(struct usb_interface *intf,
504                                 const struct usb_device_id *id)
505 {
506         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
507                                 THIS_MODULE);
508 }
509
510 static struct usb_driver sd_driver = {
511         .name = MODULE_NAME,
512         .id_table = device_table,
513         .probe = sd_probe,
514         .disconnect = gspca_disconnect,
515 #ifdef CONFIG_PM
516         .suspend = gspca_suspend,
517         .resume = gspca_resume,
518         .reset_resume = gspca_resume,
519 #endif
520 };
521
522 module_usb_driver(sd_driver);
This page took 0.059494 seconds and 4 git commands to generate.