0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <linux/kernel.h>
0021 #include <linux/errno.h>
0022 #include <linux/delay.h>
0023 #include <linux/init.h>
0024 #include <linux/slab.h>
0025 #include <linux/module.h>
0026 #include <linux/completion.h>
0027 #include <linux/uaccess.h>
0028 #include <linux/usb.h>
0029 #include <linux/printk.h>
0030 #include <linux/input.h>
0031 #include <linux/input/mt.h>
0032 #include <linux/usb/input.h>
0033 #include <linux/videodev2.h>
0034 #include <media/v4l2-device.h>
0035 #include <media/v4l2-dev.h>
0036 #include <media/v4l2-ioctl.h>
0037 #include <media/v4l2-ctrls.h>
0038 #include <media/videobuf2-v4l2.h>
0039 #include <media/videobuf2-dma-sg.h>
0040
0041
0042 struct sur40_header {
0043
0044 __le16 type;
0045 __le16 count;
0046
0047 __le32 packet_id;
0048
0049 __le32 timestamp;
0050 __le32 unknown;
0051
0052 } __packed;
0053
0054 struct sur40_blob {
0055
0056 __le16 blob_id;
0057
0058 u8 action;
0059 u8 type;
0060
0061 __le16 bb_pos_x;
0062 __le16 bb_pos_y;
0063
0064 __le16 bb_size_x;
0065 __le16 bb_size_y;
0066
0067 __le16 pos_x;
0068 __le16 pos_y;
0069
0070 __le16 ctr_x;
0071 __le16 ctr_y;
0072
0073 __le16 axis_x;
0074 __le16 axis_y;
0075
0076 __le32 angle;
0077
0078
0079 __le32 area;
0080
0081 u8 padding[24];
0082
0083 __le32 tag_id;
0084 __le32 unknown;
0085
0086 } __packed;
0087
0088
0089 struct sur40_data {
0090 struct sur40_header header;
0091 struct sur40_blob blobs[];
0092 } __packed;
0093
0094
0095
0096 struct sur40_image_header {
0097 __le32 magic;
0098 __le32 packet_id;
0099 __le32 size;
0100 __le32 timestamp;
0101 __le32 unknown;
0102 } __packed;
0103
0104
0105 #define DRIVER_SHORT "sur40"
0106 #define DRIVER_LONG "Samsung SUR40"
0107 #define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
0108 #define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
0109
0110
0111 #define ID_MICROSOFT 0x045e
0112 #define ID_SUR40 0x0775
0113
0114
0115 #define SENSOR_RES_X 1920
0116 #define SENSOR_RES_Y 1080
0117
0118
0119 #define TOUCH_ENDPOINT 0x86
0120
0121
0122 #define VIDEO_ENDPOINT 0x82
0123
0124
0125 #define VIDEO_HEADER_MAGIC 0x46425553
0126 #define VIDEO_PACKET_SIZE 16384
0127
0128
0129 #define POLL_INTERVAL 1
0130
0131
0132 #define MAX_CONTACTS 64
0133
0134
0135 #define SUR40_GET_VERSION 0xb0
0136 #define SUR40_ACCEL_CAPS 0xb3
0137 #define SUR40_SENSOR_CAPS 0xc1
0138
0139 #define SUR40_POKE 0xc5
0140 #define SUR40_PEEK 0xc4
0141
0142 #define SUR40_GET_STATE 0xc5
0143 #define SUR40_GET_SENSORS 0xb1
0144
0145 #define SUR40_BLOB 0x01
0146 #define SUR40_TOUCH 0x02
0147 #define SUR40_TAG 0x04
0148
0149
0150 #define SUR40_BRIGHTNESS_MAX 0xff
0151 #define SUR40_BRIGHTNESS_MIN 0x00
0152 #define SUR40_BRIGHTNESS_DEF 0xff
0153
0154 #define SUR40_CONTRAST_MAX 0x0f
0155 #define SUR40_CONTRAST_MIN 0x00
0156 #define SUR40_CONTRAST_DEF 0x0a
0157
0158 #define SUR40_GAIN_MAX 0x09
0159 #define SUR40_GAIN_MIN 0x00
0160 #define SUR40_GAIN_DEF 0x08
0161
0162 #define SUR40_BACKLIGHT_MAX 0x01
0163 #define SUR40_BACKLIGHT_MIN 0x00
0164 #define SUR40_BACKLIGHT_DEF 0x01
0165
0166 #define sur40_str(s) #s
0167 #define SUR40_PARAM_RANGE(lo, hi) " (range " sur40_str(lo) "-" sur40_str(hi) ")"
0168
0169
0170 static uint brightness = SUR40_BRIGHTNESS_DEF;
0171 module_param(brightness, uint, 0644);
0172 MODULE_PARM_DESC(brightness, "set initial brightness"
0173 SUR40_PARAM_RANGE(SUR40_BRIGHTNESS_MIN, SUR40_BRIGHTNESS_MAX));
0174 static uint contrast = SUR40_CONTRAST_DEF;
0175 module_param(contrast, uint, 0644);
0176 MODULE_PARM_DESC(contrast, "set initial contrast"
0177 SUR40_PARAM_RANGE(SUR40_CONTRAST_MIN, SUR40_CONTRAST_MAX));
0178 static uint gain = SUR40_GAIN_DEF;
0179 module_param(gain, uint, 0644);
0180 MODULE_PARM_DESC(gain, "set initial gain"
0181 SUR40_PARAM_RANGE(SUR40_GAIN_MIN, SUR40_GAIN_MAX));
0182
0183 static const struct v4l2_pix_format sur40_pix_format[] = {
0184 {
0185 .pixelformat = V4L2_TCH_FMT_TU08,
0186 .width = SENSOR_RES_X / 2,
0187 .height = SENSOR_RES_Y / 2,
0188 .field = V4L2_FIELD_NONE,
0189 .colorspace = V4L2_COLORSPACE_RAW,
0190 .bytesperline = SENSOR_RES_X / 2,
0191 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
0192 },
0193 {
0194 .pixelformat = V4L2_PIX_FMT_GREY,
0195 .width = SENSOR_RES_X / 2,
0196 .height = SENSOR_RES_Y / 2,
0197 .field = V4L2_FIELD_NONE,
0198 .colorspace = V4L2_COLORSPACE_RAW,
0199 .bytesperline = SENSOR_RES_X / 2,
0200 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
0201 }
0202 };
0203
0204
0205 struct sur40_state {
0206
0207 struct usb_device *usbdev;
0208 struct device *dev;
0209 struct input_dev *input;
0210
0211 struct v4l2_device v4l2;
0212 struct video_device vdev;
0213 struct mutex lock;
0214 struct v4l2_pix_format pix_fmt;
0215 struct v4l2_ctrl_handler hdl;
0216
0217 struct vb2_queue queue;
0218 struct list_head buf_list;
0219 spinlock_t qlock;
0220 int sequence;
0221
0222 struct sur40_data *bulk_in_buffer;
0223 size_t bulk_in_size;
0224 u8 bulk_in_epaddr;
0225 u8 vsvideo;
0226
0227 char phys[64];
0228 };
0229
0230 struct sur40_buffer {
0231 struct vb2_v4l2_buffer vb;
0232 struct list_head list;
0233 };
0234
0235
0236 static const struct video_device sur40_video_device;
0237 static const struct vb2_queue sur40_queue;
0238 static void sur40_process_video(struct sur40_state *sur40);
0239 static int sur40_s_ctrl(struct v4l2_ctrl *ctrl);
0240
0241 static const struct v4l2_ctrl_ops sur40_ctrl_ops = {
0242 .s_ctrl = sur40_s_ctrl,
0243 };
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 static int sur40_command(struct sur40_state *dev,
0256 u8 command, u16 index, void *buffer, u16 size)
0257 {
0258 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
0259 command,
0260 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0261 0x00, index, buffer, size, 1000);
0262 }
0263
0264
0265 static int sur40_poke(struct sur40_state *dev, u8 offset, u8 value)
0266 {
0267 int result;
0268 u8 index = 0x96;
0269
0270 result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
0271 SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0272 0x32, index, NULL, 0, 1000);
0273 if (result < 0)
0274 goto error;
0275 msleep(5);
0276
0277 result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
0278 SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0279 0x72, offset, NULL, 0, 1000);
0280 if (result < 0)
0281 goto error;
0282 msleep(5);
0283
0284 result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
0285 SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0286 0xb2, value, NULL, 0, 1000);
0287 if (result < 0)
0288 goto error;
0289 msleep(5);
0290
0291 error:
0292 return result;
0293 }
0294
0295 static int sur40_set_preprocessor(struct sur40_state *dev, u8 value)
0296 {
0297 u8 setting_07[2] = { 0x01, 0x00 };
0298 u8 setting_17[2] = { 0x85, 0x80 };
0299 int result;
0300
0301 if (value > 1)
0302 return -ERANGE;
0303
0304 result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
0305 SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0306 0x07, setting_07[value], NULL, 0, 1000);
0307 if (result < 0)
0308 goto error;
0309 msleep(5);
0310
0311 result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
0312 SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0313 0x17, setting_17[value], NULL, 0, 1000);
0314 if (result < 0)
0315 goto error;
0316 msleep(5);
0317
0318 error:
0319 return result;
0320 }
0321
0322 static void sur40_set_vsvideo(struct sur40_state *handle, u8 value)
0323 {
0324 int i;
0325
0326 for (i = 0; i < 4; i++)
0327 sur40_poke(handle, 0x1c+i, value);
0328 handle->vsvideo = value;
0329 }
0330
0331 static void sur40_set_irlevel(struct sur40_state *handle, u8 value)
0332 {
0333 int i;
0334
0335 for (i = 0; i < 8; i++)
0336 sur40_poke(handle, 0x08+(2*i), value);
0337 }
0338
0339
0340 static int sur40_init(struct sur40_state *dev)
0341 {
0342 int result;
0343 u8 *buffer;
0344
0345 buffer = kmalloc(24, GFP_KERNEL);
0346 if (!buffer) {
0347 result = -ENOMEM;
0348 goto error;
0349 }
0350
0351
0352 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
0353 if (result < 0)
0354 goto error;
0355
0356 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
0357 if (result < 0)
0358 goto error;
0359
0360 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
0361 if (result < 0)
0362 goto error;
0363
0364 result = sur40_command(dev, SUR40_SENSOR_CAPS, 0x00, buffer, 24);
0365 if (result < 0)
0366 goto error;
0367
0368 result = sur40_command(dev, SUR40_ACCEL_CAPS, 0x00, buffer, 5);
0369 if (result < 0)
0370 goto error;
0371
0372 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
0373 if (result < 0)
0374 goto error;
0375
0376 result = 0;
0377
0378
0379
0380
0381
0382 error:
0383 kfree(buffer);
0384 return result;
0385 }
0386
0387
0388
0389
0390
0391
0392 static int sur40_open(struct input_dev *input)
0393 {
0394 struct sur40_state *sur40 = input_get_drvdata(input);
0395
0396 dev_dbg(sur40->dev, "open\n");
0397 return sur40_init(sur40);
0398 }
0399
0400
0401 static void sur40_close(struct input_dev *input)
0402 {
0403 struct sur40_state *sur40 = input_get_drvdata(input);
0404
0405 dev_dbg(sur40->dev, "close\n");
0406
0407
0408
0409
0410 }
0411
0412
0413
0414
0415
0416 static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
0417 {
0418 int wide, major, minor;
0419 int bb_size_x, bb_size_y, pos_x, pos_y, ctr_x, ctr_y, slotnum;
0420
0421 if (blob->type != SUR40_TOUCH)
0422 return;
0423
0424 slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
0425 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
0426 return;
0427
0428 bb_size_x = le16_to_cpu(blob->bb_size_x);
0429 bb_size_y = le16_to_cpu(blob->bb_size_y);
0430
0431 pos_x = le16_to_cpu(blob->pos_x);
0432 pos_y = le16_to_cpu(blob->pos_y);
0433
0434 ctr_x = le16_to_cpu(blob->ctr_x);
0435 ctr_y = le16_to_cpu(blob->ctr_y);
0436
0437 input_mt_slot(input, slotnum);
0438 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
0439 wide = (bb_size_x > bb_size_y);
0440 major = max(bb_size_x, bb_size_y);
0441 minor = min(bb_size_x, bb_size_y);
0442
0443 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
0444 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
0445 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
0446 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
0447
0448
0449 input_report_abs(input, ABS_MT_ORIENTATION, wide);
0450 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
0451 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
0452 }
0453
0454
0455 static void sur40_poll(struct input_dev *input)
0456 {
0457 struct sur40_state *sur40 = input_get_drvdata(input);
0458 int result, bulk_read, need_blobs, packet_blobs, i;
0459 struct sur40_header *header = &sur40->bulk_in_buffer->header;
0460 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
0461
0462 dev_dbg(sur40->dev, "poll\n");
0463
0464 need_blobs = -1;
0465
0466 do {
0467
0468
0469 result = usb_bulk_msg(sur40->usbdev,
0470 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
0471 sur40->bulk_in_buffer, sur40->bulk_in_size,
0472 &bulk_read, 1000);
0473
0474 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
0475
0476 if (result < 0) {
0477 dev_err(sur40->dev, "error in usb_bulk_read\n");
0478 return;
0479 }
0480
0481 result = bulk_read - sizeof(struct sur40_header);
0482
0483 if (result % sizeof(struct sur40_blob) != 0) {
0484 dev_err(sur40->dev, "transfer size mismatch\n");
0485 return;
0486 }
0487
0488
0489 if (need_blobs == -1) {
0490 need_blobs = le16_to_cpu(header->count);
0491 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
0492
0493 }
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 packet_blobs = result / sizeof(struct sur40_blob);
0507 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
0508
0509
0510 if (packet_blobs > need_blobs)
0511 packet_blobs = need_blobs;
0512
0513 for (i = 0; i < packet_blobs; i++) {
0514 need_blobs--;
0515 dev_dbg(sur40->dev, "processing blob\n");
0516 sur40_report_blob(&(inblob[i]), input);
0517 }
0518
0519 } while (need_blobs > 0);
0520
0521 input_mt_sync_frame(input);
0522 input_sync(input);
0523
0524 sur40_process_video(sur40);
0525 }
0526
0527
0528 static void sur40_process_video(struct sur40_state *sur40)
0529 {
0530
0531 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
0532 struct sur40_buffer *new_buf;
0533 struct usb_sg_request sgr;
0534 struct sg_table *sgt;
0535 int result, bulk_read;
0536
0537 if (!vb2_start_streaming_called(&sur40->queue))
0538 return;
0539
0540
0541 spin_lock(&sur40->qlock);
0542 if (list_empty(&sur40->buf_list)) {
0543 dev_dbg(sur40->dev, "buffer queue empty\n");
0544 spin_unlock(&sur40->qlock);
0545 return;
0546 }
0547 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
0548 list_del(&new_buf->list);
0549 spin_unlock(&sur40->qlock);
0550
0551 dev_dbg(sur40->dev, "buffer acquired\n");
0552
0553
0554 result = usb_bulk_msg(sur40->usbdev,
0555 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
0556 sur40->bulk_in_buffer, sur40->bulk_in_size,
0557 &bulk_read, 1000);
0558
0559 if (result < 0) {
0560 dev_err(sur40->dev, "error in usb_bulk_read\n");
0561 goto err_poll;
0562 }
0563
0564 if (bulk_read != sizeof(struct sur40_image_header)) {
0565 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
0566 bulk_read, sizeof(struct sur40_image_header));
0567 goto err_poll;
0568 }
0569
0570 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
0571 dev_err(sur40->dev, "image magic mismatch\n");
0572 goto err_poll;
0573 }
0574
0575 if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
0576 dev_err(sur40->dev, "image size mismatch\n");
0577 goto err_poll;
0578 }
0579
0580 dev_dbg(sur40->dev, "header acquired\n");
0581
0582 sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
0583
0584 result = usb_sg_init(&sgr, sur40->usbdev,
0585 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
0586 sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
0587 if (result < 0) {
0588 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
0589 goto err_poll;
0590 }
0591
0592 usb_sg_wait(&sgr);
0593 if (sgr.status < 0) {
0594 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
0595 goto err_poll;
0596 }
0597
0598 dev_dbg(sur40->dev, "image acquired\n");
0599
0600
0601 if (sur40->sequence == -1)
0602 return;
0603
0604
0605 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
0606 new_buf->vb.sequence = sur40->sequence++;
0607 new_buf->vb.field = V4L2_FIELD_NONE;
0608 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
0609 dev_dbg(sur40->dev, "buffer marked done\n");
0610 return;
0611
0612 err_poll:
0613 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0614 }
0615
0616
0617 static int sur40_input_setup_events(struct input_dev *input_dev)
0618 {
0619 int error;
0620
0621 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
0622 0, SENSOR_RES_X, 0, 0);
0623 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
0624 0, SENSOR_RES_Y, 0, 0);
0625
0626 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
0627 0, SENSOR_RES_X, 0, 0);
0628 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
0629 0, SENSOR_RES_Y, 0, 0);
0630
0631
0632
0633 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
0634 0, SENSOR_RES_X, 0, 0);
0635 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
0636 0, SENSOR_RES_Y, 0, 0);
0637
0638 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
0639
0640 error = input_mt_init_slots(input_dev, MAX_CONTACTS,
0641 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
0642 if (error) {
0643 dev_err(input_dev->dev.parent, "failed to set up slots\n");
0644 return error;
0645 }
0646
0647 return 0;
0648 }
0649
0650
0651 static int sur40_probe(struct usb_interface *interface,
0652 const struct usb_device_id *id)
0653 {
0654 struct usb_device *usbdev = interface_to_usbdev(interface);
0655 struct sur40_state *sur40;
0656 struct usb_host_interface *iface_desc;
0657 struct usb_endpoint_descriptor *endpoint;
0658 struct input_dev *input;
0659 int error;
0660
0661
0662 iface_desc = interface->cur_altsetting;
0663 if (iface_desc->desc.bInterfaceClass != 0xFF)
0664 return -ENODEV;
0665
0666 if (iface_desc->desc.bNumEndpoints < 5)
0667 return -ENODEV;
0668
0669
0670 endpoint = &iface_desc->endpoint[4].desc;
0671 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
0672 return -ENODEV;
0673
0674
0675 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
0676 if (!sur40)
0677 return -ENOMEM;
0678
0679 input = input_allocate_device();
0680 if (!input) {
0681 error = -ENOMEM;
0682 goto err_free_dev;
0683 }
0684
0685
0686 INIT_LIST_HEAD(&sur40->buf_list);
0687 spin_lock_init(&sur40->qlock);
0688 mutex_init(&sur40->lock);
0689
0690
0691 input->name = DRIVER_LONG;
0692 usb_to_input_id(usbdev, &input->id);
0693 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
0694 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
0695 input->phys = sur40->phys;
0696 input->dev.parent = &interface->dev;
0697
0698 input->open = sur40_open;
0699 input->close = sur40_close;
0700
0701 error = sur40_input_setup_events(input);
0702 if (error)
0703 goto err_free_input;
0704
0705 input_set_drvdata(input, sur40);
0706 error = input_setup_polling(input, sur40_poll);
0707 if (error) {
0708 dev_err(&interface->dev, "failed to set up polling");
0709 goto err_free_input;
0710 }
0711
0712 input_set_poll_interval(input, POLL_INTERVAL);
0713
0714 sur40->usbdev = usbdev;
0715 sur40->dev = &interface->dev;
0716 sur40->input = input;
0717
0718
0719 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
0720 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
0721 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
0722 if (!sur40->bulk_in_buffer) {
0723 dev_err(&interface->dev, "Unable to allocate input buffer.");
0724 error = -ENOMEM;
0725 goto err_free_input;
0726 }
0727
0728
0729 error = input_register_device(input);
0730 if (error) {
0731 dev_err(&interface->dev,
0732 "Unable to register polled input device.");
0733 goto err_free_buffer;
0734 }
0735
0736
0737 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
0738 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
0739 if (error) {
0740 dev_err(&interface->dev,
0741 "Unable to register video master device.");
0742 goto err_unreg_v4l2;
0743 }
0744
0745
0746 sur40->queue = sur40_queue;
0747 sur40->queue.drv_priv = sur40;
0748 sur40->queue.lock = &sur40->lock;
0749 sur40->queue.dev = sur40->dev;
0750
0751
0752 error = vb2_queue_init(&sur40->queue);
0753 if (error)
0754 goto err_unreg_v4l2;
0755
0756 sur40->pix_fmt = sur40_pix_format[0];
0757 sur40->vdev = sur40_video_device;
0758 sur40->vdev.v4l2_dev = &sur40->v4l2;
0759 sur40->vdev.lock = &sur40->lock;
0760 sur40->vdev.queue = &sur40->queue;
0761 video_set_drvdata(&sur40->vdev, sur40);
0762
0763
0764 v4l2_ctrl_handler_init(&sur40->hdl, 4);
0765 sur40->v4l2.ctrl_handler = &sur40->hdl;
0766 sur40->vsvideo = (SUR40_CONTRAST_DEF << 4) | SUR40_GAIN_DEF;
0767
0768 v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_BRIGHTNESS,
0769 SUR40_BRIGHTNESS_MIN, SUR40_BRIGHTNESS_MAX, 1, clamp(brightness,
0770 (uint)SUR40_BRIGHTNESS_MIN, (uint)SUR40_BRIGHTNESS_MAX));
0771
0772 v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_CONTRAST,
0773 SUR40_CONTRAST_MIN, SUR40_CONTRAST_MAX, 1, clamp(contrast,
0774 (uint)SUR40_CONTRAST_MIN, (uint)SUR40_CONTRAST_MAX));
0775
0776 v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_GAIN,
0777 SUR40_GAIN_MIN, SUR40_GAIN_MAX, 1, clamp(gain,
0778 (uint)SUR40_GAIN_MIN, (uint)SUR40_GAIN_MAX));
0779
0780 v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops,
0781 V4L2_CID_BACKLIGHT_COMPENSATION, SUR40_BACKLIGHT_MIN,
0782 SUR40_BACKLIGHT_MAX, 1, SUR40_BACKLIGHT_DEF);
0783
0784 v4l2_ctrl_handler_setup(&sur40->hdl);
0785
0786 if (sur40->hdl.error) {
0787 dev_err(&interface->dev,
0788 "Unable to register video controls.");
0789 v4l2_ctrl_handler_free(&sur40->hdl);
0790 error = sur40->hdl.error;
0791 goto err_unreg_v4l2;
0792 }
0793
0794 error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
0795 if (error) {
0796 dev_err(&interface->dev,
0797 "Unable to register video subdevice.");
0798 goto err_unreg_video;
0799 }
0800
0801
0802 usb_set_intfdata(interface, sur40);
0803 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
0804
0805 return 0;
0806
0807 err_unreg_video:
0808 video_unregister_device(&sur40->vdev);
0809 err_unreg_v4l2:
0810 v4l2_device_unregister(&sur40->v4l2);
0811 err_free_buffer:
0812 kfree(sur40->bulk_in_buffer);
0813 err_free_input:
0814 input_free_device(input);
0815 err_free_dev:
0816 kfree(sur40);
0817
0818 return error;
0819 }
0820
0821
0822 static void sur40_disconnect(struct usb_interface *interface)
0823 {
0824 struct sur40_state *sur40 = usb_get_intfdata(interface);
0825
0826 v4l2_ctrl_handler_free(&sur40->hdl);
0827 video_unregister_device(&sur40->vdev);
0828 v4l2_device_unregister(&sur40->v4l2);
0829
0830 input_unregister_device(sur40->input);
0831 kfree(sur40->bulk_in_buffer);
0832 kfree(sur40);
0833
0834 usb_set_intfdata(interface, NULL);
0835 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
0836 }
0837
0838
0839
0840
0841
0842
0843
0844
0845 static int sur40_queue_setup(struct vb2_queue *q,
0846 unsigned int *nbuffers, unsigned int *nplanes,
0847 unsigned int sizes[], struct device *alloc_devs[])
0848 {
0849 struct sur40_state *sur40 = vb2_get_drv_priv(q);
0850
0851 if (q->num_buffers + *nbuffers < 3)
0852 *nbuffers = 3 - q->num_buffers;
0853
0854 if (*nplanes)
0855 return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
0856
0857 *nplanes = 1;
0858 sizes[0] = sur40->pix_fmt.sizeimage;
0859
0860 return 0;
0861 }
0862
0863
0864
0865
0866
0867 static int sur40_buffer_prepare(struct vb2_buffer *vb)
0868 {
0869 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
0870 unsigned long size = sur40->pix_fmt.sizeimage;
0871
0872 if (vb2_plane_size(vb, 0) < size) {
0873 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
0874 vb2_plane_size(vb, 0), size);
0875 return -EINVAL;
0876 }
0877
0878 vb2_set_plane_payload(vb, 0, size);
0879 return 0;
0880 }
0881
0882
0883
0884
0885 static void sur40_buffer_queue(struct vb2_buffer *vb)
0886 {
0887 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
0888 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
0889
0890 spin_lock(&sur40->qlock);
0891 list_add_tail(&buf->list, &sur40->buf_list);
0892 spin_unlock(&sur40->qlock);
0893 }
0894
0895 static void return_all_buffers(struct sur40_state *sur40,
0896 enum vb2_buffer_state state)
0897 {
0898 struct sur40_buffer *buf, *node;
0899
0900 spin_lock(&sur40->qlock);
0901 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
0902 vb2_buffer_done(&buf->vb.vb2_buf, state);
0903 list_del(&buf->list);
0904 }
0905 spin_unlock(&sur40->qlock);
0906 }
0907
0908
0909
0910
0911
0912
0913
0914 static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
0915 {
0916 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
0917
0918 sur40->sequence = 0;
0919 return 0;
0920 }
0921
0922
0923
0924
0925
0926 static void sur40_stop_streaming(struct vb2_queue *vq)
0927 {
0928 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
0929 vb2_wait_for_all_buffers(vq);
0930 sur40->sequence = -1;
0931
0932
0933 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
0934 }
0935
0936
0937 static int sur40_vidioc_querycap(struct file *file, void *priv,
0938 struct v4l2_capability *cap)
0939 {
0940 struct sur40_state *sur40 = video_drvdata(file);
0941
0942 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
0943 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
0944 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
0945 return 0;
0946 }
0947
0948 static int sur40_vidioc_enum_input(struct file *file, void *priv,
0949 struct v4l2_input *i)
0950 {
0951 if (i->index != 0)
0952 return -EINVAL;
0953 i->type = V4L2_INPUT_TYPE_TOUCH;
0954 i->std = V4L2_STD_UNKNOWN;
0955 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
0956 i->capabilities = 0;
0957 return 0;
0958 }
0959
0960 static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
0961 {
0962 return (i == 0) ? 0 : -EINVAL;
0963 }
0964
0965 static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
0966 {
0967 *i = 0;
0968 return 0;
0969 }
0970
0971 static int sur40_vidioc_try_fmt(struct file *file, void *priv,
0972 struct v4l2_format *f)
0973 {
0974 switch (f->fmt.pix.pixelformat) {
0975 case V4L2_PIX_FMT_GREY:
0976 f->fmt.pix = sur40_pix_format[1];
0977 break;
0978
0979 default:
0980 f->fmt.pix = sur40_pix_format[0];
0981 break;
0982 }
0983
0984 return 0;
0985 }
0986
0987 static int sur40_vidioc_s_fmt(struct file *file, void *priv,
0988 struct v4l2_format *f)
0989 {
0990 struct sur40_state *sur40 = video_drvdata(file);
0991
0992 switch (f->fmt.pix.pixelformat) {
0993 case V4L2_PIX_FMT_GREY:
0994 sur40->pix_fmt = sur40_pix_format[1];
0995 break;
0996
0997 default:
0998 sur40->pix_fmt = sur40_pix_format[0];
0999 break;
1000 }
1001
1002 f->fmt.pix = sur40->pix_fmt;
1003 return 0;
1004 }
1005
1006 static int sur40_vidioc_g_fmt(struct file *file, void *priv,
1007 struct v4l2_format *f)
1008 {
1009 struct sur40_state *sur40 = video_drvdata(file);
1010
1011 f->fmt.pix = sur40->pix_fmt;
1012 return 0;
1013 }
1014
1015 static int sur40_s_ctrl(struct v4l2_ctrl *ctrl)
1016 {
1017 struct sur40_state *sur40 = container_of(ctrl->handler,
1018 struct sur40_state, hdl);
1019 u8 value = sur40->vsvideo;
1020
1021 switch (ctrl->id) {
1022 case V4L2_CID_BRIGHTNESS:
1023 sur40_set_irlevel(sur40, ctrl->val);
1024 break;
1025 case V4L2_CID_CONTRAST:
1026 value = (value & 0x0f) | (ctrl->val << 4);
1027 sur40_set_vsvideo(sur40, value);
1028 break;
1029 case V4L2_CID_GAIN:
1030 value = (value & 0xf0) | (ctrl->val);
1031 sur40_set_vsvideo(sur40, value);
1032 break;
1033 case V4L2_CID_BACKLIGHT_COMPENSATION:
1034 sur40_set_preprocessor(sur40, ctrl->val);
1035 break;
1036 }
1037 return 0;
1038 }
1039
1040 static int sur40_ioctl_parm(struct file *file, void *priv,
1041 struct v4l2_streamparm *p)
1042 {
1043 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044 return -EINVAL;
1045
1046 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1047 p->parm.capture.timeperframe.numerator = 1;
1048 p->parm.capture.timeperframe.denominator = 60;
1049 p->parm.capture.readbuffers = 3;
1050 return 0;
1051 }
1052
1053 static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
1054 struct v4l2_fmtdesc *f)
1055 {
1056 if (f->index >= ARRAY_SIZE(sur40_pix_format))
1057 return -EINVAL;
1058
1059 f->pixelformat = sur40_pix_format[f->index].pixelformat;
1060 f->flags = 0;
1061 return 0;
1062 }
1063
1064 static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
1065 struct v4l2_frmsizeenum *f)
1066 {
1067 struct sur40_state *sur40 = video_drvdata(file);
1068
1069 if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
1070 && (f->pixel_format != V4L2_PIX_FMT_GREY)))
1071 return -EINVAL;
1072
1073 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1074 f->discrete.width = sur40->pix_fmt.width;
1075 f->discrete.height = sur40->pix_fmt.height;
1076 return 0;
1077 }
1078
1079 static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
1080 struct v4l2_frmivalenum *f)
1081 {
1082 struct sur40_state *sur40 = video_drvdata(file);
1083
1084 if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
1085 && (f->pixel_format != V4L2_PIX_FMT_GREY))
1086 || (f->width != sur40->pix_fmt.width)
1087 || (f->height != sur40->pix_fmt.height))
1088 return -EINVAL;
1089
1090 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1091 f->discrete.denominator = 60;
1092 f->discrete.numerator = 1;
1093 return 0;
1094 }
1095
1096
1097 static const struct usb_device_id sur40_table[] = {
1098 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) },
1099 { }
1100 };
1101 MODULE_DEVICE_TABLE(usb, sur40_table);
1102
1103
1104 static const struct vb2_ops sur40_queue_ops = {
1105 .queue_setup = sur40_queue_setup,
1106 .buf_prepare = sur40_buffer_prepare,
1107 .buf_queue = sur40_buffer_queue,
1108 .start_streaming = sur40_start_streaming,
1109 .stop_streaming = sur40_stop_streaming,
1110 .wait_prepare = vb2_ops_wait_prepare,
1111 .wait_finish = vb2_ops_wait_finish,
1112 };
1113
1114 static const struct vb2_queue sur40_queue = {
1115 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1116
1117
1118
1119
1120
1121 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
1122 .buf_struct_size = sizeof(struct sur40_buffer),
1123 .ops = &sur40_queue_ops,
1124 .mem_ops = &vb2_dma_sg_memops,
1125 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
1126 .min_buffers_needed = 3,
1127 };
1128
1129 static const struct v4l2_file_operations sur40_video_fops = {
1130 .owner = THIS_MODULE,
1131 .open = v4l2_fh_open,
1132 .release = vb2_fop_release,
1133 .unlocked_ioctl = video_ioctl2,
1134 .read = vb2_fop_read,
1135 .mmap = vb2_fop_mmap,
1136 .poll = vb2_fop_poll,
1137 };
1138
1139 static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
1140
1141 .vidioc_querycap = sur40_vidioc_querycap,
1142
1143 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
1144 .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
1145 .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
1146 .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
1147
1148 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
1149 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
1150
1151 .vidioc_g_parm = sur40_ioctl_parm,
1152 .vidioc_s_parm = sur40_ioctl_parm,
1153
1154 .vidioc_enum_input = sur40_vidioc_enum_input,
1155 .vidioc_g_input = sur40_vidioc_g_input,
1156 .vidioc_s_input = sur40_vidioc_s_input,
1157
1158 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1159 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1160 .vidioc_querybuf = vb2_ioctl_querybuf,
1161 .vidioc_qbuf = vb2_ioctl_qbuf,
1162 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1163 .vidioc_expbuf = vb2_ioctl_expbuf,
1164
1165 .vidioc_streamon = vb2_ioctl_streamon,
1166 .vidioc_streamoff = vb2_ioctl_streamoff,
1167 };
1168
1169 static const struct video_device sur40_video_device = {
1170 .name = DRIVER_LONG,
1171 .fops = &sur40_video_fops,
1172 .ioctl_ops = &sur40_video_ioctl_ops,
1173 .release = video_device_release_empty,
1174 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
1175 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
1176 };
1177
1178
1179 static struct usb_driver sur40_driver = {
1180 .name = DRIVER_SHORT,
1181 .probe = sur40_probe,
1182 .disconnect = sur40_disconnect,
1183 .id_table = sur40_table,
1184 };
1185
1186 module_usb_driver(sur40_driver);
1187
1188 MODULE_AUTHOR(DRIVER_AUTHOR);
1189 MODULE_DESCRIPTION(DRIVER_DESC);
1190 MODULE_LICENSE("GPL");