0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include <linux/module.h>
0027 #include <linux/firmware.h>
0028 #include <linux/kernel.h>
0029 #include <linux/mutex.h>
0030 #include <linux/slab.h>
0031 #include <linux/videodev2.h>
0032 #include <linux/mm.h>
0033 #include <linux/vmalloc.h>
0034 #include <linux/usb.h>
0035 #include <media/videobuf2-v4l2.h>
0036 #include <media/videobuf2-vmalloc.h>
0037 #include <media/v4l2-common.h>
0038 #include <media/v4l2-device.h>
0039 #include <media/v4l2-ioctl.h>
0040 #include <media/v4l2-ctrls.h>
0041 #include <media/v4l2-event.h>
0042
0043 #define S2255_VERSION "1.25.1"
0044 #define FIRMWARE_FILE_NAME "f2255usb.bin"
0045
0046
0047 #define S2255_DEF_JPEG_QUAL 50
0048
0049 #define S2255_VR_IN 0
0050
0051 #define S2255_VR_OUT 1
0052
0053 #define S2255_VR_FW 0x30
0054
0055 #define S2255_CONFIG_EP 2
0056
0057 #define S2255_DSP_BOOTTIME 800
0058
0059 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
0060 #define S2255_MIN_BUFS 2
0061 #define S2255_SETMODE_TIMEOUT 500
0062 #define S2255_VIDSTATUS_TIMEOUT 350
0063 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
0064 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
0065 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
0066 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
0067 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
0068 #define S2255_USB_XFER_SIZE (16 * 1024)
0069 #define MAX_CHANNELS 4
0070 #define SYS_FRAMES 4
0071
0072 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
0073 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
0074 #define LINE_SZ_4CIFS_NTSC 640
0075 #define LINE_SZ_2CIFS_NTSC 640
0076 #define LINE_SZ_1CIFS_NTSC 320
0077 #define LINE_SZ_4CIFS_PAL 704
0078 #define LINE_SZ_2CIFS_PAL 704
0079 #define LINE_SZ_1CIFS_PAL 352
0080 #define NUM_LINES_4CIFS_NTSC 240
0081 #define NUM_LINES_2CIFS_NTSC 240
0082 #define NUM_LINES_1CIFS_NTSC 240
0083 #define NUM_LINES_4CIFS_PAL 288
0084 #define NUM_LINES_2CIFS_PAL 288
0085 #define NUM_LINES_1CIFS_PAL 288
0086 #define LINE_SZ_DEF 640
0087 #define NUM_LINES_DEF 240
0088
0089
0090
0091 #define FORMAT_NTSC 1
0092 #define FORMAT_PAL 2
0093
0094 #define SCALE_4CIFS 1
0095 #define SCALE_2CIFS 2
0096 #define SCALE_1CIFS 3
0097
0098 #define SCALE_4CIFSI 4
0099
0100 #define COLOR_YUVPL 1
0101 #define COLOR_YUVPK 2
0102 #define COLOR_Y8 4
0103 #define COLOR_JPG 5
0104
0105 #define MASK_COLOR 0x000000ff
0106 #define MASK_JPG_QUALITY 0x0000ff00
0107 #define MASK_INPUT_TYPE 0x000f0000
0108
0109 #define FDEC_1 1
0110 #define FDEC_2 2
0111 #define FDEC_3 3
0112 #define FDEC_5 5
0113
0114
0115
0116
0117 #define DEF_SCALE SCALE_4CIFS
0118 #define DEF_COLOR COLOR_YUVPL
0119 #define DEF_FDEC FDEC_1
0120 #define DEF_BRIGHT 0
0121 #define DEF_CONTRAST 0x5c
0122 #define DEF_SATURATION 0x80
0123 #define DEF_HUE 0
0124
0125
0126 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
0127 #define CMD_2255 0xc2255000
0128 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
0129 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
0130 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
0131 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
0132
0133 struct s2255_mode {
0134 u32 format;
0135 u32 scale;
0136 u32 color;
0137 u32 fdec;
0138 u32 bright;
0139 u32 contrast;
0140 u32 saturation;
0141 u32 hue;
0142 u32 single;
0143 u32 usb_block;
0144 u32 restart;
0145 };
0146
0147
0148 #define S2255_READ_IDLE 0
0149 #define S2255_READ_FRAME 1
0150
0151
0152 struct s2255_framei {
0153 unsigned long size;
0154 unsigned long ulState;
0155 void *lpvbits;
0156 unsigned long cur_size;
0157 };
0158
0159
0160 struct s2255_bufferi {
0161 unsigned long dwFrames;
0162 struct s2255_framei frame[SYS_FRAMES];
0163 };
0164
0165 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
0166 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
0167 DEF_HUE, 0, DEF_USB_BLOCK, 0}
0168
0169
0170 #define S2255_FW_NOTLOADED 0
0171 #define S2255_FW_LOADED_DSPWAIT 1
0172 #define S2255_FW_SUCCESS 2
0173 #define S2255_FW_FAILED 3
0174 #define S2255_FW_DISCONNECTING 4
0175 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
0176
0177 #define S2255_READ_IDLE 0
0178 #define S2255_READ_FRAME 1
0179 struct s2255_fw {
0180 int fw_loaded;
0181 int fw_size;
0182 struct urb *fw_urb;
0183 atomic_t fw_state;
0184 void *pfw_data;
0185 wait_queue_head_t wait_fw;
0186 const struct firmware *fw;
0187 };
0188
0189 struct s2255_pipeinfo {
0190 u32 max_transfer_size;
0191 u32 cur_transfer_size;
0192 u8 *transfer_buffer;
0193 u32 state;
0194 void *stream_urb;
0195 void *dev;
0196 u32 err_count;
0197 u32 idx;
0198 };
0199
0200 struct s2255_fmt;
0201 struct s2255_dev;
0202
0203
0204 struct s2255_vc {
0205 struct s2255_dev *dev;
0206 struct video_device vdev;
0207 struct v4l2_ctrl_handler hdl;
0208 struct v4l2_ctrl *jpegqual_ctrl;
0209 int resources;
0210 struct list_head buf_list;
0211 struct s2255_bufferi buffer;
0212 struct s2255_mode mode;
0213 v4l2_std_id std;
0214
0215 unsigned jpegqual;
0216
0217 struct v4l2_captureparm cap_parm;
0218 int cur_frame;
0219 int last_frame;
0220
0221 unsigned long req_image_size;
0222
0223 unsigned long pkt_size;
0224 int bad_payload;
0225 unsigned long frame_count;
0226
0227 int jpg_size;
0228
0229 int configured;
0230 wait_queue_head_t wait_setmode;
0231 int setmode_ready;
0232
0233 int vidstatus;
0234 wait_queue_head_t wait_vidstatus;
0235 int vidstatus_ready;
0236 unsigned int width;
0237 unsigned int height;
0238 enum v4l2_field field;
0239 const struct s2255_fmt *fmt;
0240 int idx;
0241 struct vb2_queue vb_vidq;
0242 struct mutex vb_lock;
0243 spinlock_t qlock;
0244 };
0245
0246
0247 struct s2255_dev {
0248 struct s2255_vc vc[MAX_CHANNELS];
0249 struct v4l2_device v4l2_dev;
0250 atomic_t num_channels;
0251 int frames;
0252 struct mutex lock;
0253 struct mutex cmdlock;
0254 struct usb_device *udev;
0255 struct usb_interface *interface;
0256 u8 read_endpoint;
0257 struct timer_list timer;
0258 struct s2255_fw *fw_data;
0259 struct s2255_pipeinfo pipe;
0260 u32 cc;
0261 int frame_ready;
0262 int chn_ready;
0263
0264 int dsp_fw_ver;
0265 u16 pid;
0266 #define S2255_CMDBUF_SIZE 512
0267 __le32 *cmdbuf;
0268 };
0269
0270 static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
0271 {
0272 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
0273 }
0274
0275 struct s2255_fmt {
0276 u32 fourcc;
0277 int depth;
0278 };
0279
0280
0281 struct s2255_buffer {
0282
0283 struct vb2_v4l2_buffer vb;
0284 struct list_head list;
0285 };
0286
0287
0288
0289 #define S2255_CUR_USB_FWVER ((3 << 8) | 12)
0290
0291 #define S2255_CUR_DSP_FWVER 10104
0292
0293 #define S2255_MIN_DSP_STATUS 5
0294 #define S2255_MIN_DSP_COLORFILTER 8
0295 #define S2255_NORMS (V4L2_STD_ALL)
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 #define S2255_V4L2_YC_ON 1
0324 #define S2255_V4L2_YC_OFF 0
0325 #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0)
0326
0327
0328 #define PREFIX_SIZE 512
0329
0330
0331 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
0332
0333 static int debug;
0334
0335 static int s2255_start_readpipe(struct s2255_dev *dev);
0336 static void s2255_stop_readpipe(struct s2255_dev *dev);
0337 static int s2255_start_acquire(struct s2255_vc *vc);
0338 static int s2255_stop_acquire(struct s2255_vc *vc);
0339 static void s2255_fillbuff(struct s2255_vc *vc, struct s2255_buffer *buf,
0340 int jpgsize);
0341 static int s2255_set_mode(struct s2255_vc *vc, struct s2255_mode *mode);
0342 static int s2255_board_shutdown(struct s2255_dev *dev);
0343 static void s2255_fwload_start(struct s2255_dev *dev);
0344 static void s2255_destroy(struct s2255_dev *dev);
0345 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
0346 u16 index, u16 value, void *buf,
0347 s32 buf_len, int bOut);
0348
0349
0350 #define S2255_DRIVER_NAME "s2255"
0351 #define s2255_dev_err(dev, fmt, arg...) \
0352 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
0353
0354 #define dprintk(dev, level, fmt, arg...) \
0355 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
0356
0357 static struct usb_driver s2255_driver;
0358
0359
0360 static int video_nr = -1;
0361
0362
0363 static int jpeg_enable = 1;
0364
0365 module_param(debug, int, 0644);
0366 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
0367 module_param(video_nr, int, 0644);
0368 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
0369 module_param(jpeg_enable, int, 0644);
0370 MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1");
0371
0372
0373 #define USB_SENSORAY_VID 0x1943
0374 static const struct usb_device_id s2255_table[] = {
0375 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
0376 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)},
0377 { }
0378 };
0379 MODULE_DEVICE_TABLE(usb, s2255_table);
0380
0381 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
0382
0383
0384
0385 static const struct s2255_fmt formats[] = {
0386 {
0387 .fourcc = V4L2_PIX_FMT_YUYV,
0388 .depth = 16
0389
0390 }, {
0391 .fourcc = V4L2_PIX_FMT_UYVY,
0392 .depth = 16
0393 }, {
0394 .fourcc = V4L2_PIX_FMT_YUV422P,
0395 .depth = 16
0396
0397 }, {
0398 .fourcc = V4L2_PIX_FMT_GREY,
0399 .depth = 8
0400 }, {
0401 .fourcc = V4L2_PIX_FMT_JPEG,
0402 .depth = 24
0403 }, {
0404 .fourcc = V4L2_PIX_FMT_MJPEG,
0405 .depth = 24
0406 }
0407 };
0408
0409 static int norm_maxw(struct s2255_vc *vc)
0410 {
0411 return (vc->std & V4L2_STD_525_60) ?
0412 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
0413 }
0414
0415 static int norm_maxh(struct s2255_vc *vc)
0416 {
0417 return (vc->std & V4L2_STD_525_60) ?
0418 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
0419 }
0420
0421 static int norm_minw(struct s2255_vc *vc)
0422 {
0423 return (vc->std & V4L2_STD_525_60) ?
0424 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
0425 }
0426
0427 static int norm_minh(struct s2255_vc *vc)
0428 {
0429 return (vc->std & V4L2_STD_525_60) ?
0430 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
0431 }
0432
0433
0434
0435
0436
0437
0438 static void planar422p_to_yuv_packed(const unsigned char *in,
0439 unsigned char *out,
0440 int width, int height,
0441 int fmt)
0442 {
0443 unsigned char *pY;
0444 unsigned char *pCb;
0445 unsigned char *pCr;
0446 unsigned long size = height * width;
0447 unsigned int i;
0448 pY = (unsigned char *)in;
0449 pCr = (unsigned char *)in + height * width;
0450 pCb = (unsigned char *)in + height * width + (height * width / 2);
0451 for (i = 0; i < size * 2; i += 4) {
0452 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
0453 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
0454 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
0455 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
0456 }
0457 return;
0458 }
0459
0460 static void s2255_reset_dsppower(struct s2255_dev *dev)
0461 {
0462 s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1);
0463 msleep(50);
0464 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
0465 msleep(600);
0466 s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1);
0467 return;
0468 }
0469
0470
0471
0472 static void s2255_timer(struct timer_list *t)
0473 {
0474 struct s2255_dev *dev = from_timer(dev, t, timer);
0475 struct s2255_fw *data = dev->fw_data;
0476 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
0477 pr_err("s2255: can't submit urb\n");
0478 atomic_set(&data->fw_state, S2255_FW_FAILED);
0479
0480 wake_up(&data->wait_fw);
0481 return;
0482 }
0483 }
0484
0485
0486
0487
0488
0489
0490
0491
0492 static void s2255_fwchunk_complete(struct urb *urb)
0493 {
0494 struct s2255_fw *data = urb->context;
0495 struct usb_device *udev = urb->dev;
0496 int len;
0497 if (urb->status) {
0498 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
0499 atomic_set(&data->fw_state, S2255_FW_FAILED);
0500
0501 wake_up(&data->wait_fw);
0502 return;
0503 }
0504 if (data->fw_urb == NULL) {
0505 s2255_dev_err(&udev->dev, "disconnected\n");
0506 atomic_set(&data->fw_state, S2255_FW_FAILED);
0507
0508 wake_up(&data->wait_fw);
0509 return;
0510 }
0511 #define CHUNK_SIZE 512
0512
0513
0514
0515
0516 if (data->fw_loaded < data->fw_size) {
0517 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
0518 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
0519
0520 if (len < CHUNK_SIZE)
0521 memset(data->pfw_data, 0, CHUNK_SIZE);
0522
0523 memcpy(data->pfw_data,
0524 (char *) data->fw->data + data->fw_loaded, len);
0525
0526 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
0527 data->pfw_data, CHUNK_SIZE,
0528 s2255_fwchunk_complete, data);
0529 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
0530 dev_err(&udev->dev, "failed submit URB\n");
0531 atomic_set(&data->fw_state, S2255_FW_FAILED);
0532
0533 wake_up(&data->wait_fw);
0534 return;
0535 }
0536 data->fw_loaded += len;
0537 } else
0538 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
0539 return;
0540
0541 }
0542
0543 static void s2255_got_frame(struct s2255_vc *vc, int jpgsize)
0544 {
0545 struct s2255_buffer *buf;
0546 struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
0547 unsigned long flags = 0;
0548
0549 spin_lock_irqsave(&vc->qlock, flags);
0550 if (list_empty(&vc->buf_list)) {
0551 dprintk(dev, 1, "No active queue to serve\n");
0552 spin_unlock_irqrestore(&vc->qlock, flags);
0553 return;
0554 }
0555 buf = list_entry(vc->buf_list.next,
0556 struct s2255_buffer, list);
0557 list_del(&buf->list);
0558 buf->vb.vb2_buf.timestamp = ktime_get_ns();
0559 buf->vb.field = vc->field;
0560 buf->vb.sequence = vc->frame_count;
0561 spin_unlock_irqrestore(&vc->qlock, flags);
0562
0563 s2255_fillbuff(vc, buf, jpgsize);
0564
0565 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
0566 dprintk(dev, 2, "%s: [buf] [%p]\n", __func__, buf);
0567 }
0568
0569 static const struct s2255_fmt *format_by_fourcc(int fourcc)
0570 {
0571 unsigned int i;
0572 for (i = 0; i < ARRAY_SIZE(formats); i++) {
0573 if (-1 == formats[i].fourcc)
0574 continue;
0575 if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) ||
0576 (formats[i].fourcc == V4L2_PIX_FMT_MJPEG)))
0577 continue;
0578 if (formats[i].fourcc == fourcc)
0579 return formats + i;
0580 }
0581 return NULL;
0582 }
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592 static void s2255_fillbuff(struct s2255_vc *vc,
0593 struct s2255_buffer *buf, int jpgsize)
0594 {
0595 int pos = 0;
0596 const char *tmpbuf;
0597 char *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
0598 unsigned long last_frame;
0599 struct s2255_dev *dev = vc->dev;
0600
0601 if (!vbuf)
0602 return;
0603 last_frame = vc->last_frame;
0604 if (last_frame != -1) {
0605 tmpbuf =
0606 (const char *)vc->buffer.frame[last_frame].lpvbits;
0607 switch (vc->fmt->fourcc) {
0608 case V4L2_PIX_FMT_YUYV:
0609 case V4L2_PIX_FMT_UYVY:
0610 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
0611 vbuf, vc->width,
0612 vc->height,
0613 vc->fmt->fourcc);
0614 break;
0615 case V4L2_PIX_FMT_GREY:
0616 memcpy(vbuf, tmpbuf, vc->width * vc->height);
0617 break;
0618 case V4L2_PIX_FMT_JPEG:
0619 case V4L2_PIX_FMT_MJPEG:
0620 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, jpgsize);
0621 memcpy(vbuf, tmpbuf, jpgsize);
0622 break;
0623 case V4L2_PIX_FMT_YUV422P:
0624 memcpy(vbuf, tmpbuf,
0625 vc->width * vc->height * 2);
0626 break;
0627 default:
0628 pr_info("s2255: unknown format?\n");
0629 }
0630 vc->last_frame = -1;
0631 } else {
0632 pr_err("s2255: =======no frame\n");
0633 return;
0634 }
0635 dprintk(dev, 2, "s2255fill at : Buffer %p size= %d\n",
0636 vbuf, pos);
0637 }
0638
0639
0640
0641
0642
0643
0644 static int queue_setup(struct vb2_queue *vq,
0645 unsigned int *nbuffers, unsigned int *nplanes,
0646 unsigned int sizes[], struct device *alloc_devs[])
0647 {
0648 struct s2255_vc *vc = vb2_get_drv_priv(vq);
0649 if (*nbuffers < S2255_MIN_BUFS)
0650 *nbuffers = S2255_MIN_BUFS;
0651 *nplanes = 1;
0652 sizes[0] = vc->width * vc->height * (vc->fmt->depth >> 3);
0653 return 0;
0654 }
0655
0656 static int buffer_prepare(struct vb2_buffer *vb)
0657 {
0658 struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
0659 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0660 struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
0661 int w = vc->width;
0662 int h = vc->height;
0663 unsigned long size;
0664
0665 dprintk(vc->dev, 4, "%s\n", __func__);
0666 if (vc->fmt == NULL)
0667 return -EINVAL;
0668
0669 if ((w < norm_minw(vc)) ||
0670 (w > norm_maxw(vc)) ||
0671 (h < norm_minh(vc)) ||
0672 (h > norm_maxh(vc))) {
0673 dprintk(vc->dev, 4, "invalid buffer prepare\n");
0674 return -EINVAL;
0675 }
0676 size = w * h * (vc->fmt->depth >> 3);
0677 if (vb2_plane_size(vb, 0) < size) {
0678 dprintk(vc->dev, 4, "invalid buffer prepare\n");
0679 return -EINVAL;
0680 }
0681
0682 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
0683 return 0;
0684 }
0685
0686 static void buffer_queue(struct vb2_buffer *vb)
0687 {
0688 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0689 struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
0690 struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
0691 unsigned long flags = 0;
0692 dprintk(vc->dev, 1, "%s\n", __func__);
0693 spin_lock_irqsave(&vc->qlock, flags);
0694 list_add_tail(&buf->list, &vc->buf_list);
0695 spin_unlock_irqrestore(&vc->qlock, flags);
0696 }
0697
0698 static int start_streaming(struct vb2_queue *vq, unsigned int count);
0699 static void stop_streaming(struct vb2_queue *vq);
0700
0701 static const struct vb2_ops s2255_video_qops = {
0702 .queue_setup = queue_setup,
0703 .buf_prepare = buffer_prepare,
0704 .buf_queue = buffer_queue,
0705 .start_streaming = start_streaming,
0706 .stop_streaming = stop_streaming,
0707 .wait_prepare = vb2_ops_wait_prepare,
0708 .wait_finish = vb2_ops_wait_finish,
0709 };
0710
0711 static int vidioc_querycap(struct file *file, void *priv,
0712 struct v4l2_capability *cap)
0713 {
0714 struct s2255_vc *vc = video_drvdata(file);
0715 struct s2255_dev *dev = vc->dev;
0716
0717 strscpy(cap->driver, "s2255", sizeof(cap->driver));
0718 strscpy(cap->card, "s2255", sizeof(cap->card));
0719 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0720 return 0;
0721 }
0722
0723 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
0724 struct v4l2_fmtdesc *f)
0725 {
0726 int index = f->index;
0727
0728 if (index >= ARRAY_SIZE(formats))
0729 return -EINVAL;
0730 if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) ||
0731 (formats[index].fourcc == V4L2_PIX_FMT_MJPEG)))
0732 return -EINVAL;
0733 f->pixelformat = formats[index].fourcc;
0734 return 0;
0735 }
0736
0737 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
0738 struct v4l2_format *f)
0739 {
0740 struct s2255_vc *vc = video_drvdata(file);
0741 int is_ntsc = vc->std & V4L2_STD_525_60;
0742
0743 f->fmt.pix.width = vc->width;
0744 f->fmt.pix.height = vc->height;
0745 if (f->fmt.pix.height >=
0746 (is_ntsc ? NUM_LINES_1CIFS_NTSC : NUM_LINES_1CIFS_PAL) * 2)
0747 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
0748 else
0749 f->fmt.pix.field = V4L2_FIELD_TOP;
0750 f->fmt.pix.pixelformat = vc->fmt->fourcc;
0751 f->fmt.pix.bytesperline = f->fmt.pix.width * (vc->fmt->depth >> 3);
0752 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
0753 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
0754 return 0;
0755 }
0756
0757 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
0758 struct v4l2_format *f)
0759 {
0760 const struct s2255_fmt *fmt;
0761 enum v4l2_field field;
0762 struct s2255_vc *vc = video_drvdata(file);
0763 int is_ntsc = vc->std & V4L2_STD_525_60;
0764
0765 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
0766
0767 if (fmt == NULL)
0768 return -EINVAL;
0769
0770 dprintk(vc->dev, 50, "%s NTSC: %d suggested width: %d, height: %d\n",
0771 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
0772 if (is_ntsc) {
0773
0774 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
0775 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
0776 field = V4L2_FIELD_INTERLACED;
0777 } else {
0778 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
0779 field = V4L2_FIELD_TOP;
0780 }
0781 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
0782 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
0783 else
0784 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
0785 } else {
0786
0787 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
0788 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
0789 field = V4L2_FIELD_INTERLACED;
0790 } else {
0791 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
0792 field = V4L2_FIELD_TOP;
0793 }
0794 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL)
0795 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
0796 else
0797 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
0798 }
0799 f->fmt.pix.field = field;
0800 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
0801 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
0802 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
0803 dprintk(vc->dev, 50, "%s: set width %d height %d field %d\n", __func__,
0804 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
0805 return 0;
0806 }
0807
0808 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
0809 struct v4l2_format *f)
0810 {
0811 struct s2255_vc *vc = video_drvdata(file);
0812 const struct s2255_fmt *fmt;
0813 struct vb2_queue *q = &vc->vb_vidq;
0814 struct s2255_mode mode;
0815 int ret;
0816
0817 ret = vidioc_try_fmt_vid_cap(file, vc, f);
0818
0819 if (ret < 0)
0820 return ret;
0821
0822 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
0823
0824 if (fmt == NULL)
0825 return -EINVAL;
0826
0827 if (vb2_is_busy(q)) {
0828 dprintk(vc->dev, 1, "queue busy\n");
0829 return -EBUSY;
0830 }
0831
0832 mode = vc->mode;
0833 vc->fmt = fmt;
0834 vc->width = f->fmt.pix.width;
0835 vc->height = f->fmt.pix.height;
0836 vc->field = f->fmt.pix.field;
0837 if (vc->width > norm_minw(vc)) {
0838 if (vc->height > norm_minh(vc)) {
0839 if (vc->cap_parm.capturemode &
0840 V4L2_MODE_HIGHQUALITY)
0841 mode.scale = SCALE_4CIFSI;
0842 else
0843 mode.scale = SCALE_4CIFS;
0844 } else
0845 mode.scale = SCALE_2CIFS;
0846
0847 } else {
0848 mode.scale = SCALE_1CIFS;
0849 }
0850
0851 switch (vc->fmt->fourcc) {
0852 case V4L2_PIX_FMT_GREY:
0853 mode.color &= ~MASK_COLOR;
0854 mode.color |= COLOR_Y8;
0855 break;
0856 case V4L2_PIX_FMT_JPEG:
0857 case V4L2_PIX_FMT_MJPEG:
0858 mode.color &= ~MASK_COLOR;
0859 mode.color |= COLOR_JPG;
0860 mode.color |= (vc->jpegqual << 8);
0861 break;
0862 case V4L2_PIX_FMT_YUV422P:
0863 mode.color &= ~MASK_COLOR;
0864 mode.color |= COLOR_YUVPL;
0865 break;
0866 case V4L2_PIX_FMT_YUYV:
0867 case V4L2_PIX_FMT_UYVY:
0868 default:
0869 mode.color &= ~MASK_COLOR;
0870 mode.color |= COLOR_YUVPK;
0871 break;
0872 }
0873 if ((mode.color & MASK_COLOR) != (vc->mode.color & MASK_COLOR))
0874 mode.restart = 1;
0875 else if (mode.scale != vc->mode.scale)
0876 mode.restart = 1;
0877 else if (mode.format != vc->mode.format)
0878 mode.restart = 1;
0879 vc->mode = mode;
0880 (void) s2255_set_mode(vc, &mode);
0881 return 0;
0882 }
0883
0884
0885
0886 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
0887 int size)
0888 {
0889 int pipe;
0890 int done;
0891 long retval = -1;
0892 if (udev) {
0893 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
0894 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
0895 }
0896 return retval;
0897 }
0898
0899 static u32 get_transfer_size(struct s2255_mode *mode)
0900 {
0901 int linesPerFrame = LINE_SZ_DEF;
0902 int pixelsPerLine = NUM_LINES_DEF;
0903 u32 outImageSize;
0904 u32 usbInSize;
0905 unsigned int mask_mult;
0906
0907 if (mode == NULL)
0908 return 0;
0909
0910 if (mode->format == FORMAT_NTSC) {
0911 switch (mode->scale) {
0912 case SCALE_4CIFS:
0913 case SCALE_4CIFSI:
0914 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
0915 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
0916 break;
0917 case SCALE_2CIFS:
0918 linesPerFrame = NUM_LINES_2CIFS_NTSC;
0919 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
0920 break;
0921 case SCALE_1CIFS:
0922 linesPerFrame = NUM_LINES_1CIFS_NTSC;
0923 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
0924 break;
0925 default:
0926 break;
0927 }
0928 } else if (mode->format == FORMAT_PAL) {
0929 switch (mode->scale) {
0930 case SCALE_4CIFS:
0931 case SCALE_4CIFSI:
0932 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
0933 pixelsPerLine = LINE_SZ_4CIFS_PAL;
0934 break;
0935 case SCALE_2CIFS:
0936 linesPerFrame = NUM_LINES_2CIFS_PAL;
0937 pixelsPerLine = LINE_SZ_2CIFS_PAL;
0938 break;
0939 case SCALE_1CIFS:
0940 linesPerFrame = NUM_LINES_1CIFS_PAL;
0941 pixelsPerLine = LINE_SZ_1CIFS_PAL;
0942 break;
0943 default:
0944 break;
0945 }
0946 }
0947 outImageSize = linesPerFrame * pixelsPerLine;
0948 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
0949
0950 outImageSize *= 2;
0951 }
0952
0953
0954
0955 usbInSize = outImageSize + PREFIX_SIZE;
0956 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
0957
0958 if (usbInSize & ~mask_mult)
0959 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
0960 return usbInSize;
0961 }
0962
0963 static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
0964 {
0965 struct device *dev = &sdev->udev->dev;
0966 dev_info(dev, "------------------------------------------------\n");
0967 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
0968 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
0969 dev_info(dev, "bright: 0x%x\n", mode->bright);
0970 dev_info(dev, "------------------------------------------------\n");
0971 }
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981 static int s2255_set_mode(struct s2255_vc *vc,
0982 struct s2255_mode *mode)
0983 {
0984 int res;
0985 unsigned long chn_rev;
0986 struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
0987 int i;
0988 __le32 *buffer = dev->cmdbuf;
0989
0990 mutex_lock(&dev->cmdlock);
0991 chn_rev = G_chnmap[vc->idx];
0992 dprintk(dev, 3, "%s channel: %d\n", __func__, vc->idx);
0993
0994 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
0995 mode->color &= ~MASK_COLOR;
0996 mode->color |= COLOR_JPG;
0997 mode->color &= ~MASK_JPG_QUALITY;
0998 mode->color |= (vc->jpegqual << 8);
0999 }
1000
1001 vc->mode = *mode;
1002 vc->req_image_size = get_transfer_size(mode);
1003 dprintk(dev, 1, "%s: reqsize %ld\n", __func__, vc->req_image_size);
1004
1005 buffer[0] = IN_DATA_TOKEN;
1006 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1007 buffer[2] = CMD_SET_MODE;
1008 for (i = 0; i < sizeof(struct s2255_mode) / sizeof(u32); i++)
1009 buffer[3 + i] = cpu_to_le32(((u32 *)&vc->mode)[i]);
1010 vc->setmode_ready = 0;
1011 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1012 if (debug)
1013 s2255_print_cfg(dev, mode);
1014
1015 if (mode->restart) {
1016 wait_event_timeout(vc->wait_setmode,
1017 (vc->setmode_ready != 0),
1018 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1019 if (vc->setmode_ready != 1) {
1020 dprintk(dev, 0, "s2255: no set mode response\n");
1021 res = -EFAULT;
1022 }
1023 }
1024
1025 vc->mode.restart = 0;
1026 dprintk(dev, 1, "%s chn %d, result: %d\n", __func__, vc->idx, res);
1027 mutex_unlock(&dev->cmdlock);
1028 return res;
1029 }
1030
1031 static int s2255_cmd_status(struct s2255_vc *vc, u32 *pstatus)
1032 {
1033 int res;
1034 u32 chn_rev;
1035 struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
1036 __le32 *buffer = dev->cmdbuf;
1037
1038 mutex_lock(&dev->cmdlock);
1039 chn_rev = G_chnmap[vc->idx];
1040 dprintk(dev, 4, "%s chan %d\n", __func__, vc->idx);
1041
1042 buffer[0] = IN_DATA_TOKEN;
1043 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1044 buffer[2] = CMD_STATUS;
1045 *pstatus = 0;
1046 vc->vidstatus_ready = 0;
1047 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1048 wait_event_timeout(vc->wait_vidstatus,
1049 (vc->vidstatus_ready != 0),
1050 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1051 if (vc->vidstatus_ready != 1) {
1052 dprintk(dev, 0, "s2255: no vidstatus response\n");
1053 res = -EFAULT;
1054 }
1055 *pstatus = vc->vidstatus;
1056 dprintk(dev, 4, "%s, vid status %d\n", __func__, *pstatus);
1057 mutex_unlock(&dev->cmdlock);
1058 return res;
1059 }
1060
1061 static int start_streaming(struct vb2_queue *vq, unsigned int count)
1062 {
1063 struct s2255_vc *vc = vb2_get_drv_priv(vq);
1064 int j;
1065
1066 vc->last_frame = -1;
1067 vc->bad_payload = 0;
1068 vc->cur_frame = 0;
1069 vc->frame_count = 0;
1070 for (j = 0; j < SYS_FRAMES; j++) {
1071 vc->buffer.frame[j].ulState = S2255_READ_IDLE;
1072 vc->buffer.frame[j].cur_size = 0;
1073 }
1074 return s2255_start_acquire(vc);
1075 }
1076
1077
1078 static void stop_streaming(struct vb2_queue *vq)
1079 {
1080 struct s2255_vc *vc = vb2_get_drv_priv(vq);
1081 struct s2255_buffer *buf, *node;
1082 unsigned long flags;
1083 (void) s2255_stop_acquire(vc);
1084 spin_lock_irqsave(&vc->qlock, flags);
1085 list_for_each_entry_safe(buf, node, &vc->buf_list, list) {
1086 list_del(&buf->list);
1087 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1088 dprintk(vc->dev, 2, "[%p/%d] done\n",
1089 buf, buf->vb.vb2_buf.index);
1090 }
1091 spin_unlock_irqrestore(&vc->qlock, flags);
1092 }
1093
1094 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id i)
1095 {
1096 struct s2255_vc *vc = video_drvdata(file);
1097 struct s2255_mode mode;
1098 struct vb2_queue *q = &vc->vb_vidq;
1099
1100
1101
1102
1103
1104 if (vb2_is_busy(q))
1105 return -EBUSY;
1106
1107 mode = vc->mode;
1108 if (i & V4L2_STD_525_60) {
1109 dprintk(vc->dev, 4, "%s 60 Hz\n", __func__);
1110
1111 if (mode.format != FORMAT_NTSC) {
1112 mode.restart = 1;
1113 mode.format = FORMAT_NTSC;
1114 mode.fdec = FDEC_1;
1115 vc->width = LINE_SZ_4CIFS_NTSC;
1116 vc->height = NUM_LINES_4CIFS_NTSC * 2;
1117 }
1118 } else if (i & V4L2_STD_625_50) {
1119 dprintk(vc->dev, 4, "%s 50 Hz\n", __func__);
1120 if (mode.format != FORMAT_PAL) {
1121 mode.restart = 1;
1122 mode.format = FORMAT_PAL;
1123 mode.fdec = FDEC_1;
1124 vc->width = LINE_SZ_4CIFS_PAL;
1125 vc->height = NUM_LINES_4CIFS_PAL * 2;
1126 }
1127 } else
1128 return -EINVAL;
1129 vc->std = i;
1130 if (mode.restart)
1131 s2255_set_mode(vc, &mode);
1132 return 0;
1133 }
1134
1135 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *i)
1136 {
1137 struct s2255_vc *vc = video_drvdata(file);
1138
1139 *i = vc->std;
1140 return 0;
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150 static int vidioc_enum_input(struct file *file, void *priv,
1151 struct v4l2_input *inp)
1152 {
1153 struct s2255_vc *vc = video_drvdata(file);
1154 struct s2255_dev *dev = vc->dev;
1155 u32 status = 0;
1156
1157 if (inp->index != 0)
1158 return -EINVAL;
1159 inp->type = V4L2_INPUT_TYPE_CAMERA;
1160 inp->std = S2255_NORMS;
1161 inp->status = 0;
1162 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1163 int rc;
1164 rc = s2255_cmd_status(vc, &status);
1165 dprintk(dev, 4, "s2255_cmd_status rc: %d status %x\n",
1166 rc, status);
1167 if (rc == 0)
1168 inp->status = (status & 0x01) ? 0
1169 : V4L2_IN_ST_NO_SIGNAL;
1170 }
1171 switch (dev->pid) {
1172 case 0x2255:
1173 default:
1174 strscpy(inp->name, "Composite", sizeof(inp->name));
1175 break;
1176 case 0x2257:
1177 strscpy(inp->name, (vc->idx < 2) ? "Composite" : "S-Video",
1178 sizeof(inp->name));
1179 break;
1180 }
1181 return 0;
1182 }
1183
1184 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1185 {
1186 *i = 0;
1187 return 0;
1188 }
1189 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1190 {
1191 if (i > 0)
1192 return -EINVAL;
1193 return 0;
1194 }
1195
1196 static int s2255_s_ctrl(struct v4l2_ctrl *ctrl)
1197 {
1198 struct s2255_vc *vc =
1199 container_of(ctrl->handler, struct s2255_vc, hdl);
1200 struct s2255_mode mode;
1201 mode = vc->mode;
1202
1203 switch (ctrl->id) {
1204 case V4L2_CID_BRIGHTNESS:
1205 mode.bright = ctrl->val;
1206 break;
1207 case V4L2_CID_CONTRAST:
1208 mode.contrast = ctrl->val;
1209 break;
1210 case V4L2_CID_HUE:
1211 mode.hue = ctrl->val;
1212 break;
1213 case V4L2_CID_SATURATION:
1214 mode.saturation = ctrl->val;
1215 break;
1216 case V4L2_CID_S2255_COLORFILTER:
1217 mode.color &= ~MASK_INPUT_TYPE;
1218 mode.color |= !ctrl->val << 16;
1219 break;
1220 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1221 vc->jpegqual = ctrl->val;
1222 return 0;
1223 default:
1224 return -EINVAL;
1225 }
1226 mode.restart = 0;
1227
1228
1229
1230
1231 s2255_set_mode(vc, &mode);
1232 return 0;
1233 }
1234
1235 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1236 struct v4l2_jpegcompression *jc)
1237 {
1238 struct s2255_vc *vc = video_drvdata(file);
1239
1240 memset(jc, 0, sizeof(*jc));
1241 jc->quality = vc->jpegqual;
1242 dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
1243 return 0;
1244 }
1245
1246 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1247 const struct v4l2_jpegcompression *jc)
1248 {
1249 struct s2255_vc *vc = video_drvdata(file);
1250
1251 if (jc->quality < 0 || jc->quality > 100)
1252 return -EINVAL;
1253 v4l2_ctrl_s_ctrl(vc->jpegqual_ctrl, jc->quality);
1254 dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
1255 return 0;
1256 }
1257
1258 static int vidioc_g_parm(struct file *file, void *priv,
1259 struct v4l2_streamparm *sp)
1260 {
1261 __u32 def_num, def_dem;
1262 struct s2255_vc *vc = video_drvdata(file);
1263
1264 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1265 return -EINVAL;
1266 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1267 sp->parm.capture.capturemode = vc->cap_parm.capturemode;
1268 sp->parm.capture.readbuffers = S2255_MIN_BUFS;
1269 def_num = (vc->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1270 def_dem = (vc->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1271 sp->parm.capture.timeperframe.denominator = def_dem;
1272 switch (vc->mode.fdec) {
1273 default:
1274 case FDEC_1:
1275 sp->parm.capture.timeperframe.numerator = def_num;
1276 break;
1277 case FDEC_2:
1278 sp->parm.capture.timeperframe.numerator = def_num * 2;
1279 break;
1280 case FDEC_3:
1281 sp->parm.capture.timeperframe.numerator = def_num * 3;
1282 break;
1283 case FDEC_5:
1284 sp->parm.capture.timeperframe.numerator = def_num * 5;
1285 break;
1286 }
1287 dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d\n",
1288 __func__,
1289 sp->parm.capture.capturemode,
1290 sp->parm.capture.timeperframe.numerator,
1291 sp->parm.capture.timeperframe.denominator);
1292 return 0;
1293 }
1294
1295 static int vidioc_s_parm(struct file *file, void *priv,
1296 struct v4l2_streamparm *sp)
1297 {
1298 struct s2255_vc *vc = video_drvdata(file);
1299 struct s2255_mode mode;
1300 int fdec = FDEC_1;
1301 __u32 def_num, def_dem;
1302 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1303 return -EINVAL;
1304 mode = vc->mode;
1305
1306 if ((vc->cap_parm.capturemode != sp->parm.capture.capturemode)
1307 && vb2_is_streaming(&vc->vb_vidq))
1308 return -EBUSY;
1309 def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
1310 def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
1311 if (def_dem != sp->parm.capture.timeperframe.denominator)
1312 sp->parm.capture.timeperframe.numerator = def_num;
1313 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1314 sp->parm.capture.timeperframe.numerator = def_num;
1315 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1316 sp->parm.capture.timeperframe.numerator = def_num * 2;
1317 fdec = FDEC_2;
1318 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1319 sp->parm.capture.timeperframe.numerator = def_num * 3;
1320 fdec = FDEC_3;
1321 } else {
1322 sp->parm.capture.timeperframe.numerator = def_num * 5;
1323 fdec = FDEC_5;
1324 }
1325 mode.fdec = fdec;
1326 sp->parm.capture.timeperframe.denominator = def_dem;
1327 sp->parm.capture.readbuffers = S2255_MIN_BUFS;
1328 s2255_set_mode(vc, &mode);
1329 dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1330 __func__,
1331 sp->parm.capture.capturemode,
1332 sp->parm.capture.timeperframe.numerator,
1333 sp->parm.capture.timeperframe.denominator, fdec);
1334 return 0;
1335 }
1336
1337 #define NUM_SIZE_ENUMS 3
1338 static const struct v4l2_frmsize_discrete ntsc_sizes[] = {
1339 { 640, 480 },
1340 { 640, 240 },
1341 { 320, 240 },
1342 };
1343 static const struct v4l2_frmsize_discrete pal_sizes[] = {
1344 { 704, 576 },
1345 { 704, 288 },
1346 { 352, 288 },
1347 };
1348
1349 static int vidioc_enum_framesizes(struct file *file, void *priv,
1350 struct v4l2_frmsizeenum *fe)
1351 {
1352 struct s2255_vc *vc = video_drvdata(file);
1353 int is_ntsc = vc->std & V4L2_STD_525_60;
1354 const struct s2255_fmt *fmt;
1355
1356 if (fe->index >= NUM_SIZE_ENUMS)
1357 return -EINVAL;
1358
1359 fmt = format_by_fourcc(fe->pixel_format);
1360 if (fmt == NULL)
1361 return -EINVAL;
1362 fe->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1363 fe->discrete = is_ntsc ? ntsc_sizes[fe->index] : pal_sizes[fe->index];
1364 return 0;
1365 }
1366
1367 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1368 struct v4l2_frmivalenum *fe)
1369 {
1370 struct s2255_vc *vc = video_drvdata(file);
1371 const struct s2255_fmt *fmt;
1372 const struct v4l2_frmsize_discrete *sizes;
1373 int is_ntsc = vc->std & V4L2_STD_525_60;
1374 #define NUM_FRAME_ENUMS 4
1375 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1376 int i;
1377
1378 if (fe->index >= NUM_FRAME_ENUMS)
1379 return -EINVAL;
1380
1381 fmt = format_by_fourcc(fe->pixel_format);
1382 if (fmt == NULL)
1383 return -EINVAL;
1384
1385 sizes = is_ntsc ? ntsc_sizes : pal_sizes;
1386 for (i = 0; i < NUM_SIZE_ENUMS; i++, sizes++)
1387 if (fe->width == sizes->width &&
1388 fe->height == sizes->height)
1389 break;
1390 if (i == NUM_SIZE_ENUMS)
1391 return -EINVAL;
1392
1393 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1394 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1395 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1396 dprintk(vc->dev, 4, "%s discrete %d/%d\n", __func__,
1397 fe->discrete.numerator,
1398 fe->discrete.denominator);
1399 return 0;
1400 }
1401
1402 static int s2255_open(struct file *file)
1403 {
1404 struct s2255_vc *vc = video_drvdata(file);
1405 struct s2255_dev *dev = vc->dev;
1406 int state;
1407 int rc = 0;
1408
1409 rc = v4l2_fh_open(file);
1410 if (rc != 0)
1411 return rc;
1412
1413 dprintk(dev, 1, "s2255: %s\n", __func__);
1414 state = atomic_read(&dev->fw_data->fw_state);
1415 switch (state) {
1416 case S2255_FW_DISCONNECTING:
1417 return -ENODEV;
1418 case S2255_FW_FAILED:
1419 s2255_dev_err(&dev->udev->dev,
1420 "firmware load failed. retrying.\n");
1421 s2255_fwload_start(dev);
1422 wait_event_timeout(dev->fw_data->wait_fw,
1423 ((atomic_read(&dev->fw_data->fw_state)
1424 == S2255_FW_SUCCESS) ||
1425 (atomic_read(&dev->fw_data->fw_state)
1426 == S2255_FW_DISCONNECTING)),
1427 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1428
1429 state = atomic_read(&dev->fw_data->fw_state);
1430 break;
1431 case S2255_FW_NOTLOADED:
1432 case S2255_FW_LOADED_DSPWAIT:
1433
1434
1435 pr_info("%s waiting for firmware load\n", __func__);
1436 wait_event_timeout(dev->fw_data->wait_fw,
1437 ((atomic_read(&dev->fw_data->fw_state)
1438 == S2255_FW_SUCCESS) ||
1439 (atomic_read(&dev->fw_data->fw_state)
1440 == S2255_FW_DISCONNECTING)),
1441 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1442
1443 state = atomic_read(&dev->fw_data->fw_state);
1444 break;
1445 case S2255_FW_SUCCESS:
1446 default:
1447 break;
1448 }
1449
1450 switch (state) {
1451 case S2255_FW_SUCCESS:
1452 break;
1453 case S2255_FW_FAILED:
1454 pr_info("2255 firmware load failed.\n");
1455 return -ENODEV;
1456 case S2255_FW_DISCONNECTING:
1457 pr_info("%s: disconnecting\n", __func__);
1458 return -ENODEV;
1459 case S2255_FW_LOADED_DSPWAIT:
1460 case S2255_FW_NOTLOADED:
1461 pr_info("%s: firmware not loaded, please retry\n",
1462 __func__);
1463
1464
1465
1466
1467
1468 atomic_set(&dev->fw_data->fw_state,
1469 S2255_FW_FAILED);
1470 return -EAGAIN;
1471 default:
1472 pr_info("%s: unknown state\n", __func__);
1473 return -EFAULT;
1474 }
1475 if (!vc->configured) {
1476
1477 vc->fmt = &formats[0];
1478 s2255_set_mode(vc, &vc->mode);
1479 vc->configured = 1;
1480 }
1481 return 0;
1482 }
1483
1484 static void s2255_destroy(struct s2255_dev *dev)
1485 {
1486 dprintk(dev, 1, "%s", __func__);
1487
1488 s2255_board_shutdown(dev);
1489
1490 del_timer_sync(&dev->timer);
1491 if (dev->fw_data->fw_urb) {
1492 usb_kill_urb(dev->fw_data->fw_urb);
1493 usb_free_urb(dev->fw_data->fw_urb);
1494 dev->fw_data->fw_urb = NULL;
1495 }
1496 release_firmware(dev->fw_data->fw);
1497 kfree(dev->fw_data->pfw_data);
1498 kfree(dev->fw_data);
1499
1500 s2255_reset_dsppower(dev);
1501 mutex_destroy(&dev->lock);
1502 usb_put_dev(dev->udev);
1503 v4l2_device_unregister(&dev->v4l2_dev);
1504 kfree(dev->cmdbuf);
1505 kfree(dev);
1506 }
1507
1508 static const struct v4l2_file_operations s2255_fops_v4l = {
1509 .owner = THIS_MODULE,
1510 .open = s2255_open,
1511 .release = vb2_fop_release,
1512 .poll = vb2_fop_poll,
1513 .unlocked_ioctl = video_ioctl2,
1514 .mmap = vb2_fop_mmap,
1515 .read = vb2_fop_read,
1516 };
1517
1518 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1519 .vidioc_querycap = vidioc_querycap,
1520 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1521 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1522 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1523 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1524 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1525 .vidioc_querybuf = vb2_ioctl_querybuf,
1526 .vidioc_qbuf = vb2_ioctl_qbuf,
1527 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1528 .vidioc_s_std = vidioc_s_std,
1529 .vidioc_g_std = vidioc_g_std,
1530 .vidioc_enum_input = vidioc_enum_input,
1531 .vidioc_g_input = vidioc_g_input,
1532 .vidioc_s_input = vidioc_s_input,
1533 .vidioc_streamon = vb2_ioctl_streamon,
1534 .vidioc_streamoff = vb2_ioctl_streamoff,
1535 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1536 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1537 .vidioc_s_parm = vidioc_s_parm,
1538 .vidioc_g_parm = vidioc_g_parm,
1539 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1540 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1541 .vidioc_log_status = v4l2_ctrl_log_status,
1542 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1543 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1544 };
1545
1546 static void s2255_video_device_release(struct video_device *vdev)
1547 {
1548 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1549 struct s2255_vc *vc =
1550 container_of(vdev, struct s2255_vc, vdev);
1551
1552 dprintk(dev, 4, "%s, chnls: %d\n", __func__,
1553 atomic_read(&dev->num_channels));
1554
1555 v4l2_ctrl_handler_free(&vc->hdl);
1556
1557 if (atomic_dec_and_test(&dev->num_channels))
1558 s2255_destroy(dev);
1559 return;
1560 }
1561
1562 static const struct video_device template = {
1563 .name = "s2255v",
1564 .fops = &s2255_fops_v4l,
1565 .ioctl_ops = &s2255_ioctl_ops,
1566 .release = s2255_video_device_release,
1567 .tvnorms = S2255_NORMS,
1568 };
1569
1570 static const struct v4l2_ctrl_ops s2255_ctrl_ops = {
1571 .s_ctrl = s2255_s_ctrl,
1572 };
1573
1574 static const struct v4l2_ctrl_config color_filter_ctrl = {
1575 .ops = &s2255_ctrl_ops,
1576 .name = "Color Filter",
1577 .id = V4L2_CID_S2255_COLORFILTER,
1578 .type = V4L2_CTRL_TYPE_BOOLEAN,
1579 .max = 1,
1580 .step = 1,
1581 .def = 1,
1582 };
1583
1584 static int s2255_probe_v4l(struct s2255_dev *dev)
1585 {
1586 int ret;
1587 int i;
1588 int cur_nr = video_nr;
1589 struct s2255_vc *vc;
1590 struct vb2_queue *q;
1591
1592 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1593 if (ret)
1594 return ret;
1595
1596
1597 for (i = 0; i < MAX_CHANNELS; i++) {
1598 vc = &dev->vc[i];
1599 INIT_LIST_HEAD(&vc->buf_list);
1600
1601 v4l2_ctrl_handler_init(&vc->hdl, 6);
1602 v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1603 V4L2_CID_BRIGHTNESS, -127, 127, 1, DEF_BRIGHT);
1604 v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1605 V4L2_CID_CONTRAST, 0, 255, 1, DEF_CONTRAST);
1606 v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1607 V4L2_CID_SATURATION, 0, 255, 1, DEF_SATURATION);
1608 v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1609 V4L2_CID_HUE, 0, 255, 1, DEF_HUE);
1610 vc->jpegqual_ctrl = v4l2_ctrl_new_std(&vc->hdl,
1611 &s2255_ctrl_ops,
1612 V4L2_CID_JPEG_COMPRESSION_QUALITY,
1613 0, 100, 1, S2255_DEF_JPEG_QUAL);
1614 if (dev->dsp_fw_ver >= S2255_MIN_DSP_COLORFILTER &&
1615 (dev->pid != 0x2257 || vc->idx <= 1))
1616 v4l2_ctrl_new_custom(&vc->hdl, &color_filter_ctrl,
1617 NULL);
1618 if (vc->hdl.error) {
1619 ret = vc->hdl.error;
1620 v4l2_ctrl_handler_free(&vc->hdl);
1621 dev_err(&dev->udev->dev, "couldn't register control\n");
1622 break;
1623 }
1624 q = &vc->vb_vidq;
1625 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1626 q->io_modes = VB2_MMAP | VB2_READ | VB2_USERPTR;
1627 q->drv_priv = vc;
1628 q->lock = &vc->vb_lock;
1629 q->buf_struct_size = sizeof(struct s2255_buffer);
1630 q->mem_ops = &vb2_vmalloc_memops;
1631 q->ops = &s2255_video_qops;
1632 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1633 ret = vb2_queue_init(q);
1634 if (ret != 0) {
1635 dev_err(&dev->udev->dev,
1636 "%s vb2_queue_init 0x%x\n", __func__, ret);
1637 break;
1638 }
1639
1640 vc->vdev = template;
1641 vc->vdev.queue = q;
1642 vc->vdev.ctrl_handler = &vc->hdl;
1643 vc->vdev.lock = &dev->lock;
1644 vc->vdev.v4l2_dev = &dev->v4l2_dev;
1645 vc->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1646 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1647 video_set_drvdata(&vc->vdev, vc);
1648 if (video_nr == -1)
1649 ret = video_register_device(&vc->vdev,
1650 VFL_TYPE_VIDEO,
1651 video_nr);
1652 else
1653 ret = video_register_device(&vc->vdev,
1654 VFL_TYPE_VIDEO,
1655 cur_nr + i);
1656
1657 if (ret) {
1658 dev_err(&dev->udev->dev,
1659 "failed to register video device!\n");
1660 break;
1661 }
1662 atomic_inc(&dev->num_channels);
1663 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1664 video_device_node_name(&vc->vdev));
1665
1666 }
1667 pr_info("Sensoray 2255 V4L driver Revision: %s\n",
1668 S2255_VERSION);
1669
1670 if (atomic_read(&dev->num_channels) == 0) {
1671 v4l2_device_unregister(&dev->v4l2_dev);
1672 return ret;
1673 }
1674 if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
1675 pr_warn("s2255: Not all channels available.\n");
1676 return 0;
1677 }
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1691 {
1692 char *pdest;
1693 u32 offset = 0;
1694 int bframe = 0;
1695 char *psrc;
1696 unsigned long copy_size;
1697 unsigned long size;
1698 s32 idx = -1;
1699 struct s2255_framei *frm;
1700 unsigned char *pdata;
1701 struct s2255_vc *vc;
1702 dprintk(dev, 100, "buffer to user\n");
1703 vc = &dev->vc[dev->cc];
1704 idx = vc->cur_frame;
1705 frm = &vc->buffer.frame[idx];
1706 if (frm->ulState == S2255_READ_IDLE) {
1707 int jj;
1708 unsigned int cc;
1709 __le32 *pdword;
1710 int payload;
1711
1712 pdata = (unsigned char *)pipe_info->transfer_buffer;
1713 pdword = (__le32 *)pdata;
1714 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1715 switch (*pdword) {
1716 case S2255_MARKER_FRAME:
1717 dprintk(dev, 4, "marker @ offset: %d [%x %x]\n",
1718 jj, pdata[0], pdata[1]);
1719 offset = jj + PREFIX_SIZE;
1720 bframe = 1;
1721 cc = le32_to_cpu(pdword[1]);
1722 if (cc >= MAX_CHANNELS) {
1723 dprintk(dev, 0,
1724 "bad channel\n");
1725 return -EINVAL;
1726 }
1727
1728 dev->cc = G_chnmap[cc];
1729 vc = &dev->vc[dev->cc];
1730 payload = le32_to_cpu(pdword[3]);
1731 if (payload > vc->req_image_size) {
1732 vc->bad_payload++;
1733
1734 return -EINVAL;
1735 }
1736 vc->pkt_size = payload;
1737 vc->jpg_size = le32_to_cpu(pdword[4]);
1738 break;
1739 case S2255_MARKER_RESPONSE:
1740
1741 pdata += DEF_USB_BLOCK;
1742 jj += DEF_USB_BLOCK;
1743 if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
1744 break;
1745 cc = G_chnmap[le32_to_cpu(pdword[1])];
1746 if (cc >= MAX_CHANNELS)
1747 break;
1748 vc = &dev->vc[cc];
1749 switch (pdword[2]) {
1750 case S2255_RESPONSE_SETMODE:
1751
1752
1753 vc->setmode_ready = 1;
1754 wake_up(&vc->wait_setmode);
1755 dprintk(dev, 5, "setmode rdy %d\n", cc);
1756 break;
1757 case S2255_RESPONSE_FW:
1758 dev->chn_ready |= (1 << cc);
1759 if ((dev->chn_ready & 0x0f) != 0x0f)
1760 break;
1761
1762 pr_info("s2255: fw loaded\n");
1763 atomic_set(&dev->fw_data->fw_state,
1764 S2255_FW_SUCCESS);
1765 wake_up(&dev->fw_data->wait_fw);
1766 break;
1767 case S2255_RESPONSE_STATUS:
1768 vc->vidstatus = le32_to_cpu(pdword[3]);
1769 vc->vidstatus_ready = 1;
1770 wake_up(&vc->wait_vidstatus);
1771 dprintk(dev, 5, "vstat %x chan %d\n",
1772 le32_to_cpu(pdword[3]), cc);
1773 break;
1774 default:
1775 pr_info("s2255 unknown resp\n");
1776 }
1777 pdata++;
1778 break;
1779 default:
1780 pdata++;
1781 break;
1782 }
1783 if (bframe)
1784 break;
1785 }
1786 if (!bframe)
1787 return -EINVAL;
1788 }
1789 vc = &dev->vc[dev->cc];
1790 idx = vc->cur_frame;
1791 frm = &vc->buffer.frame[idx];
1792
1793 if (!vb2_is_streaming(&vc->vb_vidq)) {
1794
1795 frm->ulState = S2255_READ_IDLE;
1796 return -EINVAL;
1797 }
1798
1799 if (frm->ulState == S2255_READ_IDLE) {
1800 frm->ulState = S2255_READ_FRAME;
1801 frm->cur_size = 0;
1802 }
1803
1804
1805 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1806
1807
1808 if (frm->lpvbits == NULL) {
1809 dprintk(dev, 1, "s2255 frame buffer == NULL.%p %p %d %d",
1810 frm, dev, dev->cc, idx);
1811 return -ENOMEM;
1812 }
1813
1814 pdest = frm->lpvbits + frm->cur_size;
1815
1816 copy_size = (pipe_info->cur_transfer_size - offset);
1817
1818 size = vc->pkt_size - PREFIX_SIZE;
1819
1820
1821 if ((copy_size + frm->cur_size) < vc->req_image_size)
1822 memcpy(pdest, psrc, copy_size);
1823
1824 frm->cur_size += copy_size;
1825 dprintk(dev, 4, "cur_size: %lu, size: %lu\n", frm->cur_size, size);
1826
1827 if (frm->cur_size >= size) {
1828 dprintk(dev, 2, "******[%d]Buffer[%d]full*******\n",
1829 dev->cc, idx);
1830 vc->last_frame = vc->cur_frame;
1831 vc->cur_frame++;
1832
1833 if ((vc->cur_frame == SYS_FRAMES) ||
1834 (vc->cur_frame == vc->buffer.dwFrames))
1835 vc->cur_frame = 0;
1836
1837 if (vb2_is_streaming(&vc->vb_vidq))
1838 s2255_got_frame(vc, vc->jpg_size);
1839 vc->frame_count++;
1840 frm->ulState = S2255_READ_IDLE;
1841 frm->cur_size = 0;
1842
1843 }
1844
1845 return 0;
1846 }
1847
1848 static void s2255_read_video_callback(struct s2255_dev *dev,
1849 struct s2255_pipeinfo *pipe_info)
1850 {
1851 int res;
1852 dprintk(dev, 50, "callback read video\n");
1853
1854 if (dev->cc >= MAX_CHANNELS) {
1855 dev->cc = 0;
1856 dev_err(&dev->udev->dev, "invalid channel\n");
1857 return;
1858 }
1859
1860 res = save_frame(dev, pipe_info);
1861 if (res != 0)
1862 dprintk(dev, 4, "s2255: read callback failed\n");
1863
1864 dprintk(dev, 50, "callback read video done\n");
1865 return;
1866 }
1867
1868 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
1869 u16 Index, u16 Value, void *TransferBuffer,
1870 s32 TransferBufferLength, int bOut)
1871 {
1872 int r;
1873 unsigned char *buf;
1874
1875 buf = kmalloc(TransferBufferLength, GFP_KERNEL);
1876 if (!buf)
1877 return -ENOMEM;
1878
1879 if (!bOut) {
1880 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1881 Request,
1882 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1883 USB_DIR_IN,
1884 Value, Index, buf,
1885 TransferBufferLength, USB_CTRL_SET_TIMEOUT);
1886
1887 if (r >= 0)
1888 memcpy(TransferBuffer, buf, TransferBufferLength);
1889 } else {
1890 memcpy(buf, TransferBuffer, TransferBufferLength);
1891 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1892 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1893 Value, Index, buf,
1894 TransferBufferLength, USB_CTRL_SET_TIMEOUT);
1895 }
1896 kfree(buf);
1897 return r;
1898 }
1899
1900
1901
1902
1903
1904
1905 static int s2255_get_fx2fw(struct s2255_dev *dev)
1906 {
1907 int fw;
1908 int ret;
1909 unsigned char transBuffer[64];
1910 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
1911 S2255_VR_IN);
1912 if (ret < 0)
1913 dprintk(dev, 2, "get fw error: %x\n", ret);
1914 fw = transBuffer[0] + (transBuffer[1] << 8);
1915 dprintk(dev, 2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
1916 return fw;
1917 }
1918
1919
1920
1921
1922
1923 static int s2255_create_sys_buffers(struct s2255_vc *vc)
1924 {
1925 unsigned long i;
1926 unsigned long reqsize;
1927 vc->buffer.dwFrames = SYS_FRAMES;
1928
1929 reqsize = SYS_FRAMES_MAXSIZE;
1930
1931 if (reqsize > SYS_FRAMES_MAXSIZE)
1932 reqsize = SYS_FRAMES_MAXSIZE;
1933
1934 for (i = 0; i < SYS_FRAMES; i++) {
1935
1936 vc->buffer.frame[i].lpvbits = vmalloc(reqsize);
1937 vc->buffer.frame[i].size = reqsize;
1938 if (vc->buffer.frame[i].lpvbits == NULL) {
1939 pr_info("out of memory. using less frames\n");
1940 vc->buffer.dwFrames = i;
1941 break;
1942 }
1943 }
1944
1945
1946 for (i = 0; i < SYS_FRAMES; i++) {
1947 vc->buffer.frame[i].ulState = 0;
1948 vc->buffer.frame[i].cur_size = 0;
1949 }
1950
1951 vc->cur_frame = 0;
1952 vc->last_frame = -1;
1953 return 0;
1954 }
1955
1956 static int s2255_release_sys_buffers(struct s2255_vc *vc)
1957 {
1958 unsigned long i;
1959 for (i = 0; i < SYS_FRAMES; i++) {
1960 vfree(vc->buffer.frame[i].lpvbits);
1961 vc->buffer.frame[i].lpvbits = NULL;
1962 }
1963 return 0;
1964 }
1965
1966 static int s2255_board_init(struct s2255_dev *dev)
1967 {
1968 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
1969 int fw_ver;
1970 int j;
1971 struct s2255_pipeinfo *pipe = &dev->pipe;
1972 dprintk(dev, 4, "board init: %p", dev);
1973 memset(pipe, 0, sizeof(*pipe));
1974 pipe->dev = dev;
1975 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
1976 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
1977
1978 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
1979 GFP_KERNEL);
1980 if (pipe->transfer_buffer == NULL) {
1981 dprintk(dev, 1, "out of memory!\n");
1982 return -ENOMEM;
1983 }
1984
1985 fw_ver = s2255_get_fx2fw(dev);
1986
1987 pr_info("s2255: usb firmware version %d.%d\n",
1988 (fw_ver >> 8) & 0xff,
1989 fw_ver & 0xff);
1990
1991 if (fw_ver < S2255_CUR_USB_FWVER)
1992 pr_info("s2255: newer USB firmware available\n");
1993
1994 for (j = 0; j < MAX_CHANNELS; j++) {
1995 struct s2255_vc *vc = &dev->vc[j];
1996 vc->mode = mode_def;
1997 if (dev->pid == 0x2257 && j > 1)
1998 vc->mode.color |= (1 << 16);
1999 vc->jpegqual = S2255_DEF_JPEG_QUAL;
2000 vc->width = LINE_SZ_4CIFS_NTSC;
2001 vc->height = NUM_LINES_4CIFS_NTSC * 2;
2002 vc->std = V4L2_STD_NTSC_M;
2003 vc->fmt = &formats[0];
2004 vc->mode.restart = 1;
2005 vc->req_image_size = get_transfer_size(&mode_def);
2006 vc->frame_count = 0;
2007
2008 s2255_create_sys_buffers(vc);
2009 }
2010
2011 s2255_start_readpipe(dev);
2012 dprintk(dev, 1, "%s: success\n", __func__);
2013 return 0;
2014 }
2015
2016 static int s2255_board_shutdown(struct s2255_dev *dev)
2017 {
2018 u32 i;
2019 dprintk(dev, 1, "%s: dev: %p", __func__, dev);
2020
2021 for (i = 0; i < MAX_CHANNELS; i++) {
2022 if (vb2_is_streaming(&dev->vc[i].vb_vidq))
2023 s2255_stop_acquire(&dev->vc[i]);
2024 }
2025 s2255_stop_readpipe(dev);
2026 for (i = 0; i < MAX_CHANNELS; i++)
2027 s2255_release_sys_buffers(&dev->vc[i]);
2028
2029 kfree(dev->pipe.transfer_buffer);
2030 return 0;
2031 }
2032
2033 static void read_pipe_completion(struct urb *purb)
2034 {
2035 struct s2255_pipeinfo *pipe_info;
2036 struct s2255_dev *dev;
2037 int status;
2038 int pipe;
2039 pipe_info = purb->context;
2040 if (pipe_info == NULL) {
2041 dev_err(&purb->dev->dev, "no context!\n");
2042 return;
2043 }
2044 dev = pipe_info->dev;
2045 if (dev == NULL) {
2046 dev_err(&purb->dev->dev, "no context!\n");
2047 return;
2048 }
2049 status = purb->status;
2050
2051 if (status == -ESHUTDOWN) {
2052 dprintk(dev, 2, "%s: err shutdown\n", __func__);
2053 pipe_info->err_count++;
2054 return;
2055 }
2056
2057 if (pipe_info->state == 0) {
2058 dprintk(dev, 2, "%s: exiting USB pipe", __func__);
2059 return;
2060 }
2061
2062 if (status == 0)
2063 s2255_read_video_callback(dev, pipe_info);
2064 else {
2065 pipe_info->err_count++;
2066 dprintk(dev, 1, "%s: failed URB %d\n", __func__, status);
2067 }
2068
2069 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2070
2071 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2072 pipe,
2073 pipe_info->transfer_buffer,
2074 pipe_info->cur_transfer_size,
2075 read_pipe_completion, pipe_info);
2076
2077 if (pipe_info->state != 0) {
2078 if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC))
2079 dev_err(&dev->udev->dev, "error submitting urb\n");
2080 } else {
2081 dprintk(dev, 2, "%s :complete state 0\n", __func__);
2082 }
2083 return;
2084 }
2085
2086 static int s2255_start_readpipe(struct s2255_dev *dev)
2087 {
2088 int pipe;
2089 int retval;
2090 struct s2255_pipeinfo *pipe_info = &dev->pipe;
2091 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2092 dprintk(dev, 2, "%s: IN %d\n", __func__, dev->read_endpoint);
2093 pipe_info->state = 1;
2094 pipe_info->err_count = 0;
2095 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2096 if (!pipe_info->stream_urb)
2097 return -ENOMEM;
2098
2099 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2100 pipe,
2101 pipe_info->transfer_buffer,
2102 pipe_info->cur_transfer_size,
2103 read_pipe_completion, pipe_info);
2104 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2105 if (retval) {
2106 pr_err("s2255: start read pipe failed\n");
2107 return retval;
2108 }
2109 return 0;
2110 }
2111
2112
2113 static int s2255_start_acquire(struct s2255_vc *vc)
2114 {
2115 int res;
2116 unsigned long chn_rev;
2117 int j;
2118 struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
2119 __le32 *buffer = dev->cmdbuf;
2120
2121 mutex_lock(&dev->cmdlock);
2122 chn_rev = G_chnmap[vc->idx];
2123 vc->last_frame = -1;
2124 vc->bad_payload = 0;
2125 vc->cur_frame = 0;
2126 for (j = 0; j < SYS_FRAMES; j++) {
2127 vc->buffer.frame[j].ulState = 0;
2128 vc->buffer.frame[j].cur_size = 0;
2129 }
2130
2131
2132 buffer[0] = IN_DATA_TOKEN;
2133 buffer[1] = (__le32) cpu_to_le32(chn_rev);
2134 buffer[2] = CMD_START;
2135 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2136 if (res != 0)
2137 dev_err(&dev->udev->dev, "CMD_START error\n");
2138
2139 dprintk(dev, 2, "start acquire exit[%d] %d\n", vc->idx, res);
2140 mutex_unlock(&dev->cmdlock);
2141 return res;
2142 }
2143
2144 static int s2255_stop_acquire(struct s2255_vc *vc)
2145 {
2146 int res;
2147 unsigned long chn_rev;
2148 struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
2149 __le32 *buffer = dev->cmdbuf;
2150
2151 mutex_lock(&dev->cmdlock);
2152 chn_rev = G_chnmap[vc->idx];
2153
2154 buffer[0] = IN_DATA_TOKEN;
2155 buffer[1] = (__le32) cpu_to_le32(chn_rev);
2156 buffer[2] = CMD_STOP;
2157
2158 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2159 if (res != 0)
2160 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2161
2162 dprintk(dev, 4, "%s: chn %d, res %d\n", __func__, vc->idx, res);
2163 mutex_unlock(&dev->cmdlock);
2164 return res;
2165 }
2166
2167 static void s2255_stop_readpipe(struct s2255_dev *dev)
2168 {
2169 struct s2255_pipeinfo *pipe = &dev->pipe;
2170
2171 pipe->state = 0;
2172 if (pipe->stream_urb) {
2173
2174 usb_kill_urb(pipe->stream_urb);
2175 usb_free_urb(pipe->stream_urb);
2176 pipe->stream_urb = NULL;
2177 }
2178 dprintk(dev, 4, "%s", __func__);
2179 return;
2180 }
2181
2182 static void s2255_fwload_start(struct s2255_dev *dev)
2183 {
2184 s2255_reset_dsppower(dev);
2185 dev->fw_data->fw_size = dev->fw_data->fw->size;
2186 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2187 memcpy(dev->fw_data->pfw_data,
2188 dev->fw_data->fw->data, CHUNK_SIZE);
2189 dev->fw_data->fw_loaded = CHUNK_SIZE;
2190 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2191 usb_sndbulkpipe(dev->udev, 2),
2192 dev->fw_data->pfw_data,
2193 CHUNK_SIZE, s2255_fwchunk_complete,
2194 dev->fw_data);
2195 mod_timer(&dev->timer, jiffies + HZ);
2196 }
2197
2198
2199 static int s2255_probe(struct usb_interface *interface,
2200 const struct usb_device_id *id)
2201 {
2202 struct s2255_dev *dev = NULL;
2203 struct usb_host_interface *iface_desc;
2204 struct usb_endpoint_descriptor *endpoint;
2205 int i;
2206 int retval = -ENOMEM;
2207 __le32 *pdata;
2208 int fw_size;
2209
2210
2211 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2212 if (dev == NULL) {
2213 s2255_dev_err(&interface->dev, "out of memory\n");
2214 return -ENOMEM;
2215 }
2216
2217 dev->cmdbuf = kzalloc(S2255_CMDBUF_SIZE, GFP_KERNEL);
2218 if (dev->cmdbuf == NULL) {
2219 s2255_dev_err(&interface->dev, "out of memory\n");
2220 goto errorFWDATA1;
2221 }
2222
2223 atomic_set(&dev->num_channels, 0);
2224 dev->pid = id->idProduct;
2225 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2226 if (!dev->fw_data)
2227 goto errorFWDATA1;
2228 mutex_init(&dev->lock);
2229 mutex_init(&dev->cmdlock);
2230
2231 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2232 if (dev->udev == NULL) {
2233 dev_err(&interface->dev, "null usb device\n");
2234 retval = -ENODEV;
2235 goto errorUDEV;
2236 }
2237 dev_dbg(&interface->dev, "dev: %p, udev %p interface %p\n",
2238 dev, dev->udev, interface);
2239 dev->interface = interface;
2240
2241 iface_desc = interface->cur_altsetting;
2242 dev_dbg(&interface->dev, "num EP: %d\n",
2243 iface_desc->desc.bNumEndpoints);
2244 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2245 endpoint = &iface_desc->endpoint[i].desc;
2246 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2247
2248 dev->read_endpoint = endpoint->bEndpointAddress;
2249 }
2250 }
2251
2252 if (!dev->read_endpoint) {
2253 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2254 goto errorEP;
2255 }
2256 timer_setup(&dev->timer, s2255_timer, 0);
2257 init_waitqueue_head(&dev->fw_data->wait_fw);
2258 for (i = 0; i < MAX_CHANNELS; i++) {
2259 struct s2255_vc *vc = &dev->vc[i];
2260 vc->idx = i;
2261 vc->dev = dev;
2262 init_waitqueue_head(&vc->wait_setmode);
2263 init_waitqueue_head(&vc->wait_vidstatus);
2264 spin_lock_init(&vc->qlock);
2265 mutex_init(&vc->vb_lock);
2266 }
2267
2268 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2269 if (!dev->fw_data->fw_urb)
2270 goto errorFWURB;
2271
2272 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2273 if (!dev->fw_data->pfw_data) {
2274 dev_err(&interface->dev, "out of memory!\n");
2275 goto errorFWDATA2;
2276 }
2277
2278 if (request_firmware(&dev->fw_data->fw,
2279 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2280 dev_err(&interface->dev, "sensoray 2255 failed to get firmware\n");
2281 goto errorREQFW;
2282 }
2283
2284 fw_size = dev->fw_data->fw->size;
2285 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2286
2287 if (*pdata != S2255_FW_MARKER) {
2288 dev_err(&interface->dev, "Firmware invalid.\n");
2289 retval = -ENODEV;
2290 goto errorFWMARKER;
2291 } else {
2292
2293 __le32 *pRel;
2294 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2295 pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel));
2296 dev->dsp_fw_ver = le32_to_cpu(*pRel);
2297 if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER)
2298 pr_info("s2255: f2255usb.bin out of date.\n");
2299 if (dev->pid == 0x2257 &&
2300 dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
2301 pr_warn("2257 needs firmware %d or above.\n",
2302 S2255_MIN_DSP_COLORFILTER);
2303 }
2304 usb_reset_device(dev->udev);
2305
2306 retval = s2255_board_init(dev);
2307 if (retval)
2308 goto errorBOARDINIT;
2309 s2255_fwload_start(dev);
2310
2311 retval = s2255_probe_v4l(dev);
2312 if (retval)
2313 goto errorBOARDINIT;
2314 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2315 return 0;
2316 errorBOARDINIT:
2317 s2255_board_shutdown(dev);
2318 errorFWMARKER:
2319 release_firmware(dev->fw_data->fw);
2320 errorREQFW:
2321 kfree(dev->fw_data->pfw_data);
2322 errorFWDATA2:
2323 usb_free_urb(dev->fw_data->fw_urb);
2324 errorFWURB:
2325 del_timer_sync(&dev->timer);
2326 errorEP:
2327 usb_put_dev(dev->udev);
2328 errorUDEV:
2329 kfree(dev->fw_data);
2330 mutex_destroy(&dev->lock);
2331 errorFWDATA1:
2332 kfree(dev->cmdbuf);
2333 kfree(dev);
2334 pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval);
2335 return retval;
2336 }
2337
2338
2339 static void s2255_disconnect(struct usb_interface *interface)
2340 {
2341 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
2342 int i;
2343 int channels = atomic_read(&dev->num_channels);
2344 mutex_lock(&dev->lock);
2345 v4l2_device_disconnect(&dev->v4l2_dev);
2346 mutex_unlock(&dev->lock);
2347
2348 atomic_inc(&dev->num_channels);
2349
2350 for (i = 0; i < channels; i++)
2351 video_unregister_device(&dev->vc[i].vdev);
2352
2353 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2354 wake_up(&dev->fw_data->wait_fw);
2355 for (i = 0; i < MAX_CHANNELS; i++) {
2356 dev->vc[i].setmode_ready = 1;
2357 wake_up(&dev->vc[i].wait_setmode);
2358 dev->vc[i].vidstatus_ready = 1;
2359 wake_up(&dev->vc[i].wait_vidstatus);
2360 }
2361 if (atomic_dec_and_test(&dev->num_channels))
2362 s2255_destroy(dev);
2363 dev_info(&interface->dev, "%s\n", __func__);
2364 }
2365
2366 static struct usb_driver s2255_driver = {
2367 .name = S2255_DRIVER_NAME,
2368 .probe = s2255_probe,
2369 .disconnect = s2255_disconnect,
2370 .id_table = s2255_table,
2371 };
2372
2373 module_usb_driver(s2255_driver);
2374
2375 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2376 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2377 MODULE_LICENSE("GPL");
2378 MODULE_VERSION(S2255_VERSION);
2379 MODULE_FIRMWARE(FIRMWARE_FILE_NAME);