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