imon.c 62 KB

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