Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Fujifilm Finepix subdriver
0004  *
0005  * Copyright (C) 2008 Frank Zago
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #define MODULE_NAME "finepix"
0011 
0012 #include "gspca.h"
0013 
0014 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
0015 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
0016 MODULE_LICENSE("GPL");
0017 
0018 /* Default timeout, in ms */
0019 #define FPIX_TIMEOUT 250
0020 
0021 /* Maximum transfer size to use. The windows driver reads by chunks of
0022  * 0x2000 bytes, so do the same. Note: reading more seems to work
0023  * too. */
0024 #define FPIX_MAX_TRANSFER 0x2000
0025 
0026 /* Structure to hold all of our device specific stuff */
0027 struct usb_fpix {
0028     struct gspca_dev gspca_dev; /* !! must be the first item */
0029 
0030     struct work_struct work_struct;
0031 };
0032 
0033 /* Delay after which claim the next frame. If the delay is too small,
0034  * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
0035  * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
0036  * 30ms is bad while 35ms is perfect. */
0037 #define NEXT_FRAME_DELAY 35
0038 
0039 /* These cameras only support 320x200. */
0040 static const struct v4l2_pix_format fpix_mode[1] = {
0041     { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
0042         .bytesperline = 320,
0043         .sizeimage = 320 * 240 * 3 / 8 + 590,
0044         .colorspace = V4L2_COLORSPACE_SRGB,
0045         .priv = 0}
0046 };
0047 
0048 /* send a command to the webcam */
0049 static int command(struct gspca_dev *gspca_dev,
0050         int order)  /* 0: reset, 1: frame request */
0051 {
0052     static u8 order_values[2][12] = {
0053         {0xc6, 0, 0, 0, 0, 0, 0,    0, 0x20, 0, 0, 0},  /* reset */
0054         {0xd3, 0, 0, 0, 0, 0, 0, 0x01,    0, 0, 0, 0},  /* fr req */
0055     };
0056 
0057     memcpy(gspca_dev->usb_buf, order_values[order], 12);
0058     return usb_control_msg(gspca_dev->dev,
0059             usb_sndctrlpipe(gspca_dev->dev, 0),
0060             USB_REQ_GET_STATUS,
0061             USB_DIR_OUT | USB_TYPE_CLASS |
0062             USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
0063             12, FPIX_TIMEOUT);
0064 }
0065 
0066 /*
0067  * This function is called as a workqueue function and runs whenever the camera
0068  * is streaming data. Because it is a workqueue function it is allowed to sleep
0069  * so we can use synchronous USB calls. To avoid possible collisions with other
0070  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
0071  * performing USB operations using it. In practice we don't really need this
0072  * as the camera doesn't provide any controls.
0073  */
0074 static void dostream(struct work_struct *work)
0075 {
0076     struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
0077     struct gspca_dev *gspca_dev = &dev->gspca_dev;
0078     struct urb *urb = gspca_dev->urb[0];
0079     u8 *data = urb->transfer_buffer;
0080     int ret = 0;
0081     int len;
0082 
0083     gspca_dbg(gspca_dev, D_STREAM, "dostream started\n");
0084 
0085     /* loop reading a frame */
0086 again:
0087     while (gspca_dev->present && gspca_dev->streaming) {
0088 #ifdef CONFIG_PM
0089         if (gspca_dev->frozen)
0090             break;
0091 #endif
0092 
0093         /* request a frame */
0094         mutex_lock(&gspca_dev->usb_lock);
0095         ret = command(gspca_dev, 1);
0096         mutex_unlock(&gspca_dev->usb_lock);
0097         if (ret < 0)
0098             break;
0099 #ifdef CONFIG_PM
0100         if (gspca_dev->frozen)
0101             break;
0102 #endif
0103         if (!gspca_dev->present || !gspca_dev->streaming)
0104             break;
0105 
0106         /* the frame comes in parts */
0107         for (;;) {
0108             ret = usb_bulk_msg(gspca_dev->dev,
0109                     urb->pipe,
0110                     data,
0111                     FPIX_MAX_TRANSFER,
0112                     &len, FPIX_TIMEOUT);
0113             if (ret < 0) {
0114                 /* Most of the time we get a timeout
0115                  * error. Just restart. */
0116                 goto again;
0117             }
0118 #ifdef CONFIG_PM
0119             if (gspca_dev->frozen)
0120                 goto out;
0121 #endif
0122             if (!gspca_dev->present || !gspca_dev->streaming)
0123                 goto out;
0124             if (len < FPIX_MAX_TRANSFER ||
0125                 (data[len - 2] == 0xff &&
0126                     data[len - 1] == 0xd9)) {
0127 
0128                 /* If the result is less than what was asked
0129                  * for, then it's the end of the
0130                  * frame. Sometimes the jpeg is not complete,
0131                  * but there's nothing we can do. We also end
0132                  * here if the the jpeg ends right at the end
0133                  * of the frame. */
0134                 gspca_frame_add(gspca_dev, LAST_PACKET,
0135                         data, len);
0136                 break;
0137             }
0138 
0139             /* got a partial image */
0140             gspca_frame_add(gspca_dev,
0141                     gspca_dev->last_packet_type
0142                         == LAST_PACKET
0143                     ? FIRST_PACKET : INTER_PACKET,
0144                     data, len);
0145         }
0146 
0147         /* We must wait before trying reading the next
0148          * frame. If we don't, or if the delay is too short,
0149          * the camera will disconnect. */
0150         msleep(NEXT_FRAME_DELAY);
0151     }
0152 
0153 out:
0154     gspca_dbg(gspca_dev, D_STREAM, "dostream stopped\n");
0155 }
0156 
0157 /* this function is called at probe time */
0158 static int sd_config(struct gspca_dev *gspca_dev,
0159         const struct usb_device_id *id)
0160 {
0161     struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
0162     struct cam *cam = &gspca_dev->cam;
0163 
0164     cam->cam_mode = fpix_mode;
0165     cam->nmodes = 1;
0166     cam->bulk = 1;
0167     cam->bulk_size = FPIX_MAX_TRANSFER;
0168 
0169     INIT_WORK(&dev->work_struct, dostream);
0170 
0171     return 0;
0172 }
0173 
0174 /* this function is called at probe and resume time */
0175 static int sd_init(struct gspca_dev *gspca_dev)
0176 {
0177     return 0;
0178 }
0179 
0180 /* start the camera */
0181 static int sd_start(struct gspca_dev *gspca_dev)
0182 {
0183     struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
0184     int ret, len;
0185 
0186     /* Init the device */
0187     ret = command(gspca_dev, 0);
0188     if (ret < 0) {
0189         pr_err("init failed %d\n", ret);
0190         return ret;
0191     }
0192 
0193     /* Read the result of the command. Ignore the result, for it
0194      * varies with the device. */
0195     ret = usb_bulk_msg(gspca_dev->dev,
0196             gspca_dev->urb[0]->pipe,
0197             gspca_dev->urb[0]->transfer_buffer,
0198             FPIX_MAX_TRANSFER, &len,
0199             FPIX_TIMEOUT);
0200     if (ret < 0) {
0201         pr_err("usb_bulk_msg failed %d\n", ret);
0202         return ret;
0203     }
0204 
0205     /* Request a frame, but don't read it */
0206     ret = command(gspca_dev, 1);
0207     if (ret < 0) {
0208         pr_err("frame request failed %d\n", ret);
0209         return ret;
0210     }
0211 
0212     /* Again, reset bulk in endpoint */
0213     usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
0214 
0215     schedule_work(&dev->work_struct);
0216 
0217     return 0;
0218 }
0219 
0220 /* called on streamoff with alt==0 and on disconnect */
0221 /* the usb_lock is held at entry - restore on exit */
0222 static void sd_stop0(struct gspca_dev *gspca_dev)
0223 {
0224     struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
0225 
0226     /* wait for the work queue to terminate */
0227     mutex_unlock(&gspca_dev->usb_lock);
0228     flush_work(&dev->work_struct);
0229     mutex_lock(&gspca_dev->usb_lock);
0230 }
0231 
0232 /* Table of supported USB devices */
0233 static const struct usb_device_id device_table[] = {
0234     {USB_DEVICE(0x04cb, 0x0104)},
0235     {USB_DEVICE(0x04cb, 0x0109)},
0236     {USB_DEVICE(0x04cb, 0x010b)},
0237     {USB_DEVICE(0x04cb, 0x010f)},
0238     {USB_DEVICE(0x04cb, 0x0111)},
0239     {USB_DEVICE(0x04cb, 0x0113)},
0240     {USB_DEVICE(0x04cb, 0x0115)},
0241     {USB_DEVICE(0x04cb, 0x0117)},
0242     {USB_DEVICE(0x04cb, 0x0119)},
0243     {USB_DEVICE(0x04cb, 0x011b)},
0244     {USB_DEVICE(0x04cb, 0x011d)},
0245     {USB_DEVICE(0x04cb, 0x0121)},
0246     {USB_DEVICE(0x04cb, 0x0123)},
0247     {USB_DEVICE(0x04cb, 0x0125)},
0248     {USB_DEVICE(0x04cb, 0x0127)},
0249     {USB_DEVICE(0x04cb, 0x0129)},
0250     {USB_DEVICE(0x04cb, 0x012b)},
0251     {USB_DEVICE(0x04cb, 0x012d)},
0252     {USB_DEVICE(0x04cb, 0x012f)},
0253     {USB_DEVICE(0x04cb, 0x0131)},
0254     {USB_DEVICE(0x04cb, 0x013b)},
0255     {USB_DEVICE(0x04cb, 0x013d)},
0256     {USB_DEVICE(0x04cb, 0x013f)},
0257     {}
0258 };
0259 
0260 MODULE_DEVICE_TABLE(usb, device_table);
0261 
0262 /* sub-driver description */
0263 static const struct sd_desc sd_desc = {
0264     .name   = MODULE_NAME,
0265     .config = sd_config,
0266     .init   = sd_init,
0267     .start  = sd_start,
0268     .stop0  = sd_stop0,
0269 };
0270 
0271 /* -- device connect -- */
0272 static int sd_probe(struct usb_interface *intf,
0273         const struct usb_device_id *id)
0274 {
0275     return gspca_dev_probe(intf, id,
0276             &sd_desc,
0277             sizeof(struct usb_fpix),
0278             THIS_MODULE);
0279 }
0280 
0281 static struct usb_driver sd_driver = {
0282     .name       = MODULE_NAME,
0283     .id_table   = device_table,
0284     .probe      = sd_probe,
0285     .disconnect = gspca_disconnect,
0286 #ifdef CONFIG_PM
0287     .suspend = gspca_suspend,
0288     .resume  = gspca_resume,
0289     .reset_resume = gspca_resume,
0290 #endif
0291 };
0292 
0293 module_usb_driver(sd_driver);