PageRenderTime 60ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/rc/imon.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2476 lines | 1757 code | 388 blank | 331 comment | 362 complexity | bf7ad5071e25001540135eba23591f23 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
  3. *
  4. * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
  5. * Portions based on the original lirc_imon driver,
  6. * Copyright(C) 2004 Venky Raju(dev@venky.ws)
  7. *
  8. * Huge thanks to R. Geoff Newbury for invaluable debugging on the
  9. * 0xffdc iMON devices, and for sending me one to hack on, without
  10. * which the support for them wouldn't be nearly as good. Thanks
  11. * also to the numerous 0xffdc device owners that tested auto-config
  12. * support for me and provided debug dumps from their devices.
  13. *
  14. * imon is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  29. #include <linux/errno.h>
  30. #include <linux/init.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/input.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/input.h>
  38. #include <media/rc-core.h>
  39. #include <linux/time.h>
  40. #include <linux/timer.h>
  41. #define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
  42. #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
  43. #define MOD_NAME "imon"
  44. #define MOD_VERSION "0.9.3"
  45. #define DISPLAY_MINOR_BASE 144
  46. #define DEVICE_NAME "lcd%d"
  47. #define BUF_CHUNK_SIZE 8
  48. #define BUF_SIZE 128
  49. #define BIT_DURATION 250 /* each bit received is 250us */
  50. #define IMON_CLOCK_ENABLE_PACKETS 2
  51. /*** P R O T O T Y P E S ***/
  52. /* USB Callback prototypes */
  53. static int imon_probe(struct usb_interface *interface,
  54. const struct usb_device_id *id);
  55. static void imon_disconnect(struct usb_interface *interface);
  56. static void usb_rx_callback_intf0(struct urb *urb);
  57. static void usb_rx_callback_intf1(struct urb *urb);
  58. static void usb_tx_callback(struct urb *urb);
  59. /* suspend/resume support */
  60. static int imon_resume(struct usb_interface *intf);
  61. static int imon_suspend(struct usb_interface *intf, pm_message_t message);
  62. /* Display file_operations function prototypes */
  63. static int display_open(struct inode *inode, struct file *file);
  64. static int display_close(struct inode *inode, struct file *file);
  65. /* VFD write operation */
  66. static ssize_t vfd_write(struct file *file, const char *buf,
  67. size_t n_bytes, loff_t *pos);
  68. /* LCD file_operations override function prototypes */
  69. static ssize_t lcd_write(struct file *file, const char *buf,
  70. size_t n_bytes, loff_t *pos);
  71. /*** G L O B A L S ***/
  72. struct imon_context {
  73. struct device *dev;
  74. /* Newer devices have two interfaces */
  75. struct usb_device *usbdev_intf0;
  76. struct usb_device *usbdev_intf1;
  77. bool display_supported; /* not all controllers do */
  78. bool display_isopen; /* display port has been opened */
  79. bool rf_device; /* true if iMON 2.4G LT/DT RF device */
  80. bool rf_isassociating; /* RF remote associating */
  81. bool dev_present_intf0; /* USB device presence, interface 0 */
  82. bool dev_present_intf1; /* USB device presence, interface 1 */
  83. struct mutex lock; /* to lock this object */
  84. wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
  85. struct usb_endpoint_descriptor *rx_endpoint_intf0;
  86. struct usb_endpoint_descriptor *rx_endpoint_intf1;
  87. struct usb_endpoint_descriptor *tx_endpoint;
  88. struct urb *rx_urb_intf0;
  89. struct urb *rx_urb_intf1;
  90. struct urb *tx_urb;
  91. bool tx_control;
  92. unsigned char usb_rx_buf[8];
  93. unsigned char usb_tx_buf[8];
  94. struct tx_t {
  95. unsigned char data_buf[35]; /* user data buffer */
  96. struct completion finished; /* wait for write to finish */
  97. bool busy; /* write in progress */
  98. int status; /* status of tx completion */
  99. } tx;
  100. u16 vendor; /* usb vendor ID */
  101. u16 product; /* usb product ID */
  102. struct rc_dev *rdev; /* rc-core device for remote */
  103. struct input_dev *idev; /* input device for panel & IR mouse */
  104. struct input_dev *touch; /* input device for touchscreen */
  105. spinlock_t kc_lock; /* make sure we get keycodes right */
  106. u32 kc; /* current input keycode */
  107. u32 last_keycode; /* last reported input keycode */
  108. u32 rc_scancode; /* the computed remote scancode */
  109. u8 rc_toggle; /* the computed remote toggle bit */
  110. u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
  111. bool release_code; /* some keys send a release code */
  112. u8 display_type; /* store the display type */
  113. bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
  114. char name_rdev[128]; /* rc input device name */
  115. char phys_rdev[64]; /* rc input device phys path */
  116. char name_idev[128]; /* input device name */
  117. char phys_idev[64]; /* input device phys path */
  118. char name_touch[128]; /* touch screen name */
  119. char phys_touch[64]; /* touch screen phys path */
  120. struct timer_list ttimer; /* touch screen timer */
  121. int touch_x; /* x coordinate on touchscreen */
  122. int touch_y; /* y coordinate on touchscreen */
  123. };
  124. #define TOUCH_TIMEOUT (HZ/30)
  125. /* vfd character device file operations */
  126. static const struct file_operations vfd_fops = {
  127. .owner = THIS_MODULE,
  128. .open = &display_open,
  129. .write = &vfd_write,
  130. .release = &display_close,
  131. .llseek = noop_llseek,
  132. };
  133. /* lcd character device file operations */
  134. static const struct file_operations lcd_fops = {
  135. .owner = THIS_MODULE,
  136. .open = &display_open,
  137. .write = &lcd_write,
  138. .release = &display_close,
  139. .llseek = noop_llseek,
  140. };
  141. enum {
  142. IMON_DISPLAY_TYPE_AUTO = 0,
  143. IMON_DISPLAY_TYPE_VFD = 1,
  144. IMON_DISPLAY_TYPE_LCD = 2,
  145. IMON_DISPLAY_TYPE_VGA = 3,
  146. IMON_DISPLAY_TYPE_NONE = 4,
  147. };
  148. enum {
  149. IMON_KEY_IMON = 0,
  150. IMON_KEY_MCE = 1,
  151. IMON_KEY_PANEL = 2,
  152. };
  153. /*
  154. * USB Device ID for iMON USB Control Boards
  155. *
  156. * The Windows drivers contain 6 different inf files, more or less one for
  157. * each new device until the 0x0034-0x0046 devices, which all use the same
  158. * driver. Some of the devices in the 34-46 range haven't been definitively
  159. * identified yet. Early devices have either a TriGem Computer, Inc. or a
  160. * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
  161. * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
  162. * the ffdc and later devices, which do onboard decoding.
  163. */
  164. static struct usb_device_id imon_usb_id_table[] = {
  165. /*
  166. * Several devices with this same device ID, all use iMON_PAD.inf
  167. * SoundGraph iMON PAD (IR & VFD)
  168. * SoundGraph iMON PAD (IR & LCD)
  169. * SoundGraph iMON Knob (IR only)
  170. */
  171. { USB_DEVICE(0x15c2, 0xffdc) },
  172. /*
  173. * Newer devices, all driven by the latest iMON Windows driver, full
  174. * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
  175. * Need user input to fill in details on unknown devices.
  176. */
  177. /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
  178. { USB_DEVICE(0x15c2, 0x0034) },
  179. /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
  180. { USB_DEVICE(0x15c2, 0x0035) },
  181. /* SoundGraph iMON OEM VFD (IR & VFD) */
  182. { USB_DEVICE(0x15c2, 0x0036) },
  183. /* device specifics unknown */
  184. { USB_DEVICE(0x15c2, 0x0037) },
  185. /* SoundGraph iMON OEM LCD (IR & LCD) */
  186. { USB_DEVICE(0x15c2, 0x0038) },
  187. /* SoundGraph iMON UltraBay (IR & LCD) */
  188. { USB_DEVICE(0x15c2, 0x0039) },
  189. /* device specifics unknown */
  190. { USB_DEVICE(0x15c2, 0x003a) },
  191. /* device specifics unknown */
  192. { USB_DEVICE(0x15c2, 0x003b) },
  193. /* SoundGraph iMON OEM Inside (IR only) */
  194. { USB_DEVICE(0x15c2, 0x003c) },
  195. /* device specifics unknown */
  196. { USB_DEVICE(0x15c2, 0x003d) },
  197. /* device specifics unknown */
  198. { USB_DEVICE(0x15c2, 0x003e) },
  199. /* device specifics unknown */
  200. { USB_DEVICE(0x15c2, 0x003f) },
  201. /* device specifics unknown */
  202. { USB_DEVICE(0x15c2, 0x0040) },
  203. /* SoundGraph iMON MINI (IR only) */
  204. { USB_DEVICE(0x15c2, 0x0041) },
  205. /* Antec Veris Multimedia Station EZ External (IR only) */
  206. { USB_DEVICE(0x15c2, 0x0042) },
  207. /* Antec Veris Multimedia Station Basic Internal (IR only) */
  208. { USB_DEVICE(0x15c2, 0x0043) },
  209. /* Antec Veris Multimedia Station Elite (IR & VFD) */
  210. { USB_DEVICE(0x15c2, 0x0044) },
  211. /* Antec Veris Multimedia Station Premiere (IR & LCD) */
  212. { USB_DEVICE(0x15c2, 0x0045) },
  213. /* device specifics unknown */
  214. { USB_DEVICE(0x15c2, 0x0046) },
  215. {}
  216. };
  217. /* USB Device data */
  218. static struct usb_driver imon_driver = {
  219. .name = MOD_NAME,
  220. .probe = imon_probe,
  221. .disconnect = imon_disconnect,
  222. .suspend = imon_suspend,
  223. .resume = imon_resume,
  224. .id_table = imon_usb_id_table,
  225. };
  226. static struct usb_class_driver imon_vfd_class = {
  227. .name = DEVICE_NAME,
  228. .fops = &vfd_fops,
  229. .minor_base = DISPLAY_MINOR_BASE,
  230. };
  231. static struct usb_class_driver imon_lcd_class = {
  232. .name = DEVICE_NAME,
  233. .fops = &lcd_fops,
  234. .minor_base = DISPLAY_MINOR_BASE,
  235. };
  236. /* imon receiver front panel/knob key table */
  237. static const struct {
  238. u64 hw_code;
  239. u32 keycode;
  240. } imon_panel_key_table[] = {
  241. { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
  242. { 0x000000001200ffeell, KEY_UP },
  243. { 0x000000001300ffeell, KEY_DOWN },
  244. { 0x000000001400ffeell, KEY_LEFT },
  245. { 0x000000001500ffeell, KEY_RIGHT },
  246. { 0x000000001600ffeell, KEY_ENTER },
  247. { 0x000000001700ffeell, KEY_ESC },
  248. { 0x000000001f00ffeell, KEY_AUDIO },
  249. { 0x000000002000ffeell, KEY_VIDEO },
  250. { 0x000000002100ffeell, KEY_CAMERA },
  251. { 0x000000002700ffeell, KEY_DVD },
  252. { 0x000000002300ffeell, KEY_TV },
  253. { 0x000000002b00ffeell, KEY_EXIT },
  254. { 0x000000002c00ffeell, KEY_SELECT },
  255. { 0x000000002d00ffeell, KEY_MENU },
  256. { 0x000000000500ffeell, KEY_PREVIOUS },
  257. { 0x000000000700ffeell, KEY_REWIND },
  258. { 0x000000000400ffeell, KEY_STOP },
  259. { 0x000000003c00ffeell, KEY_PLAYPAUSE },
  260. { 0x000000000800ffeell, KEY_FASTFORWARD },
  261. { 0x000000000600ffeell, KEY_NEXT },
  262. { 0x000000010000ffeell, KEY_RIGHT },
  263. { 0x000001000000ffeell, KEY_LEFT },
  264. { 0x000000003d00ffeell, KEY_SELECT },
  265. { 0x000100000000ffeell, KEY_VOLUMEUP },
  266. { 0x010000000000ffeell, KEY_VOLUMEDOWN },
  267. { 0x000000000100ffeell, KEY_MUTE },
  268. /* 0xffdc iMON MCE VFD */
  269. { 0x00010000ffffffeell, KEY_VOLUMEUP },
  270. { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
  271. { 0x00000001ffffffeell, KEY_MUTE },
  272. { 0x0000000fffffffeell, KEY_MEDIA },
  273. { 0x00000012ffffffeell, KEY_UP },
  274. { 0x00000013ffffffeell, KEY_DOWN },
  275. { 0x00000014ffffffeell, KEY_LEFT },
  276. { 0x00000015ffffffeell, KEY_RIGHT },
  277. { 0x00000016ffffffeell, KEY_ENTER },
  278. { 0x00000017ffffffeell, KEY_ESC },
  279. /* iMON Knob values */
  280. { 0x000100ffffffffeell, KEY_VOLUMEUP },
  281. { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
  282. { 0x000008ffffffffeell, KEY_MUTE },
  283. };
  284. /* to prevent races between open() and disconnect(), probing, etc */
  285. static DEFINE_MUTEX(driver_lock);
  286. /* Module bookkeeping bits */
  287. MODULE_AUTHOR(MOD_AUTHOR);
  288. MODULE_DESCRIPTION(MOD_DESC);
  289. MODULE_VERSION(MOD_VERSION);
  290. MODULE_LICENSE("GPL");
  291. MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
  292. static bool debug;
  293. module_param(debug, bool, S_IRUGO | S_IWUSR);
  294. MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
  295. /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
  296. static int display_type;
  297. module_param(display_type, int, S_IRUGO);
  298. MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
  299. "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
  300. static int pad_stabilize = 1;
  301. module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
  302. MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
  303. "presses in arrow key mode. 0=disable, 1=enable (default).");
  304. /*
  305. * In certain use cases, mouse mode isn't really helpful, and could actually
  306. * cause confusion, so allow disabling it when the IR device is open.
  307. */
  308. static bool nomouse;
  309. module_param(nomouse, bool, S_IRUGO | S_IWUSR);
  310. MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
  311. "open. 0=don't disable, 1=disable. (default: don't disable)");
  312. /* threshold at which a pad push registers as an arrow key in kbd mode */
  313. static int pad_thresh;
  314. module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
  315. MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
  316. "arrow key in kbd mode (default: 28)");
  317. static void free_imon_context(struct imon_context *ictx)
  318. {
  319. struct device *dev = ictx->dev;
  320. usb_free_urb(ictx->tx_urb);
  321. usb_free_urb(ictx->rx_urb_intf0);
  322. usb_free_urb(ictx->rx_urb_intf1);
  323. kfree(ictx);
  324. dev_dbg(dev, "%s: iMON context freed\n", __func__);
  325. }
  326. /**
  327. * Called when the Display device (e.g. /dev/lcd0)
  328. * is opened by the application.
  329. */
  330. static int display_open(struct inode *inode, struct file *file)
  331. {
  332. struct usb_interface *interface;
  333. struct imon_context *ictx = NULL;
  334. int subminor;
  335. int retval = 0;
  336. /* prevent races with disconnect */
  337. mutex_lock(&driver_lock);
  338. subminor = iminor(inode);
  339. interface = usb_find_interface(&imon_driver, subminor);
  340. if (!interface) {
  341. pr_err("could not find interface for minor %d\n", subminor);
  342. retval = -ENODEV;
  343. goto exit;
  344. }
  345. ictx = usb_get_intfdata(interface);
  346. if (!ictx) {
  347. pr_err("no context found for minor %d\n", subminor);
  348. retval = -ENODEV;
  349. goto exit;
  350. }
  351. mutex_lock(&ictx->lock);
  352. if (!ictx->display_supported) {
  353. pr_err("display not supported by device\n");
  354. retval = -ENODEV;
  355. } else if (ictx->display_isopen) {
  356. pr_err("display port is already open\n");
  357. retval = -EBUSY;
  358. } else {
  359. ictx->display_isopen = true;
  360. file->private_data = ictx;
  361. dev_dbg(ictx->dev, "display port opened\n");
  362. }
  363. mutex_unlock(&ictx->lock);
  364. exit:
  365. mutex_unlock(&driver_lock);
  366. return retval;
  367. }
  368. /**
  369. * Called when the display device (e.g. /dev/lcd0)
  370. * is closed by the application.
  371. */
  372. static int display_close(struct inode *inode, struct file *file)
  373. {
  374. struct imon_context *ictx = NULL;
  375. int retval = 0;
  376. ictx = file->private_data;
  377. if (!ictx) {
  378. pr_err("no context for device\n");
  379. return -ENODEV;
  380. }
  381. mutex_lock(&ictx->lock);
  382. if (!ictx->display_supported) {
  383. pr_err("display not supported by device\n");
  384. retval = -ENODEV;
  385. } else if (!ictx->display_isopen) {
  386. pr_err("display is not open\n");
  387. retval = -EIO;
  388. } else {
  389. ictx->display_isopen = false;
  390. dev_dbg(ictx->dev, "display port closed\n");
  391. }
  392. mutex_unlock(&ictx->lock);
  393. return retval;
  394. }
  395. /**
  396. * Sends a packet to the device -- this function must be called with
  397. * ictx->lock held, or its unlock/lock sequence while waiting for tx
  398. * to complete can/will lead to a deadlock.
  399. */
  400. static int send_packet(struct imon_context *ictx)
  401. {
  402. unsigned int pipe;
  403. unsigned long timeout;
  404. int interval = 0;
  405. int retval = 0;
  406. struct usb_ctrlrequest *control_req = NULL;
  407. /* Check if we need to use control or interrupt urb */
  408. if (!ictx->tx_control) {
  409. pipe = usb_sndintpipe(ictx->usbdev_intf0,
  410. ictx->tx_endpoint->bEndpointAddress);
  411. interval = ictx->tx_endpoint->bInterval;
  412. usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
  413. ictx->usb_tx_buf,
  414. sizeof(ictx->usb_tx_buf),
  415. usb_tx_callback, ictx, interval);
  416. ictx->tx_urb->actual_length = 0;
  417. } else {
  418. /* fill request into kmalloc'ed space: */
  419. control_req = kmalloc(sizeof(struct usb_ctrlrequest),
  420. GFP_KERNEL);
  421. if (control_req == NULL)
  422. return -ENOMEM;
  423. /* setup packet is '21 09 0200 0001 0008' */
  424. control_req->bRequestType = 0x21;
  425. control_req->bRequest = 0x09;
  426. control_req->wValue = cpu_to_le16(0x0200);
  427. control_req->wIndex = cpu_to_le16(0x0001);
  428. control_req->wLength = cpu_to_le16(0x0008);
  429. /* control pipe is endpoint 0x00 */
  430. pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
  431. /* build the control urb */
  432. usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
  433. pipe, (unsigned char *)control_req,
  434. ictx->usb_tx_buf,
  435. sizeof(ictx->usb_tx_buf),
  436. usb_tx_callback, ictx);
  437. ictx->tx_urb->actual_length = 0;
  438. }
  439. init_completion(&ictx->tx.finished);
  440. ictx->tx.busy = true;
  441. smp_rmb(); /* ensure later readers know we're busy */
  442. retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
  443. if (retval) {
  444. ictx->tx.busy = false;
  445. smp_rmb(); /* ensure later readers know we're not busy */
  446. pr_err("error submitting urb(%d)\n", retval);
  447. } else {
  448. /* Wait for transmission to complete (or abort) */
  449. mutex_unlock(&ictx->lock);
  450. retval = wait_for_completion_interruptible(
  451. &ictx->tx.finished);
  452. if (retval)
  453. pr_err("task interrupted\n");
  454. mutex_lock(&ictx->lock);
  455. retval = ictx->tx.status;
  456. if (retval)
  457. pr_err("packet tx failed (%d)\n", retval);
  458. }
  459. kfree(control_req);
  460. /*
  461. * Induce a mandatory 5ms delay before returning, as otherwise,
  462. * send_packet can get called so rapidly as to overwhelm the device,
  463. * particularly on faster systems and/or those with quirky usb.
  464. */
  465. timeout = msecs_to_jiffies(5);
  466. set_current_state(TASK_UNINTERRUPTIBLE);
  467. schedule_timeout(timeout);
  468. return retval;
  469. }
  470. /**
  471. * Sends an associate packet to the iMON 2.4G.
  472. *
  473. * This might not be such a good idea, since it has an id collision with
  474. * some versions of the "IR & VFD" combo. The only way to determine if it
  475. * is an RF version is to look at the product description string. (Which
  476. * we currently do not fetch).
  477. */
  478. static int send_associate_24g(struct imon_context *ictx)
  479. {
  480. int retval;
  481. const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
  482. 0x00, 0x00, 0x00, 0x20 };
  483. if (!ictx) {
  484. pr_err("no context for device\n");
  485. return -ENODEV;
  486. }
  487. if (!ictx->dev_present_intf0) {
  488. pr_err("no iMON device present\n");
  489. return -ENODEV;
  490. }
  491. memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
  492. retval = send_packet(ictx);
  493. return retval;
  494. }
  495. /**
  496. * Sends packets to setup and show clock on iMON display
  497. *
  498. * Arguments: year - last 2 digits of year, month - 1..12,
  499. * day - 1..31, dow - day of the week (0-Sun...6-Sat),
  500. * hour - 0..23, minute - 0..59, second - 0..59
  501. */
  502. static int send_set_imon_clock(struct imon_context *ictx,
  503. unsigned int year, unsigned int month,
  504. unsigned int day, unsigned int dow,
  505. unsigned int hour, unsigned int minute,
  506. unsigned int second)
  507. {
  508. unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
  509. int retval = 0;
  510. int i;
  511. if (!ictx) {
  512. pr_err("no context for device\n");
  513. return -ENODEV;
  514. }
  515. switch (ictx->display_type) {
  516. case IMON_DISPLAY_TYPE_LCD:
  517. clock_enable_pkt[0][0] = 0x80;
  518. clock_enable_pkt[0][1] = year;
  519. clock_enable_pkt[0][2] = month-1;
  520. clock_enable_pkt[0][3] = day;
  521. clock_enable_pkt[0][4] = hour;
  522. clock_enable_pkt[0][5] = minute;
  523. clock_enable_pkt[0][6] = second;
  524. clock_enable_pkt[1][0] = 0x80;
  525. clock_enable_pkt[1][1] = 0;
  526. clock_enable_pkt[1][2] = 0;
  527. clock_enable_pkt[1][3] = 0;
  528. clock_enable_pkt[1][4] = 0;
  529. clock_enable_pkt[1][5] = 0;
  530. clock_enable_pkt[1][6] = 0;
  531. if (ictx->product == 0xffdc) {
  532. clock_enable_pkt[0][7] = 0x50;
  533. clock_enable_pkt[1][7] = 0x51;
  534. } else {
  535. clock_enable_pkt[0][7] = 0x88;
  536. clock_enable_pkt[1][7] = 0x8a;
  537. }
  538. break;
  539. case IMON_DISPLAY_TYPE_VFD:
  540. clock_enable_pkt[0][0] = year;
  541. clock_enable_pkt[0][1] = month-1;
  542. clock_enable_pkt[0][2] = day;
  543. clock_enable_pkt[0][3] = dow;
  544. clock_enable_pkt[0][4] = hour;
  545. clock_enable_pkt[0][5] = minute;
  546. clock_enable_pkt[0][6] = second;
  547. clock_enable_pkt[0][7] = 0x40;
  548. clock_enable_pkt[1][0] = 0;
  549. clock_enable_pkt[1][1] = 0;
  550. clock_enable_pkt[1][2] = 1;
  551. clock_enable_pkt[1][3] = 0;
  552. clock_enable_pkt[1][4] = 0;
  553. clock_enable_pkt[1][5] = 0;
  554. clock_enable_pkt[1][6] = 0;
  555. clock_enable_pkt[1][7] = 0x42;
  556. break;
  557. default:
  558. return -ENODEV;
  559. }
  560. for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
  561. memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
  562. retval = send_packet(ictx);
  563. if (retval) {
  564. pr_err("send_packet failed for packet %d\n", i);
  565. break;
  566. }
  567. }
  568. return retval;
  569. }
  570. /**
  571. * These are the sysfs functions to handle the association on the iMON 2.4G LT.
  572. */
  573. static ssize_t show_associate_remote(struct device *d,
  574. struct device_attribute *attr,
  575. char *buf)
  576. {
  577. struct imon_context *ictx = dev_get_drvdata(d);
  578. if (!ictx)
  579. return -ENODEV;
  580. mutex_lock(&ictx->lock);
  581. if (ictx->rf_isassociating)
  582. strcpy(buf, "associating\n");
  583. else
  584. strcpy(buf, "closed\n");
  585. dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
  586. "instructions on how to associate your iMON 2.4G DT/LT "
  587. "remote\n");
  588. mutex_unlock(&ictx->lock);
  589. return strlen(buf);
  590. }
  591. static ssize_t store_associate_remote(struct device *d,
  592. struct device_attribute *attr,
  593. const char *buf, size_t count)
  594. {
  595. struct imon_context *ictx;
  596. ictx = dev_get_drvdata(d);
  597. if (!ictx)
  598. return -ENODEV;
  599. mutex_lock(&ictx->lock);
  600. ictx->rf_isassociating = true;
  601. send_associate_24g(ictx);
  602. mutex_unlock(&ictx->lock);
  603. return count;
  604. }
  605. /**
  606. * sysfs functions to control internal imon clock
  607. */
  608. static ssize_t show_imon_clock(struct device *d,
  609. struct device_attribute *attr, char *buf)
  610. {
  611. struct imon_context *ictx = dev_get_drvdata(d);
  612. size_t len;
  613. if (!ictx)
  614. return -ENODEV;
  615. mutex_lock(&ictx->lock);
  616. if (!ictx->display_supported) {
  617. len = snprintf(buf, PAGE_SIZE, "Not supported.");
  618. } else {
  619. len = snprintf(buf, PAGE_SIZE,
  620. "To set the clock on your iMON display:\n"
  621. "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
  622. "%s", ictx->display_isopen ?
  623. "\nNOTE: imon device must be closed\n" : "");
  624. }
  625. mutex_unlock(&ictx->lock);
  626. return len;
  627. }
  628. static ssize_t store_imon_clock(struct device *d,
  629. struct device_attribute *attr,
  630. const char *buf, size_t count)
  631. {
  632. struct imon_context *ictx = dev_get_drvdata(d);
  633. ssize_t retval;
  634. unsigned int year, month, day, dow, hour, minute, second;
  635. if (!ictx)
  636. return -ENODEV;
  637. mutex_lock(&ictx->lock);
  638. if (!ictx->display_supported) {
  639. retval = -ENODEV;
  640. goto exit;
  641. } else if (ictx->display_isopen) {
  642. retval = -EBUSY;
  643. goto exit;
  644. }
  645. if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
  646. &hour, &minute, &second) != 7) {
  647. retval = -EINVAL;
  648. goto exit;
  649. }
  650. if ((month < 1 || month > 12) ||
  651. (day < 1 || day > 31) || (dow > 6) ||
  652. (hour > 23) || (minute > 59) || (second > 59)) {
  653. retval = -EINVAL;
  654. goto exit;
  655. }
  656. retval = send_set_imon_clock(ictx, year, month, day, dow,
  657. hour, minute, second);
  658. if (retval)
  659. goto exit;
  660. retval = count;
  661. exit:
  662. mutex_unlock(&ictx->lock);
  663. return retval;
  664. }
  665. static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
  666. store_imon_clock);
  667. static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
  668. store_associate_remote);
  669. static struct attribute *imon_display_sysfs_entries[] = {
  670. &dev_attr_imon_clock.attr,
  671. NULL
  672. };
  673. static struct attribute_group imon_display_attr_group = {
  674. .attrs = imon_display_sysfs_entries
  675. };
  676. static struct attribute *imon_rf_sysfs_entries[] = {
  677. &dev_attr_associate_remote.attr,
  678. NULL
  679. };
  680. static struct attribute_group imon_rf_attr_group = {
  681. .attrs = imon_rf_sysfs_entries
  682. };
  683. /**
  684. * Writes data to the VFD. The iMON VFD is 2x16 characters
  685. * and requires data in 5 consecutive USB interrupt packets,
  686. * each packet but the last carrying 7 bytes.
  687. *
  688. * I don't know if the VFD board supports features such as
  689. * scrolling, clearing rows, blanking, etc. so at
  690. * the caller must provide a full screen of data. If fewer
  691. * than 32 bytes are provided spaces will be appended to
  692. * generate a full screen.
  693. */
  694. static ssize_t vfd_write(struct file *file, const char *buf,
  695. size_t n_bytes, loff_t *pos)
  696. {
  697. int i;
  698. int offset;
  699. int seq;
  700. int retval = 0;
  701. struct imon_context *ictx;
  702. const unsigned char vfd_packet6[] = {
  703. 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
  704. ictx = file->private_data;
  705. if (!ictx) {
  706. pr_err("no context for device\n");
  707. return -ENODEV;
  708. }
  709. mutex_lock(&ictx->lock);
  710. if (!ictx->dev_present_intf0) {
  711. pr_err("no iMON device present\n");
  712. retval = -ENODEV;
  713. goto exit;
  714. }
  715. if (n_bytes <= 0 || n_bytes > 32) {
  716. pr_err("invalid payload size\n");
  717. retval = -EINVAL;
  718. goto exit;
  719. }
  720. if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
  721. retval = -EFAULT;
  722. goto exit;
  723. }
  724. /* Pad with spaces */
  725. for (i = n_bytes; i < 32; ++i)
  726. ictx->tx.data_buf[i] = ' ';
  727. for (i = 32; i < 35; ++i)
  728. ictx->tx.data_buf[i] = 0xFF;
  729. offset = 0;
  730. seq = 0;
  731. do {
  732. memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
  733. ictx->usb_tx_buf[7] = (unsigned char) seq;
  734. retval = send_packet(ictx);
  735. if (retval) {
  736. pr_err("send packet failed for packet #%d\n", seq / 2);
  737. goto exit;
  738. } else {
  739. seq += 2;
  740. offset += 7;
  741. }
  742. } while (offset < 35);
  743. /* Send packet #6 */
  744. memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
  745. ictx->usb_tx_buf[7] = (unsigned char) seq;
  746. retval = send_packet(ictx);
  747. if (retval)
  748. pr_err("send packet failed for packet #%d\n", seq / 2);
  749. exit:
  750. mutex_unlock(&ictx->lock);
  751. return (!retval) ? n_bytes : retval;
  752. }
  753. /**
  754. * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
  755. * packets. We accept data as 16 hexadecimal digits, followed by a
  756. * newline (to make it easy to drive the device from a command-line
  757. * -- even though the actual binary data is a bit complicated).
  758. *
  759. * The device itself is not a "traditional" text-mode display. It's
  760. * actually a 16x96 pixel bitmap display. That means if you want to
  761. * display text, you've got to have your own "font" and translate the
  762. * text into bitmaps for display. This is really flexible (you can
  763. * display whatever diacritics you need, and so on), but it's also
  764. * a lot more complicated than most LCDs...
  765. */
  766. static ssize_t lcd_write(struct file *file, const char *buf,
  767. size_t n_bytes, loff_t *pos)
  768. {
  769. int retval = 0;
  770. struct imon_context *ictx;
  771. ictx = file->private_data;
  772. if (!ictx) {
  773. pr_err("no context for device\n");
  774. return -ENODEV;
  775. }
  776. mutex_lock(&ictx->lock);
  777. if (!ictx->display_supported) {
  778. pr_err("no iMON display present\n");
  779. retval = -ENODEV;
  780. goto exit;
  781. }
  782. if (n_bytes != 8) {
  783. pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
  784. retval = -EINVAL;
  785. goto exit;
  786. }
  787. if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
  788. retval = -EFAULT;
  789. goto exit;
  790. }
  791. retval = send_packet(ictx);
  792. if (retval) {
  793. pr_err("send packet failed!\n");
  794. goto exit;
  795. } else {
  796. dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
  797. __func__, (int) n_bytes);
  798. }
  799. exit:
  800. mutex_unlock(&ictx->lock);
  801. return (!retval) ? n_bytes : retval;
  802. }
  803. /**
  804. * Callback function for USB core API: transmit data
  805. */
  806. static void usb_tx_callback(struct urb *urb)
  807. {
  808. struct imon_context *ictx;
  809. if (!urb)
  810. return;
  811. ictx = (struct imon_context *)urb->context;
  812. if (!ictx)
  813. return;
  814. ictx->tx.status = urb->status;
  815. /* notify waiters that write has finished */
  816. ictx->tx.busy = false;
  817. smp_rmb(); /* ensure later readers know we're not busy */
  818. complete(&ictx->tx.finished);
  819. }
  820. /**
  821. * report touchscreen input
  822. */
  823. static void imon_touch_display_timeout(unsigned long data)
  824. {
  825. struct imon_context *ictx = (struct imon_context *)data;
  826. if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
  827. return;
  828. input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
  829. input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
  830. input_report_key(ictx->touch, BTN_TOUCH, 0x00);
  831. input_sync(ictx->touch);
  832. }
  833. /**
  834. * iMON IR receivers support two different signal sets -- those used by
  835. * the iMON remotes, and those used by the Windows MCE remotes (which is
  836. * really just RC-6), but only one or the other at a time, as the signals
  837. * are decoded onboard the receiver.
  838. *
  839. * This function gets called two different ways, one way is from
  840. * rc_register_device, for initial protocol selection/setup, and the other is
  841. * via a userspace-initiated protocol change request, either by direct sysfs
  842. * prodding or by something like ir-keytable. In the rc_register_device case,
  843. * the imon context lock is already held, but when initiated from userspace,
  844. * it is not, so we must acquire it prior to calling send_packet, which
  845. * requires that the lock is held.
  846. */
  847. static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
  848. {
  849. int retval;
  850. struct imon_context *ictx = rc->priv;
  851. struct device *dev = ictx->dev;
  852. bool unlock = false;
  853. unsigned char ir_proto_packet[] = {
  854. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
  855. if (rc_type && !(rc_type & rc->allowed_protos))
  856. dev_warn(dev, "Looks like you're trying to use an IR protocol "
  857. "this device does not support\n");
  858. switch (rc_type) {
  859. case RC_TYPE_RC6:
  860. dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
  861. ir_proto_packet[0] = 0x01;
  862. break;
  863. case RC_TYPE_UNKNOWN:
  864. case RC_TYPE_OTHER:
  865. dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
  866. if (!pad_stabilize)
  867. dev_dbg(dev, "PAD stabilize functionality disabled\n");
  868. /* ir_proto_packet[0] = 0x00; // already the default */
  869. rc_type = RC_TYPE_OTHER;
  870. break;
  871. default:
  872. dev_warn(dev, "Unsupported IR protocol specified, overriding "
  873. "to iMON IR protocol\n");
  874. if (!pad_stabilize)
  875. dev_dbg(dev, "PAD stabilize functionality disabled\n");
  876. /* ir_proto_packet[0] = 0x00; // already the default */
  877. rc_type = RC_TYPE_OTHER;
  878. break;
  879. }
  880. memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
  881. if (!mutex_is_locked(&ictx->lock)) {
  882. unlock = true;
  883. mutex_lock(&ictx->lock);
  884. }
  885. retval = send_packet(ictx);
  886. if (retval)
  887. goto out;
  888. ictx->rc_type = rc_type;
  889. ictx->pad_mouse = false;
  890. out:
  891. if (unlock)
  892. mutex_unlock(&ictx->lock);
  893. return retval;
  894. }
  895. static inline int tv2int(const struct timeval *a, const struct timeval *b)
  896. {
  897. int usecs = 0;
  898. int sec = 0;
  899. if (b->tv_usec > a->tv_usec) {
  900. usecs = 1000000;
  901. sec--;
  902. }
  903. usecs += a->tv_usec - b->tv_usec;
  904. sec += a->tv_sec - b->tv_sec;
  905. sec *= 1000;
  906. usecs /= 1000;
  907. sec += usecs;
  908. if (sec < 0)
  909. sec = 1000;
  910. return sec;
  911. }
  912. /**
  913. * The directional pad behaves a bit differently, depending on whether this is
  914. * one of the older ffdc devices or a newer device. Newer devices appear to
  915. * have a higher resolution matrix for more precise mouse movement, but it
  916. * makes things overly sensitive in keyboard mode, so we do some interesting
  917. * contortions to make it less touchy. Older devices run through the same
  918. * routine with shorter timeout and a smaller threshold.
  919. */
  920. static int stabilize(int a, int b, u16 timeout, u16 threshold)
  921. {
  922. struct timeval ct;
  923. static struct timeval prev_time = {0, 0};
  924. static struct timeval hit_time = {0, 0};
  925. static int x, y, prev_result, hits;
  926. int result = 0;
  927. int msec, msec_hit;
  928. do_gettimeofday(&ct);
  929. msec = tv2int(&ct, &prev_time);
  930. msec_hit = tv2int(&ct, &hit_time);
  931. if (msec > 100) {
  932. x = 0;
  933. y = 0;
  934. hits = 0;
  935. }
  936. x += a;
  937. y += b;
  938. prev_time = ct;
  939. if (abs(x) > threshold || abs(y) > threshold) {
  940. if (abs(y) > abs(x))
  941. result = (y > 0) ? 0x7F : 0x80;
  942. else
  943. result = (x > 0) ? 0x7F00 : 0x8000;
  944. x = 0;
  945. y = 0;
  946. if (result == prev_result) {
  947. hits++;
  948. if (hits > 3) {
  949. switch (result) {
  950. case 0x7F:
  951. y = 17 * threshold / 30;
  952. break;
  953. case 0x80:
  954. y -= 17 * threshold / 30;
  955. break;
  956. case 0x7F00:
  957. x = 17 * threshold / 30;
  958. break;
  959. case 0x8000:
  960. x -= 17 * threshold / 30;
  961. break;
  962. }
  963. }
  964. if (hits == 2 && msec_hit < timeout) {
  965. result = 0;
  966. hits = 1;
  967. }
  968. } else {
  969. prev_result = result;
  970. hits = 1;
  971. hit_time = ct;
  972. }
  973. }
  974. return result;
  975. }
  976. static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
  977. {
  978. u32 keycode;
  979. u32 release;
  980. bool is_release_code = false;
  981. /* Look for the initial press of a button */
  982. keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
  983. ictx->rc_toggle = 0x0;
  984. ictx->rc_scancode = scancode;
  985. /* Look for the release of a button */
  986. if (keycode == KEY_RESERVED) {
  987. release = scancode & ~0x4000;
  988. keycode = rc_g_keycode_from_table(ictx->rdev, release);
  989. if (keycode != KEY_RESERVED)
  990. is_release_code = true;
  991. }
  992. ictx->release_code = is_release_code;
  993. return keycode;
  994. }
  995. static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
  996. {
  997. u32 keycode;
  998. #define MCE_KEY_MASK 0x7000
  999. #define MCE_TOGGLE_BIT 0x8000
  1000. /*
  1001. * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
  1002. * (the toggle bit flipping between alternating key presses), while
  1003. * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
  1004. * the table trim, we always or in the bits to look up 0x8000ff4xx,
  1005. * but we can't or them into all codes, as some keys are decoded in
  1006. * a different way w/o the same use of the toggle bit...
  1007. */
  1008. if (scancode & 0x80000000)
  1009. scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
  1010. ictx->rc_scancode = scancode;
  1011. keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
  1012. /* not used in mce mode, but make sure we know its false */
  1013. ictx->release_code = false;
  1014. return keycode;
  1015. }
  1016. static u32 imon_panel_key_lookup(u64 code)
  1017. {
  1018. int i;
  1019. u32 keycode = KEY_RESERVED;
  1020. for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
  1021. if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
  1022. keycode = imon_panel_key_table[i].keycode;
  1023. break;
  1024. }
  1025. }
  1026. return keycode;
  1027. }
  1028. static bool imon_mouse_event(struct imon_context *ictx,
  1029. unsigned char *buf, int len)
  1030. {
  1031. char rel_x = 0x00, rel_y = 0x00;
  1032. u8 right_shift = 1;
  1033. bool mouse_input = true;
  1034. int dir = 0;
  1035. unsigned long flags;
  1036. spin_lock_irqsave(&ictx->kc_lock, flags);
  1037. /* newer iMON device PAD or mouse button */
  1038. if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
  1039. rel_x = buf[2];
  1040. rel_y = buf[3];
  1041. right_shift = 1;
  1042. /* 0xffdc iMON PAD or mouse button input */
  1043. } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
  1044. !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
  1045. rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
  1046. (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
  1047. if (buf[0] & 0x02)
  1048. rel_x |= ~0x0f;
  1049. rel_x = rel_x + rel_x / 2;
  1050. rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
  1051. (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
  1052. if (buf[0] & 0x01)
  1053. rel_y |= ~0x0f;
  1054. rel_y = rel_y + rel_y / 2;
  1055. right_shift = 2;
  1056. /* some ffdc devices decode mouse buttons differently... */
  1057. } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
  1058. right_shift = 2;
  1059. /* ch+/- buttons, which we use for an emulated scroll wheel */
  1060. } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
  1061. dir = 1;
  1062. } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
  1063. dir = -1;
  1064. } else
  1065. mouse_input = false;
  1066. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1067. if (mouse_input) {
  1068. dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
  1069. if (dir) {
  1070. input_report_rel(ictx->idev, REL_WHEEL, dir);
  1071. } else if (rel_x || rel_y) {
  1072. input_report_rel(ictx->idev, REL_X, rel_x);
  1073. input_report_rel(ictx->idev, REL_Y, rel_y);
  1074. } else {
  1075. input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
  1076. input_report_key(ictx->idev, BTN_RIGHT,
  1077. buf[1] >> right_shift & 0x1);
  1078. }
  1079. input_sync(ictx->idev);
  1080. spin_lock_irqsave(&ictx->kc_lock, flags);
  1081. ictx->last_keycode = ictx->kc;
  1082. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1083. }
  1084. return mouse_input;
  1085. }
  1086. static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
  1087. {
  1088. mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
  1089. ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
  1090. ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
  1091. input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
  1092. input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
  1093. input_report_key(ictx->touch, BTN_TOUCH, 0x01);
  1094. input_sync(ictx->touch);
  1095. }
  1096. static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
  1097. {
  1098. int dir = 0;
  1099. char rel_x = 0x00, rel_y = 0x00;
  1100. u16 timeout, threshold;
  1101. u32 scancode = KEY_RESERVED;
  1102. unsigned long flags;
  1103. /*
  1104. * The imon directional pad functions more like a touchpad. Bytes 3 & 4
  1105. * contain a position coordinate (x,y), with each component ranging
  1106. * from -14 to 14. We want to down-sample this to only 4 discrete values
  1107. * for up/down/left/right arrow keys. Also, when you get too close to
  1108. * diagonals, it has a tendency to jump back and forth, so lets try to
  1109. * ignore when they get too close.
  1110. */
  1111. if (ictx->product != 0xffdc) {
  1112. /* first, pad to 8 bytes so it conforms with everything else */
  1113. buf[5] = buf[6] = buf[7] = 0;
  1114. timeout = 500; /* in msecs */
  1115. /* (2*threshold) x (2*threshold) square */
  1116. threshold = pad_thresh ? pad_thresh : 28;
  1117. rel_x = buf[2];
  1118. rel_y = buf[3];
  1119. if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
  1120. if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
  1121. dir = stabilize((int)rel_x, (int)rel_y,
  1122. timeout, threshold);
  1123. if (!dir) {
  1124. spin_lock_irqsave(&ictx->kc_lock,
  1125. flags);
  1126. ictx->kc = KEY_UNKNOWN;
  1127. spin_unlock_irqrestore(&ictx->kc_lock,
  1128. flags);
  1129. return;
  1130. }
  1131. buf[2] = dir & 0xFF;
  1132. buf[3] = (dir >> 8) & 0xFF;
  1133. scancode = be32_to_cpu(*((u32 *)buf));
  1134. }
  1135. } else {
  1136. /*
  1137. * Hack alert: instead of using keycodes, we have
  1138. * to use hard-coded scancodes here...
  1139. */
  1140. if (abs(rel_y) > abs(rel_x)) {
  1141. buf[2] = (rel_y > 0) ? 0x7F : 0x80;
  1142. buf[3] = 0;
  1143. if (rel_y > 0)
  1144. scancode = 0x01007f00; /* KEY_DOWN */
  1145. else
  1146. scancode = 0x01008000; /* KEY_UP */
  1147. } else {
  1148. buf[2] = 0;
  1149. buf[3] = (rel_x > 0) ? 0x7F : 0x80;
  1150. if (rel_x > 0)
  1151. scancode = 0x0100007f; /* KEY_RIGHT */
  1152. else
  1153. scancode = 0x01000080; /* KEY_LEFT */
  1154. }
  1155. }
  1156. /*
  1157. * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
  1158. * device (15c2:ffdc). The remote generates various codes from
  1159. * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
  1160. * 0x688301b7 and the right one 0x688481b7. All other keys generate
  1161. * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
  1162. * reversed endianess. Extract direction from buffer, rotate endianess,
  1163. * adjust sign and feed the values into stabilize(). The resulting codes
  1164. * will be 0x01008000, 0x01007F00, which match the newer devices.
  1165. */
  1166. } else {
  1167. timeout = 10; /* in msecs */
  1168. /* (2*threshold) x (2*threshold) square */
  1169. threshold = pad_thresh ? pad_thresh : 15;
  1170. /* buf[1] is x */
  1171. rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
  1172. (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
  1173. if (buf[0] & 0x02)
  1174. rel_x |= ~0x10+1;
  1175. /* buf[2] is y */
  1176. rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
  1177. (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
  1178. if (buf[0] & 0x01)
  1179. rel_y |= ~0x10+1;
  1180. buf[0] = 0x01;
  1181. buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
  1182. if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
  1183. dir = stabilize((int)rel_x, (int)rel_y,
  1184. timeout, threshold);
  1185. if (!dir) {
  1186. spin_lock_irqsave(&ictx->kc_lock, flags);
  1187. ictx->kc = KEY_UNKNOWN;
  1188. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1189. return;
  1190. }
  1191. buf[2] = dir & 0xFF;
  1192. buf[3] = (dir >> 8) & 0xFF;
  1193. scancode = be32_to_cpu(*((u32 *)buf));
  1194. } else {
  1195. /*
  1196. * Hack alert: instead of using keycodes, we have
  1197. * to use hard-coded scancodes here...
  1198. */
  1199. if (abs(rel_y) > abs(rel_x)) {
  1200. buf[2] = (rel_y > 0) ? 0x7F : 0x80;
  1201. buf[3] = 0;
  1202. if (rel_y > 0)
  1203. scancode = 0x01007f00; /* KEY_DOWN */
  1204. else
  1205. scancode = 0x01008000; /* KEY_UP */
  1206. } else {
  1207. buf[2] = 0;
  1208. buf[3] = (rel_x > 0) ? 0x7F : 0x80;
  1209. if (rel_x > 0)
  1210. scancode = 0x0100007f; /* KEY_RIGHT */
  1211. else
  1212. scancode = 0x01000080; /* KEY_LEFT */
  1213. }
  1214. }
  1215. }
  1216. if (scancode) {
  1217. spin_lock_irqsave(&ictx->kc_lock, flags);
  1218. ictx->kc = imon_remote_key_lookup(ictx, scancode);
  1219. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1220. }
  1221. }
  1222. /**
  1223. * figure out if these is a press or a release. We don't actually
  1224. * care about repeats, as those will be auto-generated within the IR
  1225. * subsystem for repeating scancodes.
  1226. */
  1227. static int imon_parse_press_type(struct imon_context *ictx,
  1228. unsigned char *buf, u8 ktype)
  1229. {
  1230. int press_type = 0;
  1231. unsigned long flags;
  1232. spin_lock_irqsave(&ictx->kc_lock, flags);
  1233. /* key release of 0x02XXXXXX key */
  1234. if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
  1235. ictx->kc = ictx->last_keycode;
  1236. /* mouse button release on (some) 0xffdc devices */
  1237. else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
  1238. buf[2] == 0x81 && buf[3] == 0xb7)
  1239. ictx->kc = ictx->last_keycode;
  1240. /* mouse button release on (some other) 0xffdc devices */
  1241. else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
  1242. buf[2] == 0x81 && buf[3] == 0xb7)
  1243. ictx->kc = ictx->last_keycode;
  1244. /* mce-specific button handling, no keyup events */
  1245. else if (ktype == IMON_KEY_MCE) {
  1246. ictx->rc_toggle = buf[2];
  1247. press_type = 1;
  1248. /* incoherent or irrelevant data */
  1249. } else if (ictx->kc == KEY_RESERVED)
  1250. press_type = -EINVAL;
  1251. /* key release of 0xXXXXXXb7 key */
  1252. else if (ictx->release_code)
  1253. press_type = 0;
  1254. /* this is a button press */
  1255. else
  1256. press_type = 1;
  1257. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1258. return press_type;
  1259. }
  1260. /**
  1261. * Process the incoming packet
  1262. */
  1263. static void imon_incoming_packet(struct imon_context *ictx,
  1264. struct urb *urb, int intf)
  1265. {
  1266. int len = urb->actual_length;
  1267. unsigned char *buf = urb->transfer_buffer;
  1268. struct device *dev = ictx->dev;
  1269. unsigned long flags;
  1270. u32 kc;
  1271. int i;
  1272. u64 scancode;
  1273. int press_type = 0;
  1274. int msec;
  1275. struct timeval t;
  1276. static struct timeval prev_time = { 0, 0 };
  1277. u8 ktype;
  1278. /* filter out junk data on the older 0xffdc imon devices */
  1279. if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
  1280. return;
  1281. /* Figure out what key was pressed */
  1282. if (len == 8 && buf[7] == 0xee) {
  1283. scancode = be64_to_cpu(*((u64 *)buf));
  1284. ktype = IMON_KEY_PANEL;
  1285. kc = imon_panel_key_lookup(scancode);
  1286. } else {
  1287. scancode = be32_to_cpu(*((u32 *)buf));
  1288. if (ictx->rc_type == RC_TYPE_RC6) {
  1289. ktype = IMON_KEY_IMON;
  1290. if (buf[0] == 0x80)
  1291. ktype = IMON_KEY_MCE;
  1292. kc = imon_mce_key_lookup(ictx, scancode);
  1293. } else {
  1294. ktype = IMON_KEY_IMON;
  1295. kc = imon_remote_key_lookup(ictx, scancode);
  1296. }
  1297. }
  1298. spin_lock_irqsave(&ictx->kc_lock, flags);
  1299. /* keyboard/mouse mode toggle button */
  1300. if (kc == KEY_KEYBOARD && !ictx->release_code) {
  1301. ictx->last_keycode = kc;
  1302. if (!nomouse) {
  1303. ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
  1304. dev_dbg(dev, "toggling to %s mode\n",
  1305. ictx->pad_mouse ? "mouse" : "keyboard");
  1306. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1307. return;
  1308. } else {
  1309. ictx->pad_mouse = false;
  1310. dev_dbg(dev, "mouse mode disabled, passing key value\n");
  1311. }
  1312. }
  1313. ictx->kc = kc;
  1314. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1315. /* send touchscreen events through input subsystem if touchpad data */
  1316. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
  1317. buf[7] == 0x86) {
  1318. imon_touch_event(ictx, buf);
  1319. return;
  1320. /* look for mouse events with pad in mouse mode */
  1321. } else if (ictx->pad_mouse) {
  1322. if (imon_mouse_event(ictx, buf, len))
  1323. return;
  1324. }
  1325. /* Now for some special handling to convert pad input to arrow keys */
  1326. if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
  1327. ((len == 8) && (buf[0] & 0x40) &&
  1328. !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
  1329. len = 8;
  1330. imon_pad_to_keys(ictx, buf);
  1331. }
  1332. if (debug) {
  1333. printk(KERN_INFO "intf%d decoded packet: ", intf);
  1334. for (i = 0; i < len; ++i)
  1335. printk("%02x ", buf[i]);
  1336. printk("\n");
  1337. }
  1338. press_type = imon_parse_press_type(ictx, buf, ktype);
  1339. if (press_type < 0)
  1340. goto not_input_data;
  1341. spin_lock_irqsave(&ictx->kc_lock, flags);
  1342. if (ictx->kc == KEY_UNKNOWN)
  1343. goto unknown_key;
  1344. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1345. if (ktype != IMON_KEY_PANEL) {
  1346. if (press_type == 0)
  1347. rc_keyup(ictx->rdev);
  1348. else {
  1349. rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
  1350. spin_lock_irqsave(&ictx->kc_lock, flags);
  1351. ictx->last_keycode = ictx->kc;
  1352. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1353. }
  1354. return;
  1355. }
  1356. /* Only panel type events left to process now */
  1357. spin_lock_irqsave(&ictx->kc_lock, flags);
  1358. do_gettimeofday(&t);
  1359. /* KEY_MUTE repeats from knob need to be suppressed */
  1360. if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
  1361. msec = tv2int(&t, &prev_time);
  1362. if (msec < ictx->idev->rep[REP_DELAY]) {
  1363. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1364. return;
  1365. }
  1366. }
  1367. prev_time = t;
  1368. kc = ictx->kc;
  1369. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1370. input_report_key(ictx->idev, kc, press_type);
  1371. input_sync(ictx->idev);
  1372. /* panel keys don't generate a release */
  1373. input_report_key(ictx->idev, kc, 0);
  1374. input_sync(ictx->idev);
  1375. spin_lock_irqsave(&ictx->kc_lock, flags);
  1376. ictx->last_keycode = kc;
  1377. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1378. return;
  1379. unknown_key:
  1380. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1381. dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
  1382. (long long)scancode);
  1383. return;
  1384. not_input_data:
  1385. if (len != 8) {
  1386. dev_warn(dev, "imon %s: invalid incoming packet "
  1387. "size (len = %d, intf%d)\n", __func__, len, intf);
  1388. return;
  1389. }
  1390. /* iMON 2.4G associate frame */
  1391. if (buf[0] == 0x00 &&
  1392. buf[2] == 0xFF && /* REFID */
  1393. buf[3] == 0xFF &&
  1394. buf[4] == 0xFF &&
  1395. buf[5] == 0xFF && /* iMON 2.4G */
  1396. ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
  1397. (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
  1398. dev_warn(dev, "%s: remote associated refid=%02X\n",
  1399. __func__, buf[1]);
  1400. ictx->rf_isassociating = false;
  1401. }
  1402. }
  1403. /**
  1404. * Callback function for USB core API: receive data
  1405. */
  1406. static void usb_rx_callback_intf0(struct urb *urb)
  1407. {
  1408. struct imon_context *ictx;
  1409. int intfnum = 0;
  1410. if (!urb)
  1411. return;
  1412. ictx = (struct imon_context *)urb->context;
  1413. if (!ictx)
  1414. return;
  1415. switch (urb->status) {
  1416. case -ENOENT: /* usbcore unlink successful! */
  1417. return;
  1418. case -ESHUTDOWN: /* transport endpoint was shut down */
  1419. break;
  1420. case 0:
  1421. imon_incoming_packet(ictx, urb, intfnum);
  1422. break;
  1423. default:
  1424. dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
  1425. __func__, urb->status);
  1426. break;
  1427. }
  1428. usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
  1429. }
  1430. static void usb_rx_callback_intf1(struct urb *urb)
  1431. {
  1432. struct imon_context *ictx;
  1433. int intfnum = 1;
  1434. if (!urb)
  1435. return;
  1436. ictx = (struct imon_context *)urb->context;
  1437. if (!ictx)
  1438. return;
  1439. switch (urb->status) {
  1440. case -ENOENT: /* usbcore unlink successful! */
  1441. return;
  1442. case -ESHUTDOWN: /* transport endpoint was shut down */
  1443. break;
  1444. case 0:
  1445. imon_incoming_packet(ictx, urb, intfnum);
  1446. break;
  1447. default:
  1448. dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
  1449. __func__, urb->status);
  1450. break;
  1451. }
  1452. usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
  1453. }
  1454. /*
  1455. * The 0x15c2:0xffdc device ID was used for umpteen different imon
  1456. * devices, and all of them constantly spew interrupts, even when there
  1457. * is no actual data to report. However, byte 6 of this buffer looks like
  1458. * its unique across device variants, so we're trying to key off that to
  1459. * figure out which display type (if any) and what IR protocol the device
  1460. * actually supports. These devices have their IR protocol hard-coded into
  1461. * their firmware, they can't be changed on the fly like the newer hardware.
  1462. */
  1463. static void imon_get_ffdc_type(struct imon_context *ictx)
  1464. {
  1465. u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
  1466. u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
  1467. u64 allowed_protos = RC_TYPE_OTHER;
  1468. switch (ffdc_cfg_byte) {
  1469. /* iMON Knob, no display, iMON IR + vol knob */
  1470. case 0x21:
  1471. dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
  1472. ictx->display_supported = false;
  1473. break;
  1474. /* iMON 2.4G LT (usb stick), no display, iMON RF */
  1475. case 0x4e:
  1476. dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
  1477. ictx->display_supported = false;
  1478. ictx->rf_device = true;
  1479. break;
  1480. /* iMON VFD, no IR (does have vol knob tho) */
  1481. case 0x35:
  1482. dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
  1483. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1484. break;
  1485. /* iMON VFD, iMON IR */
  1486. case 0x24:
  1487. case 0x85:
  1488. dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
  1489. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1490. break;
  1491. /* iMON VFD, MCE IR */
  1492. case 0x46:
  1493. case 0x7e:
  1494. case 0x9e:
  1495. dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
  1496. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1497. allowed_protos = RC_TYPE_RC6;
  1498. break;
  1499. /* iMON LCD, MCE IR */
  1500. case 0x9f:
  1501. dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
  1502. detected_display_type = IMON_DISPLAY_TYPE_LCD;
  1503. allowed_protos = RC_TYPE_RC6;
  1504. break;
  1505. default:
  1506. dev_info(ictx->dev, "Unknown 0xffdc device, "
  1507. "defaulting to VFD and iMON IR");
  1508. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1509. /* We don't know which one it is, allow user to set the
  1510. * RC6 one from userspace if OTHER wasn't correct. */
  1511. allowed_protos |= RC_TYPE_RC6;
  1512. break;
  1513. }
  1514. printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
  1515. ictx->display_type = detected_display_type;
  1516. ictx->rc_type = allowed_protos;
  1517. }
  1518. static void imon_set_display_type(struct imon_context *ictx)
  1519. {
  1520. u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
  1521. /*
  1522. * Try to auto-detect the type of display if the user hasn't set
  1523. * it by hand via the display_type modparam. Default is VFD.
  1524. */
  1525. if (display_type == IMON_DISPLAY_TYPE_AUTO) {
  1526. switch (ictx->product) {
  1527. case 0xffdc:
  1528. /* set in imon_get_ffdc_type() */
  1529. configured_display_type = ictx->display_type;
  1530. break;
  1531. case 0x0034:
  1532. case 0x0035:
  1533. configured_display_type = IMON_DISPLAY_TYPE_VGA;
  1534. break;
  1535. case 0x0038:
  1536. case 0x0039:
  1537. case 0x0045:
  1538. configured_display_type = IMON_DISPLAY_TYPE_LCD;
  1539. break;
  1540. case 0x003c:
  1541. case 0x0041:
  1542. case 0x0042:
  1543. case 0x0043:
  1544. configured_display_type = IMON_DISPLAY_TYPE_NONE;
  1545. ictx->display_supported = false;
  1546. break;
  1547. case 0x0036:
  1548. case 0x0044:
  1549. default:
  1550. configured_display_type = IMON_DISPLAY_TYPE_VFD;
  1551. break;
  1552. }
  1553. } else {
  1554. configured_display_type = display_type;
  1555. if (display_type == IMON_DISPLAY_TYPE_NONE)
  1556. ictx->display_supported = false;
  1557. else
  1558. ictx->display_supported = true;
  1559. dev_info(ictx->dev, "%s: overriding display type to %d via "
  1560. "modparam\n", __func__, display_type);
  1561. }
  1562. ictx->display_type = configured_display_type;
  1563. }
  1564. static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
  1565. {
  1566. struct rc_dev *rdev;
  1567. int ret;
  1568. const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
  1569. 0x00, 0x00, 0x00, 0x88 };
  1570. rdev = rc_allocate_device();
  1571. if (!rdev) {
  1572. dev_err(ictx->dev, "remote control dev allocation failed\n");
  1573. goto out;
  1574. }
  1575. snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
  1576. "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
  1577. usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
  1578. sizeof(ictx->phys_rdev));
  1579. strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
  1580. rdev->input_name = ictx->name_rdev;
  1581. rdev->input_phys = ictx->phys_rdev;
  1582. usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
  1583. rdev->dev.parent = ictx->dev;
  1584. rdev->priv = ictx;
  1585. rdev->driver_type = RC_DRIVER_SCANCODE;
  1586. rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
  1587. rdev->change_protocol = imon_ir_change_protocol;
  1588. rdev->driver_name = MOD_NAME;
  1589. /* Enable front-panel buttons and/or knobs */
  1590. memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
  1591. ret = send_packet(ictx);
  1592. /* Not fatal, but warn about it */
  1593. if (ret)
  1594. dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
  1595. if (ictx->product == 0xffdc) {
  1596. imon_get_ffdc_type(ictx);
  1597. rdev->allowed_protos = ictx->rc_type;
  1598. }
  1599. imon_set_display_type(ictx);
  1600. if (ictx->rc_type == RC_TYPE_RC6)
  1601. rdev->map_name = RC_MAP_IMON_MCE;
  1602. else
  1603. rdev->map_name = RC_MAP_IMON_PAD;
  1604. ret = rc_register_device(rdev);
  1605. if (ret < 0) {
  1606. dev_err(ictx->dev, "remote input dev register failed\n");
  1607. goto out;
  1608. }
  1609. return rdev;
  1610. out:
  1611. rc_free_device(rdev);
  1612. return NULL;
  1613. }
  1614. static struct input_dev *imon_init_idev(struct imon_context *ictx)
  1615. {
  1616. struct input_dev *idev;
  1617. int ret, i;
  1618. idev = input_allocate_device();
  1619. if (!idev) {
  1620. dev_err(ictx->dev, "input dev allocation failed\n");
  1621. goto out;
  1622. }
  1623. snprintf(ictx->name_idev, sizeof(ictx->name_idev),
  1624. "iMON Panel, Knob and Mouse(%04x:%04x)",
  1625. ictx->vendor, ictx->product);
  1626. idev->name = ictx->name_idev;
  1627. usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
  1628. sizeof(ictx->phys_idev));
  1629. strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
  1630. idev->phys = ictx->phys_idev;
  1631. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
  1632. idev->keybit[BIT_WORD(BTN_MOUSE)] =
  1633. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1634. idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
  1635. BIT_MASK(REL_WHEEL);
  1636. /* panel and/or knob code support */
  1637. for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
  1638. u32 kc = imon_panel_key_table[i].keycode;
  1639. __set_bit(kc, idev->keybit);
  1640. }
  1641. usb_to_input_id(ictx->usbdev_intf0, &idev->id);
  1642. idev->dev.parent = ictx->dev;
  1643. input_set_drvdata(idev, ictx);
  1644. ret = input_register_device(idev);
  1645. if (ret < 0) {
  1646. dev_err(ictx->dev, "input dev register failed\n");
  1647. goto out;
  1648. }
  1649. return idev;
  1650. out:
  1651. input_free_device(idev);
  1652. return NULL;
  1653. }
  1654. static struct input_dev *imon_init_touch(struct imon_context *ictx)
  1655. {
  1656. struct input_dev *touch;
  1657. int ret;
  1658. touch = input_allocate_device();
  1659. if (!touch) {
  1660. dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
  1661. goto touch_alloc_failed;
  1662. }
  1663. snprintf(ictx->name_touch, sizeof(ictx->name_touch),
  1664. "iMON USB Touchscreen (%04x:%04x)",
  1665. ictx->vendor, ictx->product);
  1666. touch->name = ictx->name_touch;
  1667. usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
  1668. sizeof(ictx->phys_touch));
  1669. strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
  1670. touch->phys = ictx->phys_touch;
  1671. touch->evbit[0] =
  1672. BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1673. touch->keybit[BIT_WORD(BTN_TOUCH)] =
  1674. BIT_MASK(BTN_TOUCH);
  1675. input_set_abs_params(touch, ABS_X,
  1676. 0x00, 0xfff, 0, 0);
  1677. input_set_abs_params(touch, ABS_Y,
  1678. 0x00, 0xfff, 0, 0);
  1679. input_set_drvdata(touch, ictx);
  1680. usb_to_input_id(ictx->usbdev_intf1, &touch->id);
  1681. touch->dev.parent = ictx->dev;
  1682. ret = input_register_device(touch);
  1683. if (ret < 0) {
  1684. dev_info(ictx->dev, "touchscreen input dev register failed\n");
  1685. goto touch_register_failed;
  1686. }
  1687. return touch;
  1688. touch_register_failed:
  1689. input_free_device(touch);
  1690. touch_alloc_failed:
  1691. return NULL;
  1692. }
  1693. static bool imon_find_endpoints(struct imon_context *ictx,
  1694. struct usb_host_interface *iface_desc)
  1695. {
  1696. struct usb_endpoint_descriptor *ep;
  1697. struct usb_endpoint_descriptor *rx_endpoint = NULL;
  1698. struct usb_endpoint_descriptor *tx_endpoint = NULL;
  1699. int ifnum = iface_desc->desc.bInterfaceNumber;
  1700. int num_endpts = iface_desc->desc.bNumEndpoints;
  1701. int i, ep_dir, ep_type;
  1702. bool ir_ep_found = false;
  1703. bool display_ep_found = false;
  1704. bool tx_control = false;
  1705. /*
  1706. * Scan the endpoint list and set:
  1707. * first input endpoint = IR endpoint
  1708. * first output endpoint = display endpoint
  1709. */
  1710. for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
  1711. ep = &iface_desc->endpoint[i].desc;
  1712. ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
  1713. ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  1714. if (!ir_ep_found && ep_dir == USB_DIR_IN &&
  1715. ep_type == USB_ENDPOINT_XFER_INT) {
  1716. rx_endpoint = ep;
  1717. ir_ep_found = true;
  1718. dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
  1719. } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
  1720. ep_type == USB_ENDPOINT_XFER_INT) {
  1721. tx_endpoint = ep;
  1722. display_ep_found = true;
  1723. dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
  1724. }
  1725. }
  1726. if (ifnum == 0) {
  1727. ictx->rx_endpoint_intf0 = rx_endpoint;
  1728. /*
  1729. * tx is used to send characters to lcd/vfd, associate RF
  1730. * remotes, set IR protocol, and maybe more...
  1731. */
  1732. ictx->tx_endpoint = tx_endpoint;
  1733. } else {
  1734. ictx->rx_endpoint_intf1 = rx_endpoint;
  1735. }
  1736. /*
  1737. * If we didn't find a display endpoint, this is probably one of the
  1738. * newer iMON devices that use control urb instead of interrupt
  1739. */
  1740. if (!display_ep_found) {
  1741. tx_control = true;
  1742. display_ep_found = true;
  1743. dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
  1744. "interface OUT endpoint\n", __func__);
  1745. }
  1746. /*
  1747. * Some iMON receivers have no display. Unfortunately, it seems
  1748. * that SoundGraph recycles device IDs between devices both with
  1749. * and without... :\
  1750. */
  1751. if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
  1752. display_ep_found = false;
  1753. dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
  1754. }
  1755. /*
  1756. * iMON Touch devices have a VGA touchscreen, but no "display", as
  1757. * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
  1758. */
  1759. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1760. display_ep_found = false;
  1761. dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
  1762. }
  1763. /* Input endpoint is mandatory */
  1764. if (!ir_ep_found)
  1765. pr_err("no valid input (IR) endpoint found\n");
  1766. ictx->tx_control = tx_control;
  1767. if (display_ep_found)
  1768. ictx->display_supported = true;
  1769. return ir_ep_found;
  1770. }
  1771. static struct imon_context *imon_init_intf0(struct usb_interface *intf)
  1772. {
  1773. struct imon_context *ictx;
  1774. struct urb *rx_urb;
  1775. struct urb *tx_urb;
  1776. struct device *dev = &intf->dev;
  1777. struct usb_host_interface *iface_desc;
  1778. int ret = -ENOMEM;
  1779. ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
  1780. if (!ictx) {
  1781. dev_err(dev, "%s: kzalloc failed for context", __func__);
  1782. goto exit;
  1783. }
  1784. rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1785. if (!rx_urb) {
  1786. dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
  1787. goto rx_urb_alloc_failed;
  1788. }
  1789. tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1790. if (!tx_urb) {
  1791. dev_err(dev, "%s: usb_alloc_urb failed for display urb",
  1792. __func__);
  1793. goto tx_urb_alloc_failed;
  1794. }
  1795. mutex_init(&ictx->lock);
  1796. spin_lock_init(&ictx->kc_lock);
  1797. mutex_lock(&ictx->lock);
  1798. ictx->dev = dev;
  1799. ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
  1800. ictx->dev_present_intf0 = true;
  1801. ictx->rx_urb_intf0 = rx_urb;
  1802. ictx->tx_urb = tx_urb;
  1803. ictx->rf_device = false;
  1804. ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
  1805. ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
  1806. ret = -ENODEV;
  1807. iface_desc = intf->cur_altsetting;
  1808. if (!imon_find_endpoints(ictx, iface_desc)) {
  1809. goto find_endpoint_failed;
  1810. }
  1811. usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
  1812. usb_rcvintpipe(ictx->usbdev_intf0,
  1813. ictx->rx_endpoint_intf0->bEndpointAddress),
  1814. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  1815. usb_rx_callback_intf0, ictx,
  1816. ictx->rx_endpoint_intf0->bInterval);
  1817. ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
  1818. if (ret) {
  1819. pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
  1820. goto urb_submit_failed;
  1821. }
  1822. ictx->idev = imon_init_idev(ictx);
  1823. if (!ictx->idev) {
  1824. dev_err(dev, "%s: input device setup failed\n", __func__);
  1825. goto idev_setup_failed;
  1826. }
  1827. ictx->rdev = imon_init_rdev(ictx);
  1828. if (!ictx->rdev) {
  1829. dev_err(dev, "%s: rc device setup failed\n", __func__);
  1830. goto rdev_setup_failed;
  1831. }
  1832. mutex_unlock(&ictx->lock);
  1833. return ictx;
  1834. rdev_setup_failed:
  1835. input_unregister_device(ictx->idev);
  1836. idev_setup_failed:
  1837. usb_kill_urb(ictx->rx_urb_intf0);
  1838. urb_submit_failed:
  1839. find_endpoint_failed:
  1840. mutex_unlock(&ictx->lock);
  1841. usb_free_urb(tx_urb);
  1842. tx_urb_alloc_failed:
  1843. usb_free_urb(rx_urb);
  1844. rx_urb_alloc_failed:
  1845. kfree(ictx);
  1846. exit:
  1847. dev_err(dev, "unable to initialize intf0, err %d\n", ret);
  1848. return NULL;
  1849. }
  1850. static struct imon_context *imon_init_intf1(struct usb_interface *intf,
  1851. struct imon_context *ictx)
  1852. {
  1853. struct urb *rx_urb;
  1854. struct usb_host_interface *iface_desc;
  1855. int ret = -ENOMEM;
  1856. rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1857. if (!rx_urb) {
  1858. pr_err("usb_alloc_urb failed for IR urb\n");
  1859. goto rx_urb_alloc_failed;
  1860. }
  1861. mutex_lock(&ictx->lock);
  1862. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1863. init_timer(&ictx->ttimer);
  1864. ictx->ttimer.data = (unsigned long)ictx;
  1865. ictx->ttimer.function = imon_touch_display_timeout;
  1866. }
  1867. ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
  1868. ictx->dev_present_intf1 = true;
  1869. ictx->rx_urb_intf1 = rx_urb;
  1870. ret = -ENODEV;
  1871. iface_desc = intf->cur_altsetting;
  1872. if (!imon_find_endpoints(ictx, iface_desc))
  1873. goto find_endpoint_failed;
  1874. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1875. ictx->touch = imon_init_touch(ictx);
  1876. if (!ictx->touch)
  1877. goto touch_setup_failed;
  1878. } else
  1879. ictx->touch = NULL;
  1880. usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
  1881. usb_rcvintpipe(ictx->usbdev_intf1,
  1882. ictx->rx_endpoint_intf1->bEndpointAddress),
  1883. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  1884. usb_rx_callback_intf1, ictx,
  1885. ictx->rx_endpoint_intf1->bInterval);
  1886. ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
  1887. if (ret) {
  1888. pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
  1889. goto urb_submit_failed;
  1890. }
  1891. mutex_unlock(&ictx->lock);
  1892. return ictx;
  1893. urb_submit_failed:
  1894. if (ictx->touch)
  1895. input_unregister_device(ictx->touch);
  1896. touch_setup_failed:
  1897. find_endpoint_failed:
  1898. mutex_unlock(&ictx->lock);
  1899. usb_free_urb(rx_urb);
  1900. rx_urb_alloc_failed:
  1901. dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
  1902. return NULL;
  1903. }
  1904. static void imon_init_display(struct imon_context *ictx,
  1905. struct usb_interface *intf)
  1906. {
  1907. int ret;
  1908. dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
  1909. /* set up sysfs entry for built-in clock */
  1910. ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
  1911. if (ret)
  1912. dev_err(ictx->dev, "Could not create display sysfs "
  1913. "entries(%d)", ret);
  1914. if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
  1915. ret = usb_register_dev(intf, &imon_lcd_class);
  1916. else
  1917. ret = usb_register_dev(intf, &imon_vfd_class);
  1918. if (ret)
  1919. /* Not a fatal error, so ignore */
  1920. dev_info(ictx->dev, "could not get a minor number for "
  1921. "display\n");
  1922. }
  1923. /**
  1924. * Callback function for USB core API: Probe
  1925. */
  1926. static int __devinit imon_probe(struct usb_interface *interface,
  1927. const struct usb_device_id *id)
  1928. {
  1929. struct usb_device *usbdev = NULL;
  1930. struct usb_host_interface *iface_desc = NULL;
  1931. struct usb_interface *first_if;
  1932. struct device *dev = &interface->dev;
  1933. int ifnum, sysfs_err;
  1934. int ret = 0;
  1935. struct imon_context *ictx = NULL;
  1936. struct imon_context *first_if_ctx = NULL;
  1937. u16 vendor, product;
  1938. usbdev = usb_get_dev(interface_to_usbdev(interface));
  1939. iface_desc = interface->cur_altsetting;
  1940. ifnum = iface_desc->desc.bInterfaceNumber;
  1941. vendor = le16_to_cpu(usbdev->descriptor.idVendor);
  1942. product = le16_to_cpu(usbdev->descriptor.idProduct);
  1943. dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
  1944. __func__, vendor, product, ifnum);
  1945. /* prevent races probing devices w/multiple interfaces */
  1946. mutex_lock(&driver_lock);
  1947. first_if = usb_ifnum_to_if(usbdev, 0);
  1948. first_if_ctx = usb_get_intfdata(first_if);
  1949. if (ifnum == 0) {
  1950. ictx = imon_init_intf0(interface);
  1951. if (!ictx) {
  1952. pr_err("failed to initialize context!\n");
  1953. ret = -ENODEV;
  1954. goto fail;
  1955. }
  1956. } else {
  1957. /* this is the secondary interface on the device */
  1958. ictx = imon_init_intf1(interface, first_if_ctx);
  1959. if (!ictx) {
  1960. pr_err("failed to attach to context!\n");
  1961. ret = -ENODEV;
  1962. goto fail;
  1963. }
  1964. }
  1965. usb_set_intfdata(interface, ictx);
  1966. if (ifnum == 0) {
  1967. mutex_lock(&ictx->lock);
  1968. if (product == 0xffdc && ictx->rf_device) {
  1969. sysfs_err = sysfs_create_group(&interface->dev.kobj,
  1970. &imon_rf_attr_group);
  1971. if (sysfs_err)
  1972. pr_err("Could not create RF sysfs entries(%d)\n",
  1973. sysfs_err);
  1974. }
  1975. if (ictx->display_supported)
  1976. imon_init_display(ictx, interface);
  1977. mutex_unlock(&ictx->lock);
  1978. }
  1979. dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
  1980. "usb<%d:%d> initialized\n", vendor, product, ifnum,
  1981. usbdev->bus->busnum, usbdev->devnum);
  1982. mutex_unlock(&driver_lock);
  1983. return 0;
  1984. fail:
  1985. mutex_unlock(&driver_lock);
  1986. dev_err(dev, "unable to register, err %d\n", ret);
  1987. return ret;
  1988. }
  1989. /**
  1990. * Callback function for USB core API: disconnect
  1991. */
  1992. static void __devexit imon_disconnect(struct usb_interface *interface)
  1993. {
  1994. struct imon_context *ictx;
  1995. struct device *dev;
  1996. int ifnum;
  1997. /* prevent races with multi-interface device probing and display_open */
  1998. mutex_lock(&driver_lock);
  1999. ictx = usb_get_intfdata(interface);
  2000. dev = ictx->dev;
  2001. ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
  2002. /*
  2003. * sysfs_remove_group is safe to call even if sysfs_create_group
  2004. * hasn't been called
  2005. */
  2006. sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
  2007. sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
  2008. usb_set_intfdata(interface, NULL);
  2009. /* Abort ongoing write */
  2010. if (ictx->tx.busy) {
  2011. usb_kill_urb(ictx->tx_urb);
  2012. complete_all(&ictx->tx.finished);
  2013. }
  2014. if (ifnum == 0) {
  2015. ictx->dev_present_intf0 = false;
  2016. usb_kill_urb(ictx->rx_urb_intf0);
  2017. input_unregister_device(ictx->idev);
  2018. rc_unregister_device(ictx->rdev);
  2019. if (ictx->display_supported) {
  2020. if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
  2021. usb_deregister_dev(interface, &imon_lcd_class);
  2022. else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
  2023. usb_deregister_dev(interface, &imon_vfd_class);
  2024. }
  2025. } else {
  2026. ictx->dev_present_intf1 = false;
  2027. usb_kill_urb(ictx->rx_urb_intf1);
  2028. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  2029. input_unregister_device(ictx->touch);
  2030. del_timer_sync(&ictx->ttimer);
  2031. }
  2032. }
  2033. if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
  2034. free_imon_context(ictx);
  2035. mutex_unlock(&driver_lock);
  2036. dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
  2037. __func__, ifnum);
  2038. }
  2039. static int imon_suspend(struct usb_interface *intf, pm_message_t message)
  2040. {
  2041. struct imon_context *ictx = usb_get_intfdata(intf);
  2042. int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  2043. if (ifnum == 0)
  2044. usb_kill_urb(ictx->rx_urb_intf0);
  2045. else
  2046. usb_kill_urb(ictx->rx_urb_intf1);
  2047. return 0;
  2048. }
  2049. static int imon_resume(struct usb_interface *intf)
  2050. {
  2051. int rc = 0;
  2052. struct imon_context *ictx = usb_get_intfdata(intf);
  2053. int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  2054. if (ifnum == 0) {
  2055. usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
  2056. usb_rcvintpipe(ictx->usbdev_intf0,
  2057. ictx->rx_endpoint_intf0->bEndpointAddress),
  2058. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  2059. usb_rx_callback_intf0, ictx,
  2060. ictx->rx_endpoint_intf0->bInterval);
  2061. rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
  2062. } else {
  2063. usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
  2064. usb_rcvintpipe(ictx->usbdev_intf1,
  2065. ictx->rx_endpoint_intf1->bEndpointAddress),
  2066. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  2067. usb_rx_callback_intf1, ictx,
  2068. ictx->rx_endpoint_intf1->bInterval);
  2069. rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
  2070. }
  2071. return rc;
  2072. }
  2073. static int __init imon_init(void)
  2074. {
  2075. int rc;
  2076. rc = usb_register(&imon_driver);
  2077. if (rc) {
  2078. pr_err("usb register failed(%d)\n", rc);
  2079. rc = -ENODEV;
  2080. }
  2081. return rc;
  2082. }
  2083. static void __exit imon_exit(void)
  2084. {
  2085. usb_deregister(&imon_driver);
  2086. }
  2087. module_init(imon_init);
  2088. module_exit(imon_exit);