imon.c 64 KB

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