imon.c 62 KB

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