imon.c 69 KB

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