imon.c 63 KB

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