imon.c 70 KB

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