imon.c 71 KB

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