0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
0017
0018 #include <linux/errno.h>
0019 #include <linux/init.h>
0020 #include <linux/kernel.h>
0021 #include <linux/ktime.h>
0022 #include <linux/module.h>
0023 #include <linux/slab.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/ratelimit.h>
0026
0027 #include <linux/input.h>
0028 #include <linux/usb.h>
0029 #include <linux/usb/input.h>
0030 #include <media/rc-core.h>
0031
0032 #include <linux/timer.h>
0033
0034 #define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
0035 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
0036 #define MOD_NAME "imon"
0037 #define MOD_VERSION "0.9.4"
0038
0039 #define DISPLAY_MINOR_BASE 144
0040 #define DEVICE_NAME "lcd%d"
0041
0042 #define BUF_CHUNK_SIZE 8
0043 #define BUF_SIZE 128
0044
0045 #define BIT_DURATION 250
0046
0047 #define IMON_CLOCK_ENABLE_PACKETS 2
0048
0049
0050
0051
0052 static int imon_probe(struct usb_interface *interface,
0053 const struct usb_device_id *id);
0054 static void imon_disconnect(struct usb_interface *interface);
0055 static void usb_rx_callback_intf0(struct urb *urb);
0056 static void usb_rx_callback_intf1(struct urb *urb);
0057 static void usb_tx_callback(struct urb *urb);
0058
0059
0060 static int imon_resume(struct usb_interface *intf);
0061 static int imon_suspend(struct usb_interface *intf, pm_message_t message);
0062
0063
0064 static int display_open(struct inode *inode, struct file *file);
0065 static int display_close(struct inode *inode, struct file *file);
0066
0067
0068 static ssize_t vfd_write(struct file *file, const char __user *buf,
0069 size_t n_bytes, loff_t *pos);
0070
0071
0072 static ssize_t lcd_write(struct file *file, const char __user *buf,
0073 size_t n_bytes, loff_t *pos);
0074
0075
0076
0077 struct imon_panel_key_table {
0078 u64 hw_code;
0079 u32 keycode;
0080 };
0081
0082 struct imon_usb_dev_descr {
0083 __u16 flags;
0084 #define IMON_NO_FLAGS 0
0085 #define IMON_NEED_20MS_PKT_DELAY 1
0086 #define IMON_SUPPRESS_REPEATED_KEYS 2
0087 struct imon_panel_key_table key_table[];
0088 };
0089
0090 struct imon_context {
0091 struct device *dev;
0092
0093 struct usb_device *usbdev_intf0;
0094 struct usb_device *usbdev_intf1;
0095
0096 bool display_supported;
0097 bool display_isopen;
0098 bool rf_device;
0099 bool rf_isassociating;
0100 bool dev_present_intf0;
0101 bool dev_present_intf1;
0102
0103 struct mutex lock;
0104 wait_queue_head_t remove_ok;
0105
0106 struct usb_endpoint_descriptor *rx_endpoint_intf0;
0107 struct usb_endpoint_descriptor *rx_endpoint_intf1;
0108 struct usb_endpoint_descriptor *tx_endpoint;
0109 struct urb *rx_urb_intf0;
0110 struct urb *rx_urb_intf1;
0111 struct urb *tx_urb;
0112 bool tx_control;
0113 unsigned char usb_rx_buf[8];
0114 unsigned char usb_tx_buf[8];
0115 unsigned int send_packet_delay;
0116
0117 struct tx_t {
0118 unsigned char data_buf[35];
0119 struct completion finished;
0120 bool busy;
0121 int status;
0122 } tx;
0123
0124 u16 vendor;
0125 u16 product;
0126
0127 struct rc_dev *rdev;
0128 struct input_dev *idev;
0129 struct input_dev *touch;
0130
0131 spinlock_t kc_lock;
0132 u32 kc;
0133 u32 last_keycode;
0134 u32 rc_scancode;
0135 u8 rc_toggle;
0136 u64 rc_proto;
0137 bool release_code;
0138
0139 u8 display_type;
0140 bool pad_mouse;
0141
0142 char name_rdev[128];
0143 char phys_rdev[64];
0144
0145 char name_idev[128];
0146 char phys_idev[64];
0147
0148 char name_touch[128];
0149 char phys_touch[64];
0150 struct timer_list ttimer;
0151 int touch_x;
0152 int touch_y;
0153 const struct imon_usb_dev_descr *dev_descr;
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 refcount_t users;
0164
0165
0166
0167
0168 bool disconnected;
0169
0170
0171
0172
0173 struct rcu_head rcu;
0174 };
0175
0176 #define TOUCH_TIMEOUT (HZ/30)
0177
0178
0179 static const struct file_operations vfd_fops = {
0180 .owner = THIS_MODULE,
0181 .open = display_open,
0182 .write = vfd_write,
0183 .release = display_close,
0184 .llseek = noop_llseek,
0185 };
0186
0187
0188 static const struct file_operations lcd_fops = {
0189 .owner = THIS_MODULE,
0190 .open = display_open,
0191 .write = lcd_write,
0192 .release = display_close,
0193 .llseek = noop_llseek,
0194 };
0195
0196 enum {
0197 IMON_DISPLAY_TYPE_AUTO = 0,
0198 IMON_DISPLAY_TYPE_VFD = 1,
0199 IMON_DISPLAY_TYPE_LCD = 2,
0200 IMON_DISPLAY_TYPE_VGA = 3,
0201 IMON_DISPLAY_TYPE_NONE = 4,
0202 };
0203
0204 enum {
0205 IMON_KEY_IMON = 0,
0206 IMON_KEY_MCE = 1,
0207 IMON_KEY_PANEL = 2,
0208 };
0209
0210 static struct usb_class_driver imon_vfd_class = {
0211 .name = DEVICE_NAME,
0212 .fops = &vfd_fops,
0213 .minor_base = DISPLAY_MINOR_BASE,
0214 };
0215
0216 static struct usb_class_driver imon_lcd_class = {
0217 .name = DEVICE_NAME,
0218 .fops = &lcd_fops,
0219 .minor_base = DISPLAY_MINOR_BASE,
0220 };
0221
0222
0223 static const struct imon_usb_dev_descr imon_default_table = {
0224 .flags = IMON_NO_FLAGS,
0225 .key_table = {
0226 { 0x000000000f00ffeell, KEY_MEDIA },
0227 { 0x000000001200ffeell, KEY_UP },
0228 { 0x000000001300ffeell, KEY_DOWN },
0229 { 0x000000001400ffeell, KEY_LEFT },
0230 { 0x000000001500ffeell, KEY_RIGHT },
0231 { 0x000000001600ffeell, KEY_ENTER },
0232 { 0x000000001700ffeell, KEY_ESC },
0233 { 0x000000001f00ffeell, KEY_AUDIO },
0234 { 0x000000002000ffeell, KEY_VIDEO },
0235 { 0x000000002100ffeell, KEY_CAMERA },
0236 { 0x000000002700ffeell, KEY_DVD },
0237 { 0x000000002300ffeell, KEY_TV },
0238 { 0x000000002b00ffeell, KEY_EXIT },
0239 { 0x000000002c00ffeell, KEY_SELECT },
0240 { 0x000000002d00ffeell, KEY_MENU },
0241 { 0x000000000500ffeell, KEY_PREVIOUS },
0242 { 0x000000000700ffeell, KEY_REWIND },
0243 { 0x000000000400ffeell, KEY_STOP },
0244 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
0245 { 0x000000000800ffeell, KEY_FASTFORWARD },
0246 { 0x000000000600ffeell, KEY_NEXT },
0247 { 0x000000010000ffeell, KEY_RIGHT },
0248 { 0x000001000000ffeell, KEY_LEFT },
0249 { 0x000000003d00ffeell, KEY_SELECT },
0250 { 0x000100000000ffeell, KEY_VOLUMEUP },
0251 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
0252 { 0x000000000100ffeell, KEY_MUTE },
0253
0254 { 0x00010000ffffffeell, KEY_VOLUMEUP },
0255 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
0256 { 0x00000001ffffffeell, KEY_MUTE },
0257 { 0x0000000fffffffeell, KEY_MEDIA },
0258 { 0x00000012ffffffeell, KEY_UP },
0259 { 0x00000013ffffffeell, KEY_DOWN },
0260 { 0x00000014ffffffeell, KEY_LEFT },
0261 { 0x00000015ffffffeell, KEY_RIGHT },
0262 { 0x00000016ffffffeell, KEY_ENTER },
0263 { 0x00000017ffffffeell, KEY_ESC },
0264
0265 { 0x000100ffffffffeell, KEY_VOLUMEUP },
0266 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
0267 { 0x000008ffffffffeell, KEY_MUTE },
0268 { 0, KEY_RESERVED },
0269 }
0270 };
0271
0272 static const struct imon_usb_dev_descr imon_OEM_VFD = {
0273 .flags = IMON_NEED_20MS_PKT_DELAY,
0274 .key_table = {
0275 { 0x000000000f00ffeell, KEY_MEDIA },
0276 { 0x000000001200ffeell, KEY_UP },
0277 { 0x000000001300ffeell, KEY_DOWN },
0278 { 0x000000001400ffeell, KEY_LEFT },
0279 { 0x000000001500ffeell, KEY_RIGHT },
0280 { 0x000000001600ffeell, KEY_ENTER },
0281 { 0x000000001700ffeell, KEY_ESC },
0282 { 0x000000001f00ffeell, KEY_AUDIO },
0283 { 0x000000002b00ffeell, KEY_EXIT },
0284 { 0x000000002c00ffeell, KEY_SELECT },
0285 { 0x000000002d00ffeell, KEY_MENU },
0286 { 0x000000000500ffeell, KEY_PREVIOUS },
0287 { 0x000000000700ffeell, KEY_REWIND },
0288 { 0x000000000400ffeell, KEY_STOP },
0289 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
0290 { 0x000000000800ffeell, KEY_FASTFORWARD },
0291 { 0x000000000600ffeell, KEY_NEXT },
0292 { 0x000000010000ffeell, KEY_RIGHT },
0293 { 0x000001000000ffeell, KEY_LEFT },
0294 { 0x000000003d00ffeell, KEY_SELECT },
0295 { 0x000100000000ffeell, KEY_VOLUMEUP },
0296 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
0297 { 0x000000000100ffeell, KEY_MUTE },
0298
0299 { 0x00010000ffffffeell, KEY_VOLUMEUP },
0300 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
0301 { 0x00000001ffffffeell, KEY_MUTE },
0302 { 0x0000000fffffffeell, KEY_MEDIA },
0303 { 0x00000012ffffffeell, KEY_UP },
0304 { 0x00000013ffffffeell, KEY_DOWN },
0305 { 0x00000014ffffffeell, KEY_LEFT },
0306 { 0x00000015ffffffeell, KEY_RIGHT },
0307 { 0x00000016ffffffeell, KEY_ENTER },
0308 { 0x00000017ffffffeell, KEY_ESC },
0309
0310 { 0x000100ffffffffeell, KEY_VOLUMEUP },
0311 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
0312 { 0x000008ffffffffeell, KEY_MUTE },
0313 { 0, KEY_RESERVED },
0314 }
0315 };
0316
0317
0318 static const struct imon_usb_dev_descr imon_DH102 = {
0319 .flags = IMON_NO_FLAGS,
0320 .key_table = {
0321 { 0x000100000000ffeell, KEY_VOLUMEUP },
0322 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
0323 { 0x000000010000ffeell, KEY_MUTE },
0324 { 0x0000000f0000ffeell, KEY_MEDIA },
0325 { 0x000000120000ffeell, KEY_UP },
0326 { 0x000000130000ffeell, KEY_DOWN },
0327 { 0x000000140000ffeell, KEY_LEFT },
0328 { 0x000000150000ffeell, KEY_RIGHT },
0329 { 0x000000160000ffeell, KEY_ENTER },
0330 { 0x000000170000ffeell, KEY_ESC },
0331 { 0x0000002b0000ffeell, KEY_EXIT },
0332 { 0x0000002c0000ffeell, KEY_SELECT },
0333 { 0x0000002d0000ffeell, KEY_MENU },
0334 { 0, KEY_RESERVED }
0335 }
0336 };
0337
0338
0339 static const struct imon_usb_dev_descr ultrabay_table = {
0340 .flags = IMON_SUPPRESS_REPEATED_KEYS,
0341 .key_table = {
0342 { 0x0000000f0000ffeell, KEY_MEDIA },
0343 { 0x000000000100ffeell, KEY_UP },
0344 { 0x000000000001ffeell, KEY_DOWN },
0345 { 0x000000160000ffeell, KEY_ENTER },
0346 { 0x0000001f0000ffeell, KEY_AUDIO },
0347 { 0x000000200000ffeell, KEY_VIDEO },
0348 { 0x000000210000ffeell, KEY_CAMERA },
0349 { 0x000000270000ffeell, KEY_DVD },
0350 { 0x000000230000ffeell, KEY_TV },
0351 { 0x000000050000ffeell, KEY_PREVIOUS },
0352 { 0x000000070000ffeell, KEY_REWIND },
0353 { 0x000000040000ffeell, KEY_STOP },
0354 { 0x000000020000ffeell, KEY_PLAYPAUSE },
0355 { 0x000000080000ffeell, KEY_FASTFORWARD },
0356 { 0x000000060000ffeell, KEY_NEXT },
0357 { 0x000100000000ffeell, KEY_VOLUMEUP },
0358 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
0359 { 0x000000010000ffeell, KEY_MUTE },
0360 { 0, KEY_RESERVED },
0361 }
0362 };
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375 static const struct usb_device_id imon_usb_id_table[] = {
0376
0377
0378
0379
0380
0381
0382 { USB_DEVICE(0x15c2, 0xffdc),
0383 .driver_info = (unsigned long)&imon_default_table },
0384
0385
0386
0387
0388
0389
0390
0391 { USB_DEVICE(0x15c2, 0x0034),
0392 .driver_info = (unsigned long)&imon_DH102 },
0393
0394 { USB_DEVICE(0x15c2, 0x0035),
0395 .driver_info = (unsigned long)&imon_default_table},
0396
0397 { USB_DEVICE(0x15c2, 0x0036),
0398 .driver_info = (unsigned long)&imon_OEM_VFD },
0399
0400 { USB_DEVICE(0x15c2, 0x0037),
0401 .driver_info = (unsigned long)&imon_default_table},
0402
0403 { USB_DEVICE(0x15c2, 0x0038),
0404 .driver_info = (unsigned long)&imon_default_table},
0405
0406 { USB_DEVICE(0x15c2, 0x0039),
0407 .driver_info = (unsigned long)&imon_default_table},
0408
0409 { USB_DEVICE(0x15c2, 0x003a),
0410 .driver_info = (unsigned long)&imon_default_table},
0411
0412 { USB_DEVICE(0x15c2, 0x003b),
0413 .driver_info = (unsigned long)&imon_default_table},
0414
0415 { USB_DEVICE(0x15c2, 0x003c),
0416 .driver_info = (unsigned long)&imon_default_table},
0417
0418 { USB_DEVICE(0x15c2, 0x003d),
0419 .driver_info = (unsigned long)&imon_default_table},
0420
0421 { USB_DEVICE(0x15c2, 0x003e),
0422 .driver_info = (unsigned long)&imon_default_table},
0423
0424 { USB_DEVICE(0x15c2, 0x003f),
0425 .driver_info = (unsigned long)&imon_default_table},
0426
0427 { USB_DEVICE(0x15c2, 0x0040),
0428 .driver_info = (unsigned long)&imon_default_table},
0429
0430 { USB_DEVICE(0x15c2, 0x0041),
0431 .driver_info = (unsigned long)&imon_default_table},
0432
0433 { USB_DEVICE(0x15c2, 0x0042),
0434 .driver_info = (unsigned long)&imon_default_table},
0435
0436 { USB_DEVICE(0x15c2, 0x0043),
0437 .driver_info = (unsigned long)&imon_default_table},
0438
0439 { USB_DEVICE(0x15c2, 0x0044),
0440 .driver_info = (unsigned long)&imon_default_table},
0441
0442 { USB_DEVICE(0x15c2, 0x0045),
0443 .driver_info = (unsigned long)&imon_default_table},
0444
0445 { USB_DEVICE(0x15c2, 0x0046),
0446 .driver_info = (unsigned long)&imon_default_table},
0447 {}
0448 };
0449
0450
0451 static struct usb_driver imon_driver = {
0452 .name = MOD_NAME,
0453 .probe = imon_probe,
0454 .disconnect = imon_disconnect,
0455 .suspend = imon_suspend,
0456 .resume = imon_resume,
0457 .id_table = imon_usb_id_table,
0458 };
0459
0460
0461 MODULE_AUTHOR(MOD_AUTHOR);
0462 MODULE_DESCRIPTION(MOD_DESC);
0463 MODULE_VERSION(MOD_VERSION);
0464 MODULE_LICENSE("GPL");
0465 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
0466
0467 static bool debug;
0468 module_param(debug, bool, S_IRUGO | S_IWUSR);
0469 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
0470
0471
0472 static int display_type;
0473 module_param(display_type, int, S_IRUGO);
0474 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, 1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
0475
0476 static int pad_stabilize = 1;
0477 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
0478 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD presses in arrow key mode. 0=disable, 1=enable (default).");
0479
0480
0481
0482
0483
0484 static bool nomouse;
0485 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
0486 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is open. 0=don't disable, 1=disable. (default: don't disable)");
0487
0488
0489 static int pad_thresh;
0490 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
0491 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an arrow key in kbd mode (default: 28)");
0492
0493
0494 static void free_imon_context(struct imon_context *ictx)
0495 {
0496 struct device *dev = ictx->dev;
0497
0498 usb_free_urb(ictx->tx_urb);
0499 WARN_ON(ictx->dev_present_intf0);
0500 usb_free_urb(ictx->rx_urb_intf0);
0501 WARN_ON(ictx->dev_present_intf1);
0502 usb_free_urb(ictx->rx_urb_intf1);
0503 kfree_rcu(ictx, rcu);
0504
0505 dev_dbg(dev, "%s: iMON context freed\n", __func__);
0506 }
0507
0508
0509
0510
0511
0512 static int display_open(struct inode *inode, struct file *file)
0513 {
0514 struct usb_interface *interface;
0515 struct imon_context *ictx = NULL;
0516 int subminor;
0517 int retval = 0;
0518
0519 subminor = iminor(inode);
0520 interface = usb_find_interface(&imon_driver, subminor);
0521 if (!interface) {
0522 pr_err("could not find interface for minor %d\n", subminor);
0523 retval = -ENODEV;
0524 goto exit;
0525 }
0526
0527 rcu_read_lock();
0528 ictx = usb_get_intfdata(interface);
0529 if (!ictx || ictx->disconnected || !refcount_inc_not_zero(&ictx->users)) {
0530 rcu_read_unlock();
0531 pr_err("no context found for minor %d\n", subminor);
0532 retval = -ENODEV;
0533 goto exit;
0534 }
0535 rcu_read_unlock();
0536
0537 mutex_lock(&ictx->lock);
0538
0539 if (!ictx->display_supported) {
0540 pr_err("display not supported by device\n");
0541 retval = -ENODEV;
0542 } else if (ictx->display_isopen) {
0543 pr_err("display port is already open\n");
0544 retval = -EBUSY;
0545 } else {
0546 ictx->display_isopen = true;
0547 file->private_data = ictx;
0548 dev_dbg(ictx->dev, "display port opened\n");
0549 }
0550
0551 mutex_unlock(&ictx->lock);
0552
0553 if (retval && refcount_dec_and_test(&ictx->users))
0554 free_imon_context(ictx);
0555
0556 exit:
0557 return retval;
0558 }
0559
0560
0561
0562
0563
0564 static int display_close(struct inode *inode, struct file *file)
0565 {
0566 struct imon_context *ictx = file->private_data;
0567 int retval = 0;
0568
0569 mutex_lock(&ictx->lock);
0570
0571 if (!ictx->display_supported) {
0572 pr_err("display not supported by device\n");
0573 retval = -ENODEV;
0574 } else if (!ictx->display_isopen) {
0575 pr_err("display is not open\n");
0576 retval = -EIO;
0577 } else {
0578 ictx->display_isopen = false;
0579 dev_dbg(ictx->dev, "display port closed\n");
0580 }
0581
0582 mutex_unlock(&ictx->lock);
0583 if (refcount_dec_and_test(&ictx->users))
0584 free_imon_context(ictx);
0585 return retval;
0586 }
0587
0588
0589
0590
0591
0592
0593 static int send_packet(struct imon_context *ictx)
0594 {
0595 unsigned int pipe;
0596 unsigned long timeout;
0597 int interval = 0;
0598 int retval = 0;
0599 struct usb_ctrlrequest *control_req = NULL;
0600
0601
0602 if (!ictx->tx_control) {
0603 pipe = usb_sndintpipe(ictx->usbdev_intf0,
0604 ictx->tx_endpoint->bEndpointAddress);
0605 interval = ictx->tx_endpoint->bInterval;
0606
0607 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
0608 ictx->usb_tx_buf,
0609 sizeof(ictx->usb_tx_buf),
0610 usb_tx_callback, ictx, interval);
0611
0612 ictx->tx_urb->actual_length = 0;
0613 } else {
0614
0615 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
0616 if (control_req == NULL)
0617 return -ENOMEM;
0618
0619
0620 control_req->bRequestType = 0x21;
0621 control_req->bRequest = 0x09;
0622 control_req->wValue = cpu_to_le16(0x0200);
0623 control_req->wIndex = cpu_to_le16(0x0001);
0624 control_req->wLength = cpu_to_le16(0x0008);
0625
0626
0627 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
0628
0629
0630 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
0631 pipe, (unsigned char *)control_req,
0632 ictx->usb_tx_buf,
0633 sizeof(ictx->usb_tx_buf),
0634 usb_tx_callback, ictx);
0635 ictx->tx_urb->actual_length = 0;
0636 }
0637
0638 reinit_completion(&ictx->tx.finished);
0639 ictx->tx.busy = true;
0640 smp_rmb();
0641
0642 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
0643 if (retval) {
0644 ictx->tx.busy = false;
0645 smp_rmb();
0646 pr_err_ratelimited("error submitting urb(%d)\n", retval);
0647 } else {
0648
0649 mutex_unlock(&ictx->lock);
0650 retval = wait_for_completion_interruptible(
0651 &ictx->tx.finished);
0652 if (retval) {
0653 usb_kill_urb(ictx->tx_urb);
0654 pr_err_ratelimited("task interrupted\n");
0655 }
0656 mutex_lock(&ictx->lock);
0657
0658 retval = ictx->tx.status;
0659 if (retval)
0660 pr_err_ratelimited("packet tx failed (%d)\n", retval);
0661 }
0662
0663 kfree(control_req);
0664
0665
0666
0667
0668
0669
0670 timeout = msecs_to_jiffies(ictx->send_packet_delay);
0671 set_current_state(TASK_INTERRUPTIBLE);
0672 schedule_timeout(timeout);
0673
0674 return retval;
0675 }
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685 static int send_associate_24g(struct imon_context *ictx)
0686 {
0687 int retval;
0688 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
0689 0x00, 0x00, 0x00, 0x20 };
0690
0691 if (!ictx) {
0692 pr_err("no context for device\n");
0693 return -ENODEV;
0694 }
0695
0696 if (!ictx->dev_present_intf0) {
0697 pr_err("no iMON device present\n");
0698 return -ENODEV;
0699 }
0700
0701 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
0702 retval = send_packet(ictx);
0703
0704 return retval;
0705 }
0706
0707
0708
0709
0710
0711
0712
0713
0714 static int send_set_imon_clock(struct imon_context *ictx,
0715 unsigned int year, unsigned int month,
0716 unsigned int day, unsigned int dow,
0717 unsigned int hour, unsigned int minute,
0718 unsigned int second)
0719 {
0720 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
0721 int retval = 0;
0722 int i;
0723
0724 if (!ictx) {
0725 pr_err("no context for device\n");
0726 return -ENODEV;
0727 }
0728
0729 switch (ictx->display_type) {
0730 case IMON_DISPLAY_TYPE_LCD:
0731 clock_enable_pkt[0][0] = 0x80;
0732 clock_enable_pkt[0][1] = year;
0733 clock_enable_pkt[0][2] = month-1;
0734 clock_enable_pkt[0][3] = day;
0735 clock_enable_pkt[0][4] = hour;
0736 clock_enable_pkt[0][5] = minute;
0737 clock_enable_pkt[0][6] = second;
0738
0739 clock_enable_pkt[1][0] = 0x80;
0740 clock_enable_pkt[1][1] = 0;
0741 clock_enable_pkt[1][2] = 0;
0742 clock_enable_pkt[1][3] = 0;
0743 clock_enable_pkt[1][4] = 0;
0744 clock_enable_pkt[1][5] = 0;
0745 clock_enable_pkt[1][6] = 0;
0746
0747 if (ictx->product == 0xffdc) {
0748 clock_enable_pkt[0][7] = 0x50;
0749 clock_enable_pkt[1][7] = 0x51;
0750 } else {
0751 clock_enable_pkt[0][7] = 0x88;
0752 clock_enable_pkt[1][7] = 0x8a;
0753 }
0754
0755 break;
0756
0757 case IMON_DISPLAY_TYPE_VFD:
0758 clock_enable_pkt[0][0] = year;
0759 clock_enable_pkt[0][1] = month-1;
0760 clock_enable_pkt[0][2] = day;
0761 clock_enable_pkt[0][3] = dow;
0762 clock_enable_pkt[0][4] = hour;
0763 clock_enable_pkt[0][5] = minute;
0764 clock_enable_pkt[0][6] = second;
0765 clock_enable_pkt[0][7] = 0x40;
0766
0767 clock_enable_pkt[1][0] = 0;
0768 clock_enable_pkt[1][1] = 0;
0769 clock_enable_pkt[1][2] = 1;
0770 clock_enable_pkt[1][3] = 0;
0771 clock_enable_pkt[1][4] = 0;
0772 clock_enable_pkt[1][5] = 0;
0773 clock_enable_pkt[1][6] = 0;
0774 clock_enable_pkt[1][7] = 0x42;
0775
0776 break;
0777
0778 default:
0779 return -ENODEV;
0780 }
0781
0782 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
0783 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
0784 retval = send_packet(ictx);
0785 if (retval) {
0786 pr_err("send_packet failed for packet %d\n", i);
0787 break;
0788 }
0789 }
0790
0791 return retval;
0792 }
0793
0794
0795
0796
0797 static ssize_t associate_remote_show(struct device *d,
0798 struct device_attribute *attr,
0799 char *buf)
0800 {
0801 struct imon_context *ictx = dev_get_drvdata(d);
0802
0803 if (!ictx)
0804 return -ENODEV;
0805
0806 mutex_lock(&ictx->lock);
0807 if (ictx->rf_isassociating)
0808 strscpy(buf, "associating\n", PAGE_SIZE);
0809 else
0810 strscpy(buf, "closed\n", PAGE_SIZE);
0811
0812 dev_info(d, "Visit https://www.lirc.org/html/imon-24g.html for instructions on how to associate your iMON 2.4G DT/LT remote\n");
0813 mutex_unlock(&ictx->lock);
0814 return strlen(buf);
0815 }
0816
0817 static ssize_t associate_remote_store(struct device *d,
0818 struct device_attribute *attr,
0819 const char *buf, size_t count)
0820 {
0821 struct imon_context *ictx;
0822
0823 ictx = dev_get_drvdata(d);
0824
0825 if (!ictx)
0826 return -ENODEV;
0827
0828 mutex_lock(&ictx->lock);
0829 ictx->rf_isassociating = true;
0830 send_associate_24g(ictx);
0831 mutex_unlock(&ictx->lock);
0832
0833 return count;
0834 }
0835
0836
0837
0838
0839 static ssize_t imon_clock_show(struct device *d,
0840 struct device_attribute *attr, char *buf)
0841 {
0842 struct imon_context *ictx = dev_get_drvdata(d);
0843 size_t len;
0844
0845 if (!ictx)
0846 return -ENODEV;
0847
0848 mutex_lock(&ictx->lock);
0849
0850 if (!ictx->display_supported) {
0851 len = snprintf(buf, PAGE_SIZE, "Not supported.");
0852 } else {
0853 len = snprintf(buf, PAGE_SIZE,
0854 "To set the clock on your iMON display:\n"
0855 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
0856 "%s", ictx->display_isopen ?
0857 "\nNOTE: imon device must be closed\n" : "");
0858 }
0859
0860 mutex_unlock(&ictx->lock);
0861
0862 return len;
0863 }
0864
0865 static ssize_t imon_clock_store(struct device *d,
0866 struct device_attribute *attr,
0867 const char *buf, size_t count)
0868 {
0869 struct imon_context *ictx = dev_get_drvdata(d);
0870 ssize_t retval;
0871 unsigned int year, month, day, dow, hour, minute, second;
0872
0873 if (!ictx)
0874 return -ENODEV;
0875
0876 mutex_lock(&ictx->lock);
0877
0878 if (!ictx->display_supported) {
0879 retval = -ENODEV;
0880 goto exit;
0881 } else if (ictx->display_isopen) {
0882 retval = -EBUSY;
0883 goto exit;
0884 }
0885
0886 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
0887 &hour, &minute, &second) != 7) {
0888 retval = -EINVAL;
0889 goto exit;
0890 }
0891
0892 if ((month < 1 || month > 12) ||
0893 (day < 1 || day > 31) || (dow > 6) ||
0894 (hour > 23) || (minute > 59) || (second > 59)) {
0895 retval = -EINVAL;
0896 goto exit;
0897 }
0898
0899 retval = send_set_imon_clock(ictx, year, month, day, dow,
0900 hour, minute, second);
0901 if (retval)
0902 goto exit;
0903
0904 retval = count;
0905 exit:
0906 mutex_unlock(&ictx->lock);
0907
0908 return retval;
0909 }
0910
0911
0912 static DEVICE_ATTR_RW(imon_clock);
0913 static DEVICE_ATTR_RW(associate_remote);
0914
0915 static struct attribute *imon_display_sysfs_entries[] = {
0916 &dev_attr_imon_clock.attr,
0917 NULL
0918 };
0919
0920 static const struct attribute_group imon_display_attr_group = {
0921 .attrs = imon_display_sysfs_entries
0922 };
0923
0924 static struct attribute *imon_rf_sysfs_entries[] = {
0925 &dev_attr_associate_remote.attr,
0926 NULL
0927 };
0928
0929 static const struct attribute_group imon_rf_attr_group = {
0930 .attrs = imon_rf_sysfs_entries
0931 };
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944 static ssize_t vfd_write(struct file *file, const char __user *buf,
0945 size_t n_bytes, loff_t *pos)
0946 {
0947 int i;
0948 int offset;
0949 int seq;
0950 int retval = 0;
0951 struct imon_context *ictx = file->private_data;
0952 static const unsigned char vfd_packet6[] = {
0953 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
0954
0955 if (ictx->disconnected)
0956 return -ENODEV;
0957
0958 mutex_lock(&ictx->lock);
0959
0960 if (!ictx->dev_present_intf0) {
0961 pr_err_ratelimited("no iMON device present\n");
0962 retval = -ENODEV;
0963 goto exit;
0964 }
0965
0966 if (n_bytes <= 0 || n_bytes > 32) {
0967 pr_err_ratelimited("invalid payload size\n");
0968 retval = -EINVAL;
0969 goto exit;
0970 }
0971
0972 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
0973 retval = -EFAULT;
0974 goto exit;
0975 }
0976
0977
0978 for (i = n_bytes; i < 32; ++i)
0979 ictx->tx.data_buf[i] = ' ';
0980
0981 for (i = 32; i < 35; ++i)
0982 ictx->tx.data_buf[i] = 0xFF;
0983
0984 offset = 0;
0985 seq = 0;
0986
0987 do {
0988 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
0989 ictx->usb_tx_buf[7] = (unsigned char) seq;
0990
0991 retval = send_packet(ictx);
0992 if (retval) {
0993 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
0994 goto exit;
0995 } else {
0996 seq += 2;
0997 offset += 7;
0998 }
0999
1000 } while (offset < 35);
1001
1002
1003 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
1004 ictx->usb_tx_buf[7] = (unsigned char) seq;
1005 retval = send_packet(ictx);
1006 if (retval)
1007 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
1008
1009 exit:
1010 mutex_unlock(&ictx->lock);
1011
1012 return (!retval) ? n_bytes : retval;
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 static ssize_t lcd_write(struct file *file, const char __user *buf,
1029 size_t n_bytes, loff_t *pos)
1030 {
1031 int retval = 0;
1032 struct imon_context *ictx = file->private_data;
1033
1034 if (ictx->disconnected)
1035 return -ENODEV;
1036
1037 mutex_lock(&ictx->lock);
1038
1039 if (!ictx->display_supported) {
1040 pr_err_ratelimited("no iMON display present\n");
1041 retval = -ENODEV;
1042 goto exit;
1043 }
1044
1045 if (n_bytes != 8) {
1046 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1047 (int)n_bytes);
1048 retval = -EINVAL;
1049 goto exit;
1050 }
1051
1052 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1053 retval = -EFAULT;
1054 goto exit;
1055 }
1056
1057 retval = send_packet(ictx);
1058 if (retval) {
1059 pr_err_ratelimited("send packet failed!\n");
1060 goto exit;
1061 } else {
1062 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1063 __func__, (int) n_bytes);
1064 }
1065 exit:
1066 mutex_unlock(&ictx->lock);
1067 return (!retval) ? n_bytes : retval;
1068 }
1069
1070
1071
1072
1073 static void usb_tx_callback(struct urb *urb)
1074 {
1075 struct imon_context *ictx;
1076
1077 if (!urb)
1078 return;
1079 ictx = (struct imon_context *)urb->context;
1080 if (!ictx)
1081 return;
1082
1083 ictx->tx.status = urb->status;
1084
1085
1086 ictx->tx.busy = false;
1087 smp_rmb();
1088 complete(&ictx->tx.finished);
1089 }
1090
1091
1092
1093
1094 static void imon_touch_display_timeout(struct timer_list *t)
1095 {
1096 struct imon_context *ictx = from_timer(ictx, t, ttimer);
1097
1098 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
1099 return;
1100
1101 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1102 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1103 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1104 input_sync(ictx->touch);
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
1122 {
1123 int retval;
1124 struct imon_context *ictx = rc->priv;
1125 struct device *dev = ictx->dev;
1126 bool unlock = false;
1127 unsigned char ir_proto_packet[] = {
1128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1129
1130 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
1131 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
1132
1133 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
1134 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1135 ir_proto_packet[0] = 0x01;
1136 *rc_proto = RC_PROTO_BIT_RC6_MCE;
1137 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
1138 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1139 if (!pad_stabilize)
1140 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1141
1142 *rc_proto = RC_PROTO_BIT_IMON;
1143 } else {
1144 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
1145 if (!pad_stabilize)
1146 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1147
1148 *rc_proto = RC_PROTO_BIT_IMON;
1149 }
1150
1151 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1152
1153 if (!mutex_is_locked(&ictx->lock)) {
1154 unlock = true;
1155 mutex_lock(&ictx->lock);
1156 }
1157
1158 retval = send_packet(ictx);
1159 if (retval)
1160 goto out;
1161
1162 ictx->rc_proto = *rc_proto;
1163 ictx->pad_mouse = false;
1164
1165 out:
1166 if (unlock)
1167 mutex_unlock(&ictx->lock);
1168
1169 return retval;
1170 }
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1181 {
1182 ktime_t ct;
1183 static ktime_t prev_time;
1184 static ktime_t hit_time;
1185 static int x, y, prev_result, hits;
1186 int result = 0;
1187 long msec, msec_hit;
1188
1189 ct = ktime_get();
1190 msec = ktime_ms_delta(ct, prev_time);
1191 msec_hit = ktime_ms_delta(ct, hit_time);
1192
1193 if (msec > 100) {
1194 x = 0;
1195 y = 0;
1196 hits = 0;
1197 }
1198
1199 x += a;
1200 y += b;
1201
1202 prev_time = ct;
1203
1204 if (abs(x) > threshold || abs(y) > threshold) {
1205 if (abs(y) > abs(x))
1206 result = (y > 0) ? 0x7F : 0x80;
1207 else
1208 result = (x > 0) ? 0x7F00 : 0x8000;
1209
1210 x = 0;
1211 y = 0;
1212
1213 if (result == prev_result) {
1214 hits++;
1215
1216 if (hits > 3) {
1217 switch (result) {
1218 case 0x7F:
1219 y = 17 * threshold / 30;
1220 break;
1221 case 0x80:
1222 y -= 17 * threshold / 30;
1223 break;
1224 case 0x7F00:
1225 x = 17 * threshold / 30;
1226 break;
1227 case 0x8000:
1228 x -= 17 * threshold / 30;
1229 break;
1230 }
1231 }
1232
1233 if (hits == 2 && msec_hit < timeout) {
1234 result = 0;
1235 hits = 1;
1236 }
1237 } else {
1238 prev_result = result;
1239 hits = 1;
1240 hit_time = ct;
1241 }
1242 }
1243
1244 return result;
1245 }
1246
1247 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1248 {
1249 u32 keycode;
1250 u32 release;
1251 bool is_release_code = false;
1252
1253
1254 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1255 ictx->rc_toggle = 0x0;
1256 ictx->rc_scancode = scancode;
1257
1258
1259 if (keycode == KEY_RESERVED) {
1260 release = scancode & ~0x4000;
1261 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1262 if (keycode != KEY_RESERVED)
1263 is_release_code = true;
1264 }
1265
1266 ictx->release_code = is_release_code;
1267
1268 return keycode;
1269 }
1270
1271 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1272 {
1273 u32 keycode;
1274
1275 #define MCE_KEY_MASK 0x7000
1276 #define MCE_TOGGLE_BIT 0x8000
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286 if (scancode & 0x80000000)
1287 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1288
1289 ictx->rc_scancode = scancode;
1290 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1291
1292
1293 ictx->release_code = false;
1294
1295 return keycode;
1296 }
1297
1298 static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
1299 {
1300 const struct imon_panel_key_table *key_table;
1301 u32 keycode = KEY_RESERVED;
1302 int i;
1303
1304 key_table = ictx->dev_descr->key_table;
1305
1306 for (i = 0; key_table[i].hw_code != 0; i++) {
1307 if (key_table[i].hw_code == (code | 0xffee)) {
1308 keycode = key_table[i].keycode;
1309 break;
1310 }
1311 }
1312 ictx->release_code = false;
1313 return keycode;
1314 }
1315
1316 static bool imon_mouse_event(struct imon_context *ictx,
1317 unsigned char *buf, int len)
1318 {
1319 signed char rel_x = 0x00, rel_y = 0x00;
1320 u8 right_shift = 1;
1321 bool mouse_input = true;
1322 int dir = 0;
1323 unsigned long flags;
1324
1325 spin_lock_irqsave(&ictx->kc_lock, flags);
1326
1327
1328 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1329 rel_x = buf[2];
1330 rel_y = buf[3];
1331 right_shift = 1;
1332
1333 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1334 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1335 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1336 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1337 if (buf[0] & 0x02)
1338 rel_x |= ~0x0f;
1339 rel_x = rel_x + rel_x / 2;
1340 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1341 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1342 if (buf[0] & 0x01)
1343 rel_y |= ~0x0f;
1344 rel_y = rel_y + rel_y / 2;
1345 right_shift = 2;
1346
1347 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1348 right_shift = 2;
1349
1350 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1351 dir = 1;
1352 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1353 dir = -1;
1354 } else
1355 mouse_input = false;
1356
1357 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1358
1359 if (mouse_input) {
1360 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1361
1362 if (dir) {
1363 input_report_rel(ictx->idev, REL_WHEEL, dir);
1364 } else if (rel_x || rel_y) {
1365 input_report_rel(ictx->idev, REL_X, rel_x);
1366 input_report_rel(ictx->idev, REL_Y, rel_y);
1367 } else {
1368 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1369 input_report_key(ictx->idev, BTN_RIGHT,
1370 buf[1] >> right_shift & 0x1);
1371 }
1372 input_sync(ictx->idev);
1373 spin_lock_irqsave(&ictx->kc_lock, flags);
1374 ictx->last_keycode = ictx->kc;
1375 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1376 }
1377
1378 return mouse_input;
1379 }
1380
1381 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1382 {
1383 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1384 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1385 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1386 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1387 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1388 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1389 input_sync(ictx->touch);
1390 }
1391
1392 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1393 {
1394 int dir = 0;
1395 signed char rel_x = 0x00, rel_y = 0x00;
1396 u16 timeout, threshold;
1397 u32 scancode = KEY_RESERVED;
1398 unsigned long flags;
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408 if (ictx->product != 0xffdc) {
1409
1410 buf[5] = buf[6] = buf[7] = 0;
1411 timeout = 500;
1412
1413 threshold = pad_thresh ? pad_thresh : 28;
1414 rel_x = buf[2];
1415 rel_y = buf[3];
1416
1417 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1418 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1419 dir = stabilize((int)rel_x, (int)rel_y,
1420 timeout, threshold);
1421 if (!dir) {
1422 spin_lock_irqsave(&ictx->kc_lock,
1423 flags);
1424 ictx->kc = KEY_UNKNOWN;
1425 spin_unlock_irqrestore(&ictx->kc_lock,
1426 flags);
1427 return;
1428 }
1429 buf[2] = dir & 0xFF;
1430 buf[3] = (dir >> 8) & 0xFF;
1431 scancode = be32_to_cpu(*((__be32 *)buf));
1432 }
1433 } else {
1434
1435
1436
1437
1438 if (abs(rel_y) > abs(rel_x)) {
1439 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1440 buf[3] = 0;
1441 if (rel_y > 0)
1442 scancode = 0x01007f00;
1443 else
1444 scancode = 0x01008000;
1445 } else {
1446 buf[2] = 0;
1447 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1448 if (rel_x > 0)
1449 scancode = 0x0100007f;
1450 else
1451 scancode = 0x01000080;
1452 }
1453 }
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465 } else {
1466 timeout = 10;
1467
1468 threshold = pad_thresh ? pad_thresh : 15;
1469
1470
1471 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1472 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1473 if (buf[0] & 0x02)
1474 rel_x |= ~0x10+1;
1475
1476 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1477 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1478 if (buf[0] & 0x01)
1479 rel_y |= ~0x10+1;
1480
1481 buf[0] = 0x01;
1482 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1483
1484 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1485 dir = stabilize((int)rel_x, (int)rel_y,
1486 timeout, threshold);
1487 if (!dir) {
1488 spin_lock_irqsave(&ictx->kc_lock, flags);
1489 ictx->kc = KEY_UNKNOWN;
1490 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1491 return;
1492 }
1493 buf[2] = dir & 0xFF;
1494 buf[3] = (dir >> 8) & 0xFF;
1495 scancode = be32_to_cpu(*((__be32 *)buf));
1496 } else {
1497
1498
1499
1500
1501 if (abs(rel_y) > abs(rel_x)) {
1502 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1503 buf[3] = 0;
1504 if (rel_y > 0)
1505 scancode = 0x01007f00;
1506 else
1507 scancode = 0x01008000;
1508 } else {
1509 buf[2] = 0;
1510 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1511 if (rel_x > 0)
1512 scancode = 0x0100007f;
1513 else
1514 scancode = 0x01000080;
1515 }
1516 }
1517 }
1518
1519 if (scancode) {
1520 spin_lock_irqsave(&ictx->kc_lock, flags);
1521 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1522 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1523 }
1524 }
1525
1526
1527
1528
1529
1530
1531 static int imon_parse_press_type(struct imon_context *ictx,
1532 unsigned char *buf, u8 ktype)
1533 {
1534 int press_type = 0;
1535 unsigned long flags;
1536
1537 spin_lock_irqsave(&ictx->kc_lock, flags);
1538
1539
1540 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1541 ictx->kc = ictx->last_keycode;
1542
1543
1544 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1545 buf[2] == 0x81 && buf[3] == 0xb7)
1546 ictx->kc = ictx->last_keycode;
1547
1548
1549 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1550 buf[2] == 0x81 && buf[3] == 0xb7)
1551 ictx->kc = ictx->last_keycode;
1552
1553
1554 else if (ktype == IMON_KEY_MCE) {
1555 ictx->rc_toggle = buf[2];
1556 press_type = 1;
1557
1558
1559 } else if (ictx->kc == KEY_RESERVED)
1560 press_type = -EINVAL;
1561
1562
1563 else if (ictx->release_code)
1564 press_type = 0;
1565
1566
1567 else
1568 press_type = 1;
1569
1570 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1571
1572 return press_type;
1573 }
1574
1575
1576
1577
1578 static void imon_incoming_packet(struct imon_context *ictx,
1579 struct urb *urb, int intf)
1580 {
1581 int len = urb->actual_length;
1582 unsigned char *buf = urb->transfer_buffer;
1583 struct device *dev = ictx->dev;
1584 unsigned long flags;
1585 u32 kc;
1586 u64 scancode;
1587 int press_type = 0;
1588 ktime_t t;
1589 static ktime_t prev_time;
1590 u8 ktype;
1591
1592
1593 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1594 return;
1595
1596
1597 if (len == 8 && buf[7] == 0xee) {
1598 scancode = be64_to_cpu(*((__be64 *)buf));
1599 ktype = IMON_KEY_PANEL;
1600 kc = imon_panel_key_lookup(ictx, scancode);
1601 ictx->release_code = false;
1602 } else {
1603 scancode = be32_to_cpu(*((__be32 *)buf));
1604 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
1605 ktype = IMON_KEY_IMON;
1606 if (buf[0] == 0x80)
1607 ktype = IMON_KEY_MCE;
1608 kc = imon_mce_key_lookup(ictx, scancode);
1609 } else {
1610 ktype = IMON_KEY_IMON;
1611 kc = imon_remote_key_lookup(ictx, scancode);
1612 }
1613 }
1614
1615 spin_lock_irqsave(&ictx->kc_lock, flags);
1616
1617 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1618 ictx->last_keycode = kc;
1619 if (!nomouse) {
1620 ictx->pad_mouse = !ictx->pad_mouse;
1621 dev_dbg(dev, "toggling to %s mode\n",
1622 ictx->pad_mouse ? "mouse" : "keyboard");
1623 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1624 return;
1625 } else {
1626 ictx->pad_mouse = false;
1627 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1628 }
1629 }
1630
1631 ictx->kc = kc;
1632 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1633
1634
1635 if (ictx->touch && len == 8 && buf[7] == 0x86) {
1636 imon_touch_event(ictx, buf);
1637 return;
1638
1639
1640 } else if (ictx->pad_mouse) {
1641 if (imon_mouse_event(ictx, buf, len))
1642 return;
1643 }
1644
1645
1646 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1647 ((len == 8) && (buf[0] & 0x40) &&
1648 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1649 len = 8;
1650 imon_pad_to_keys(ictx, buf);
1651 }
1652
1653 if (debug) {
1654 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1655 intf, len, buf);
1656 }
1657
1658 press_type = imon_parse_press_type(ictx, buf, ktype);
1659 if (press_type < 0)
1660 goto not_input_data;
1661
1662 if (ktype != IMON_KEY_PANEL) {
1663 if (press_type == 0)
1664 rc_keyup(ictx->rdev);
1665 else {
1666 enum rc_proto proto;
1667
1668 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1669 proto = RC_PROTO_RC6_MCE;
1670 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1671 proto = RC_PROTO_IMON;
1672 else
1673 return;
1674
1675 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1676 ictx->rc_toggle);
1677
1678 spin_lock_irqsave(&ictx->kc_lock, flags);
1679 ictx->last_keycode = ictx->kc;
1680 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1681 }
1682 return;
1683 }
1684
1685
1686 spin_lock_irqsave(&ictx->kc_lock, flags);
1687
1688 t = ktime_get();
1689
1690 if (ictx->kc == KEY_MUTE ||
1691 ictx->dev_descr->flags & IMON_SUPPRESS_REPEATED_KEYS) {
1692 if (ictx->kc == ictx->last_keycode &&
1693 ktime_ms_delta(t, prev_time) < ictx->idev->rep[REP_DELAY]) {
1694 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1695 return;
1696 }
1697 }
1698
1699 prev_time = t;
1700 kc = ictx->kc;
1701
1702 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1703
1704 input_report_key(ictx->idev, kc, press_type);
1705 input_sync(ictx->idev);
1706
1707
1708 input_report_key(ictx->idev, kc, 0);
1709 input_sync(ictx->idev);
1710
1711 spin_lock_irqsave(&ictx->kc_lock, flags);
1712 ictx->last_keycode = kc;
1713 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1714
1715 return;
1716
1717 not_input_data:
1718 if (len != 8) {
1719 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1720 __func__, len, intf);
1721 return;
1722 }
1723
1724
1725 if (buf[0] == 0x00 &&
1726 buf[2] == 0xFF &&
1727 buf[3] == 0xFF &&
1728 buf[4] == 0xFF &&
1729 buf[5] == 0xFF &&
1730 ((buf[6] == 0x4E && buf[7] == 0xDF) ||
1731 (buf[6] == 0x5E && buf[7] == 0xDF))) {
1732 dev_warn(dev, "%s: remote associated refid=%02X\n",
1733 __func__, buf[1]);
1734 ictx->rf_isassociating = false;
1735 }
1736 }
1737
1738
1739
1740
1741 static void usb_rx_callback_intf0(struct urb *urb)
1742 {
1743 struct imon_context *ictx;
1744 int intfnum = 0;
1745
1746 if (!urb)
1747 return;
1748
1749 ictx = (struct imon_context *)urb->context;
1750 if (!ictx)
1751 return;
1752
1753
1754
1755
1756
1757
1758 if (!ictx->dev_present_intf0)
1759 goto out;
1760
1761 switch (urb->status) {
1762 case -ENOENT:
1763 return;
1764
1765 case -ESHUTDOWN:
1766 break;
1767
1768 case 0:
1769 imon_incoming_packet(ictx, urb, intfnum);
1770 break;
1771
1772 default:
1773 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1774 __func__, urb->status);
1775 break;
1776 }
1777
1778 out:
1779 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1780 }
1781
1782 static void usb_rx_callback_intf1(struct urb *urb)
1783 {
1784 struct imon_context *ictx;
1785 int intfnum = 1;
1786
1787 if (!urb)
1788 return;
1789
1790 ictx = (struct imon_context *)urb->context;
1791 if (!ictx)
1792 return;
1793
1794
1795
1796
1797
1798
1799 if (!ictx->dev_present_intf1)
1800 goto out;
1801
1802 switch (urb->status) {
1803 case -ENOENT:
1804 return;
1805
1806 case -ESHUTDOWN:
1807 break;
1808
1809 case 0:
1810 imon_incoming_packet(ictx, urb, intfnum);
1811 break;
1812
1813 default:
1814 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1815 __func__, urb->status);
1816 break;
1817 }
1818
1819 out:
1820 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1821 }
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832 static void imon_get_ffdc_type(struct imon_context *ictx)
1833 {
1834 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1835 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1836 u64 allowed_protos = RC_PROTO_BIT_IMON;
1837
1838 switch (ffdc_cfg_byte) {
1839
1840 case 0x21:
1841 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1842 ictx->display_supported = false;
1843 break;
1844
1845 case 0x4e:
1846 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1847 ictx->display_supported = false;
1848 ictx->rf_device = true;
1849 break;
1850
1851 case 0x35:
1852 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1853 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1854 break;
1855
1856 case 0x24:
1857 case 0x30:
1858 case 0x85:
1859 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1860 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1861 break;
1862
1863 case 0x46:
1864 case 0x9e:
1865 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1866 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1867 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1868 break;
1869
1870 case 0x7e:
1871 dev_info(ictx->dev, "0xffdc iMON VFD, iMON or MCE IR");
1872 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1873 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1874 break;
1875
1876 case 0x9f:
1877 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1878 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1879 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1880 break;
1881
1882 case 0x26:
1883 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1884 ictx->display_supported = false;
1885 break;
1886
1887 case 0x98:
1888 dev_info(ictx->dev, "0xffdc iMON UltraBay, LCD + IR");
1889 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1890 allowed_protos = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1891 ictx->dev_descr = &ultrabay_table;
1892 break;
1893
1894 default:
1895 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
1896 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1897
1898
1899
1900
1901 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1902 break;
1903 }
1904
1905 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1906
1907 ictx->display_type = detected_display_type;
1908 ictx->rc_proto = allowed_protos;
1909 }
1910
1911 static void imon_set_display_type(struct imon_context *ictx)
1912 {
1913 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1914
1915
1916
1917
1918
1919
1920 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1921 switch (ictx->product) {
1922 case 0xffdc:
1923
1924 configured_display_type = ictx->display_type;
1925 break;
1926 case 0x0034:
1927 case 0x0035:
1928 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1929 break;
1930 case 0x0038:
1931 case 0x0039:
1932 case 0x0045:
1933 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1934 break;
1935 case 0x003c:
1936 case 0x0041:
1937 case 0x0042:
1938 case 0x0043:
1939 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1940 ictx->display_supported = false;
1941 break;
1942 case 0x0036:
1943 case 0x0044:
1944 default:
1945 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1946 break;
1947 }
1948 } else {
1949 configured_display_type = display_type;
1950 if (display_type == IMON_DISPLAY_TYPE_NONE)
1951 ictx->display_supported = false;
1952 else
1953 ictx->display_supported = true;
1954 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1955 __func__, display_type);
1956 }
1957
1958 ictx->display_type = configured_display_type;
1959 }
1960
1961 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1962 {
1963 struct rc_dev *rdev;
1964 int ret;
1965 static const unsigned char fp_packet[] = {
1966 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
1967
1968 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
1969 if (!rdev) {
1970 dev_err(ictx->dev, "remote control dev allocation failed\n");
1971 goto out;
1972 }
1973
1974 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1975 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1976 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1977 sizeof(ictx->phys_rdev));
1978 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1979
1980 rdev->device_name = ictx->name_rdev;
1981 rdev->input_phys = ictx->phys_rdev;
1982 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1983 rdev->dev.parent = ictx->dev;
1984
1985 rdev->priv = ictx;
1986
1987 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1988 rdev->change_protocol = imon_ir_change_protocol;
1989 rdev->driver_name = MOD_NAME;
1990
1991
1992 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1993 ret = send_packet(ictx);
1994
1995 if (ret)
1996 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1997
1998 if (ictx->product == 0xffdc) {
1999 imon_get_ffdc_type(ictx);
2000 rdev->allowed_protocols = ictx->rc_proto;
2001 }
2002
2003 imon_set_display_type(ictx);
2004
2005 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
2006 rdev->map_name = RC_MAP_IMON_MCE;
2007 else
2008 rdev->map_name = RC_MAP_IMON_PAD;
2009
2010 ret = rc_register_device(rdev);
2011 if (ret < 0) {
2012 dev_err(ictx->dev, "remote input dev register failed\n");
2013 goto out;
2014 }
2015
2016 return rdev;
2017
2018 out:
2019 rc_free_device(rdev);
2020 return NULL;
2021 }
2022
2023 static struct input_dev *imon_init_idev(struct imon_context *ictx)
2024 {
2025 const struct imon_panel_key_table *key_table;
2026 struct input_dev *idev;
2027 int ret, i;
2028
2029 key_table = ictx->dev_descr->key_table;
2030
2031 idev = input_allocate_device();
2032 if (!idev)
2033 goto out;
2034
2035 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
2036 "iMON Panel, Knob and Mouse(%04x:%04x)",
2037 ictx->vendor, ictx->product);
2038 idev->name = ictx->name_idev;
2039
2040 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2041 sizeof(ictx->phys_idev));
2042 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
2043 idev->phys = ictx->phys_idev;
2044
2045 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
2046
2047 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2048 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2049 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2050 BIT_MASK(REL_WHEEL);
2051
2052
2053 for (i = 0; key_table[i].hw_code != 0; i++) {
2054 u32 kc = key_table[i].keycode;
2055 __set_bit(kc, idev->keybit);
2056 }
2057
2058 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2059 idev->dev.parent = ictx->dev;
2060 input_set_drvdata(idev, ictx);
2061
2062 ret = input_register_device(idev);
2063 if (ret < 0) {
2064 dev_err(ictx->dev, "input dev register failed\n");
2065 goto out;
2066 }
2067
2068 return idev;
2069
2070 out:
2071 input_free_device(idev);
2072 return NULL;
2073 }
2074
2075 static struct input_dev *imon_init_touch(struct imon_context *ictx)
2076 {
2077 struct input_dev *touch;
2078 int ret;
2079
2080 touch = input_allocate_device();
2081 if (!touch)
2082 goto touch_alloc_failed;
2083
2084 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2085 "iMON USB Touchscreen (%04x:%04x)",
2086 ictx->vendor, ictx->product);
2087 touch->name = ictx->name_touch;
2088
2089 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2090 sizeof(ictx->phys_touch));
2091 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
2092 touch->phys = ictx->phys_touch;
2093
2094 touch->evbit[0] =
2095 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2096 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2097 BIT_MASK(BTN_TOUCH);
2098 input_set_abs_params(touch, ABS_X,
2099 0x00, 0xfff, 0, 0);
2100 input_set_abs_params(touch, ABS_Y,
2101 0x00, 0xfff, 0, 0);
2102
2103 input_set_drvdata(touch, ictx);
2104
2105 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2106 touch->dev.parent = ictx->dev;
2107 ret = input_register_device(touch);
2108 if (ret < 0) {
2109 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2110 goto touch_register_failed;
2111 }
2112
2113 return touch;
2114
2115 touch_register_failed:
2116 input_free_device(touch);
2117
2118 touch_alloc_failed:
2119 return NULL;
2120 }
2121
2122 static bool imon_find_endpoints(struct imon_context *ictx,
2123 struct usb_host_interface *iface_desc)
2124 {
2125 struct usb_endpoint_descriptor *ep;
2126 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2127 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2128 int ifnum = iface_desc->desc.bInterfaceNumber;
2129 int num_endpts = iface_desc->desc.bNumEndpoints;
2130 int i, ep_dir, ep_type;
2131 bool ir_ep_found = false;
2132 bool display_ep_found = false;
2133 bool tx_control = false;
2134
2135
2136
2137
2138
2139
2140 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2141 ep = &iface_desc->endpoint[i].desc;
2142 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2143 ep_type = usb_endpoint_type(ep);
2144
2145 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2146 ep_type == USB_ENDPOINT_XFER_INT) {
2147
2148 rx_endpoint = ep;
2149 ir_ep_found = true;
2150 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2151
2152 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2153 ep_type == USB_ENDPOINT_XFER_INT) {
2154 tx_endpoint = ep;
2155 display_ep_found = true;
2156 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2157 }
2158 }
2159
2160 if (ifnum == 0) {
2161 ictx->rx_endpoint_intf0 = rx_endpoint;
2162
2163
2164
2165
2166 ictx->tx_endpoint = tx_endpoint;
2167 } else {
2168 ictx->rx_endpoint_intf1 = rx_endpoint;
2169 }
2170
2171
2172
2173
2174
2175 if (!display_ep_found) {
2176 tx_control = true;
2177 display_ep_found = true;
2178 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2179 __func__);
2180 }
2181
2182
2183
2184
2185
2186
2187 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2188 display_ep_found = false;
2189 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2190 }
2191
2192
2193
2194
2195
2196 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2197 display_ep_found = false;
2198 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2199 }
2200
2201
2202 if (!ir_ep_found)
2203 pr_err("no valid input (IR) endpoint found\n");
2204
2205 ictx->tx_control = tx_control;
2206
2207 if (display_ep_found)
2208 ictx->display_supported = true;
2209
2210 return ir_ep_found;
2211
2212 }
2213
2214 static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2215 const struct usb_device_id *id)
2216 {
2217 struct imon_context *ictx;
2218 struct urb *rx_urb;
2219 struct urb *tx_urb;
2220 struct device *dev = &intf->dev;
2221 struct usb_host_interface *iface_desc;
2222 int ret = -ENOMEM;
2223
2224 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
2225 if (!ictx)
2226 goto exit;
2227
2228 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2229 if (!rx_urb)
2230 goto rx_urb_alloc_failed;
2231 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2232 if (!tx_urb)
2233 goto tx_urb_alloc_failed;
2234
2235 mutex_init(&ictx->lock);
2236 spin_lock_init(&ictx->kc_lock);
2237
2238 mutex_lock(&ictx->lock);
2239
2240 ictx->dev = dev;
2241 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2242 ictx->rx_urb_intf0 = rx_urb;
2243 ictx->tx_urb = tx_urb;
2244 ictx->rf_device = false;
2245
2246 init_completion(&ictx->tx.finished);
2247
2248 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2249 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2250
2251
2252 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
2253
2254 ictx->send_packet_delay = ictx->dev_descr->flags &
2255 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
2256
2257 ret = -ENODEV;
2258 iface_desc = intf->cur_altsetting;
2259 if (!imon_find_endpoints(ictx, iface_desc)) {
2260 goto find_endpoint_failed;
2261 }
2262
2263 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2264 usb_rcvintpipe(ictx->usbdev_intf0,
2265 ictx->rx_endpoint_intf0->bEndpointAddress),
2266 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2267 usb_rx_callback_intf0, ictx,
2268 ictx->rx_endpoint_intf0->bInterval);
2269
2270 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2271 if (ret) {
2272 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2273 goto urb_submit_failed;
2274 }
2275
2276 ictx->idev = imon_init_idev(ictx);
2277 if (!ictx->idev) {
2278 dev_err(dev, "%s: input device setup failed\n", __func__);
2279 goto idev_setup_failed;
2280 }
2281
2282 ictx->rdev = imon_init_rdev(ictx);
2283 if (!ictx->rdev) {
2284 dev_err(dev, "%s: rc device setup failed\n", __func__);
2285 goto rdev_setup_failed;
2286 }
2287
2288 ictx->dev_present_intf0 = true;
2289
2290 mutex_unlock(&ictx->lock);
2291 return ictx;
2292
2293 rdev_setup_failed:
2294 input_unregister_device(ictx->idev);
2295 idev_setup_failed:
2296 usb_kill_urb(ictx->rx_urb_intf0);
2297 urb_submit_failed:
2298 find_endpoint_failed:
2299 usb_put_dev(ictx->usbdev_intf0);
2300 mutex_unlock(&ictx->lock);
2301 usb_free_urb(tx_urb);
2302 tx_urb_alloc_failed:
2303 usb_free_urb(rx_urb);
2304 rx_urb_alloc_failed:
2305 kfree(ictx);
2306 exit:
2307 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2308
2309 return NULL;
2310 }
2311
2312 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2313 struct imon_context *ictx)
2314 {
2315 struct urb *rx_urb;
2316 struct usb_host_interface *iface_desc;
2317 int ret = -ENOMEM;
2318
2319 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2320 if (!rx_urb)
2321 goto rx_urb_alloc_failed;
2322
2323 mutex_lock(&ictx->lock);
2324
2325 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2326 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
2327 }
2328
2329 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2330 ictx->rx_urb_intf1 = rx_urb;
2331
2332 ret = -ENODEV;
2333 iface_desc = intf->cur_altsetting;
2334 if (!imon_find_endpoints(ictx, iface_desc))
2335 goto find_endpoint_failed;
2336
2337 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2338 ictx->touch = imon_init_touch(ictx);
2339 if (!ictx->touch)
2340 goto touch_setup_failed;
2341 } else
2342 ictx->touch = NULL;
2343
2344 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2345 usb_rcvintpipe(ictx->usbdev_intf1,
2346 ictx->rx_endpoint_intf1->bEndpointAddress),
2347 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2348 usb_rx_callback_intf1, ictx,
2349 ictx->rx_endpoint_intf1->bInterval);
2350
2351 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2352
2353 if (ret) {
2354 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2355 goto urb_submit_failed;
2356 }
2357
2358 ictx->dev_present_intf1 = true;
2359
2360 mutex_unlock(&ictx->lock);
2361 return ictx;
2362
2363 urb_submit_failed:
2364 if (ictx->touch)
2365 input_unregister_device(ictx->touch);
2366 touch_setup_failed:
2367 find_endpoint_failed:
2368 usb_put_dev(ictx->usbdev_intf1);
2369 ictx->usbdev_intf1 = NULL;
2370 mutex_unlock(&ictx->lock);
2371 usb_free_urb(rx_urb);
2372 ictx->rx_urb_intf1 = NULL;
2373 rx_urb_alloc_failed:
2374 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
2375
2376 return NULL;
2377 }
2378
2379 static void imon_init_display(struct imon_context *ictx,
2380 struct usb_interface *intf)
2381 {
2382 int ret;
2383
2384 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2385
2386
2387 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2388 if (ret)
2389 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2390 ret);
2391
2392 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2393 ret = usb_register_dev(intf, &imon_lcd_class);
2394 else
2395 ret = usb_register_dev(intf, &imon_vfd_class);
2396 if (ret)
2397
2398 dev_info(ictx->dev, "could not get a minor number for display\n");
2399
2400 }
2401
2402
2403
2404
2405 static int imon_probe(struct usb_interface *interface,
2406 const struct usb_device_id *id)
2407 {
2408 struct usb_device *usbdev = NULL;
2409 struct usb_host_interface *iface_desc = NULL;
2410 struct usb_interface *first_if;
2411 struct device *dev = &interface->dev;
2412 int ifnum, sysfs_err;
2413 int ret = 0;
2414 struct imon_context *ictx = NULL;
2415 u16 vendor, product;
2416
2417 usbdev = usb_get_dev(interface_to_usbdev(interface));
2418 iface_desc = interface->cur_altsetting;
2419 ifnum = iface_desc->desc.bInterfaceNumber;
2420 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2421 product = le16_to_cpu(usbdev->descriptor.idProduct);
2422
2423 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2424 __func__, vendor, product, ifnum);
2425
2426 first_if = usb_ifnum_to_if(usbdev, 0);
2427 if (!first_if) {
2428 ret = -ENODEV;
2429 goto fail;
2430 }
2431
2432 if (ifnum == 0) {
2433 ictx = imon_init_intf0(interface, id);
2434 if (!ictx) {
2435 pr_err("failed to initialize context!\n");
2436 ret = -ENODEV;
2437 goto fail;
2438 }
2439 refcount_set(&ictx->users, 1);
2440
2441 } else {
2442
2443 struct imon_context *first_if_ctx = usb_get_intfdata(first_if);
2444
2445
2446 if (!first_if_ctx) {
2447 ret = -ENODEV;
2448 goto fail;
2449 }
2450
2451 ictx = imon_init_intf1(interface, first_if_ctx);
2452 if (!ictx) {
2453 pr_err("failed to attach to context!\n");
2454 ret = -ENODEV;
2455 goto fail;
2456 }
2457 refcount_inc(&ictx->users);
2458
2459 }
2460
2461 usb_set_intfdata(interface, ictx);
2462
2463 if (ifnum == 0) {
2464 if (product == 0xffdc && ictx->rf_device) {
2465 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2466 &imon_rf_attr_group);
2467 if (sysfs_err)
2468 pr_err("Could not create RF sysfs entries(%d)\n",
2469 sysfs_err);
2470 }
2471
2472 if (ictx->display_supported)
2473 imon_init_display(ictx, interface);
2474 }
2475
2476 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2477 vendor, product, ifnum,
2478 usbdev->bus->busnum, usbdev->devnum);
2479
2480 usb_put_dev(usbdev);
2481
2482 return 0;
2483
2484 fail:
2485 usb_put_dev(usbdev);
2486 dev_err(dev, "unable to register, err %d\n", ret);
2487
2488 return ret;
2489 }
2490
2491
2492
2493
2494 static void imon_disconnect(struct usb_interface *interface)
2495 {
2496 struct imon_context *ictx;
2497 struct device *dev;
2498 int ifnum;
2499
2500 ictx = usb_get_intfdata(interface);
2501 ictx->disconnected = true;
2502 dev = ictx->dev;
2503 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2504
2505
2506
2507
2508
2509 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2510 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2511
2512 usb_set_intfdata(interface, NULL);
2513
2514
2515 if (ictx->tx.busy) {
2516 usb_kill_urb(ictx->tx_urb);
2517 complete(&ictx->tx.finished);
2518 }
2519
2520 if (ifnum == 0) {
2521 ictx->dev_present_intf0 = false;
2522 usb_kill_urb(ictx->rx_urb_intf0);
2523 input_unregister_device(ictx->idev);
2524 rc_unregister_device(ictx->rdev);
2525 if (ictx->display_supported) {
2526 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2527 usb_deregister_dev(interface, &imon_lcd_class);
2528 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
2529 usb_deregister_dev(interface, &imon_vfd_class);
2530 }
2531 usb_put_dev(ictx->usbdev_intf0);
2532 } else {
2533 ictx->dev_present_intf1 = false;
2534 usb_kill_urb(ictx->rx_urb_intf1);
2535 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2536 del_timer_sync(&ictx->ttimer);
2537 input_unregister_device(ictx->touch);
2538 }
2539 usb_put_dev(ictx->usbdev_intf1);
2540 }
2541
2542 if (refcount_dec_and_test(&ictx->users))
2543 free_imon_context(ictx);
2544
2545 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2546 __func__, ifnum);
2547 }
2548
2549 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2550 {
2551 struct imon_context *ictx = usb_get_intfdata(intf);
2552 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2553
2554 if (ifnum == 0)
2555 usb_kill_urb(ictx->rx_urb_intf0);
2556 else
2557 usb_kill_urb(ictx->rx_urb_intf1);
2558
2559 return 0;
2560 }
2561
2562 static int imon_resume(struct usb_interface *intf)
2563 {
2564 int rc = 0;
2565 struct imon_context *ictx = usb_get_intfdata(intf);
2566 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2567
2568 if (ifnum == 0) {
2569 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2570 usb_rcvintpipe(ictx->usbdev_intf0,
2571 ictx->rx_endpoint_intf0->bEndpointAddress),
2572 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2573 usb_rx_callback_intf0, ictx,
2574 ictx->rx_endpoint_intf0->bInterval);
2575
2576 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_NOIO);
2577
2578 } else {
2579 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2580 usb_rcvintpipe(ictx->usbdev_intf1,
2581 ictx->rx_endpoint_intf1->bEndpointAddress),
2582 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2583 usb_rx_callback_intf1, ictx,
2584 ictx->rx_endpoint_intf1->bInterval);
2585
2586 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_NOIO);
2587 }
2588
2589 return rc;
2590 }
2591
2592 module_usb_driver(imon_driver);