imon.c 64 KB

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