imon.c 64 KB

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