imon.c 69 KB

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