imon.c 65 KB

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