imon.c 70 KB

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