imon.c 66 KB

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