Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/drivers/media/usb/gspca/jl2005bcd.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us