imon.c 69 KB

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