imon.c 63 KB

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