imon.c 62 KB

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