imon.c 65 KB

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