imon.c 65 KB

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