imon.c 72 KB

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