imon.c 69 KB

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