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;
  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. ictx->rc_type == RC_BIT_OTHER)
  1448. rc_keydown(ictx->rdev,
  1449. ictx->rc_type == RC_BIT_RC6_MCE ? RC_TYPE_RC6_MCE : RC_TYPE_OTHER,
  1450. ictx->rc_scancode, ictx->rc_toggle);
  1451. spin_lock_irqsave(&ictx->kc_lock, flags);
  1452. ictx->last_keycode = ictx->kc;
  1453. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1454. }
  1455. return;
  1456. }
  1457. /* Only panel type events left to process now */
  1458. spin_lock_irqsave(&ictx->kc_lock, flags);
  1459. do_gettimeofday(&t);
  1460. /* KEY_MUTE repeats from knob need to be suppressed */
  1461. if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
  1462. msec = tv2int(&t, &prev_time);
  1463. if (msec < ictx->idev->rep[REP_DELAY]) {
  1464. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1465. return;
  1466. }
  1467. }
  1468. prev_time = t;
  1469. kc = ictx->kc;
  1470. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1471. input_report_key(ictx->idev, kc, press_type);
  1472. input_sync(ictx->idev);
  1473. /* panel keys don't generate a release */
  1474. input_report_key(ictx->idev, kc, 0);
  1475. input_sync(ictx->idev);
  1476. spin_lock_irqsave(&ictx->kc_lock, flags);
  1477. ictx->last_keycode = kc;
  1478. spin_unlock_irqrestore(&ictx->kc_lock, flags);
  1479. return;
  1480. not_input_data:
  1481. if (len != 8) {
  1482. dev_warn(dev, "imon %s: invalid incoming packet "
  1483. "size (len = %d, intf%d)\n", __func__, len, intf);
  1484. return;
  1485. }
  1486. /* iMON 2.4G associate frame */
  1487. if (buf[0] == 0x00 &&
  1488. buf[2] == 0xFF && /* REFID */
  1489. buf[3] == 0xFF &&
  1490. buf[4] == 0xFF &&
  1491. buf[5] == 0xFF && /* iMON 2.4G */
  1492. ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
  1493. (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
  1494. dev_warn(dev, "%s: remote associated refid=%02X\n",
  1495. __func__, buf[1]);
  1496. ictx->rf_isassociating = false;
  1497. }
  1498. }
  1499. /**
  1500. * Callback function for USB core API: receive data
  1501. */
  1502. static void usb_rx_callback_intf0(struct urb *urb)
  1503. {
  1504. struct imon_context *ictx;
  1505. int intfnum = 0;
  1506. if (!urb)
  1507. return;
  1508. ictx = (struct imon_context *)urb->context;
  1509. if (!ictx)
  1510. return;
  1511. /*
  1512. * if we get a callback before we're done configuring the hardware, we
  1513. * can't yet process the data, as there's nowhere to send it, but we
  1514. * still need to submit a new rx URB to avoid wedging the hardware
  1515. */
  1516. if (!ictx->dev_present_intf0)
  1517. goto out;
  1518. switch (urb->status) {
  1519. case -ENOENT: /* usbcore unlink successful! */
  1520. return;
  1521. case -ESHUTDOWN: /* transport endpoint was shut down */
  1522. break;
  1523. case 0:
  1524. imon_incoming_packet(ictx, urb, intfnum);
  1525. break;
  1526. default:
  1527. dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
  1528. __func__, urb->status);
  1529. break;
  1530. }
  1531. out:
  1532. usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
  1533. }
  1534. static void usb_rx_callback_intf1(struct urb *urb)
  1535. {
  1536. struct imon_context *ictx;
  1537. int intfnum = 1;
  1538. if (!urb)
  1539. return;
  1540. ictx = (struct imon_context *)urb->context;
  1541. if (!ictx)
  1542. return;
  1543. /*
  1544. * if we get a callback before we're done configuring the hardware, we
  1545. * can't yet process the data, as there's nowhere to send it, but we
  1546. * still need to submit a new rx URB to avoid wedging the hardware
  1547. */
  1548. if (!ictx->dev_present_intf1)
  1549. goto out;
  1550. switch (urb->status) {
  1551. case -ENOENT: /* usbcore unlink successful! */
  1552. return;
  1553. case -ESHUTDOWN: /* transport endpoint was shut down */
  1554. break;
  1555. case 0:
  1556. imon_incoming_packet(ictx, urb, intfnum);
  1557. break;
  1558. default:
  1559. dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
  1560. __func__, urb->status);
  1561. break;
  1562. }
  1563. out:
  1564. usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
  1565. }
  1566. /*
  1567. * The 0x15c2:0xffdc device ID was used for umpteen different imon
  1568. * devices, and all of them constantly spew interrupts, even when there
  1569. * is no actual data to report. However, byte 6 of this buffer looks like
  1570. * its unique across device variants, so we're trying to key off that to
  1571. * figure out which display type (if any) and what IR protocol the device
  1572. * actually supports. These devices have their IR protocol hard-coded into
  1573. * their firmware, they can't be changed on the fly like the newer hardware.
  1574. */
  1575. static void imon_get_ffdc_type(struct imon_context *ictx)
  1576. {
  1577. u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
  1578. u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
  1579. u64 allowed_protos = RC_BIT_OTHER;
  1580. switch (ffdc_cfg_byte) {
  1581. /* iMON Knob, no display, iMON IR + vol knob */
  1582. case 0x21:
  1583. dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
  1584. ictx->display_supported = false;
  1585. break;
  1586. /* iMON 2.4G LT (usb stick), no display, iMON RF */
  1587. case 0x4e:
  1588. dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
  1589. ictx->display_supported = false;
  1590. ictx->rf_device = true;
  1591. break;
  1592. /* iMON VFD, no IR (does have vol knob tho) */
  1593. case 0x35:
  1594. dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
  1595. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1596. break;
  1597. /* iMON VFD, iMON IR */
  1598. case 0x24:
  1599. case 0x85:
  1600. dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
  1601. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1602. break;
  1603. /* iMON VFD, MCE IR */
  1604. case 0x46:
  1605. case 0x7e:
  1606. case 0x9e:
  1607. dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
  1608. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1609. allowed_protos = RC_BIT_RC6_MCE;
  1610. break;
  1611. /* iMON LCD, MCE IR */
  1612. case 0x9f:
  1613. dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
  1614. detected_display_type = IMON_DISPLAY_TYPE_LCD;
  1615. allowed_protos = RC_BIT_RC6_MCE;
  1616. break;
  1617. default:
  1618. dev_info(ictx->dev, "Unknown 0xffdc device, "
  1619. "defaulting to VFD and iMON IR");
  1620. detected_display_type = IMON_DISPLAY_TYPE_VFD;
  1621. /* We don't know which one it is, allow user to set the
  1622. * RC6 one from userspace if OTHER wasn't correct. */
  1623. allowed_protos |= RC_BIT_RC6_MCE;
  1624. break;
  1625. }
  1626. printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
  1627. ictx->display_type = detected_display_type;
  1628. ictx->rc_type = allowed_protos;
  1629. }
  1630. static void imon_set_display_type(struct imon_context *ictx)
  1631. {
  1632. u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
  1633. /*
  1634. * Try to auto-detect the type of display if the user hasn't set
  1635. * it by hand via the display_type modparam. Default is VFD.
  1636. */
  1637. if (display_type == IMON_DISPLAY_TYPE_AUTO) {
  1638. switch (ictx->product) {
  1639. case 0xffdc:
  1640. /* set in imon_get_ffdc_type() */
  1641. configured_display_type = ictx->display_type;
  1642. break;
  1643. case 0x0034:
  1644. case 0x0035:
  1645. configured_display_type = IMON_DISPLAY_TYPE_VGA;
  1646. break;
  1647. case 0x0038:
  1648. case 0x0039:
  1649. case 0x0045:
  1650. configured_display_type = IMON_DISPLAY_TYPE_LCD;
  1651. break;
  1652. case 0x003c:
  1653. case 0x0041:
  1654. case 0x0042:
  1655. case 0x0043:
  1656. configured_display_type = IMON_DISPLAY_TYPE_NONE;
  1657. ictx->display_supported = false;
  1658. break;
  1659. case 0x0036:
  1660. case 0x0044:
  1661. default:
  1662. configured_display_type = IMON_DISPLAY_TYPE_VFD;
  1663. break;
  1664. }
  1665. } else {
  1666. configured_display_type = display_type;
  1667. if (display_type == IMON_DISPLAY_TYPE_NONE)
  1668. ictx->display_supported = false;
  1669. else
  1670. ictx->display_supported = true;
  1671. dev_info(ictx->dev, "%s: overriding display type to %d via "
  1672. "modparam\n", __func__, display_type);
  1673. }
  1674. ictx->display_type = configured_display_type;
  1675. }
  1676. static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
  1677. {
  1678. struct rc_dev *rdev;
  1679. int ret;
  1680. const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
  1681. 0x00, 0x00, 0x00, 0x88 };
  1682. rdev = rc_allocate_device();
  1683. if (!rdev) {
  1684. dev_err(ictx->dev, "remote control dev allocation failed\n");
  1685. goto out;
  1686. }
  1687. snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
  1688. "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
  1689. usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
  1690. sizeof(ictx->phys_rdev));
  1691. strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
  1692. rdev->input_name = ictx->name_rdev;
  1693. rdev->input_phys = ictx->phys_rdev;
  1694. usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
  1695. rdev->dev.parent = ictx->dev;
  1696. rdev->priv = ictx;
  1697. rdev->driver_type = RC_DRIVER_SCANCODE;
  1698. rdev->allowed_protocols = RC_BIT_OTHER | RC_BIT_RC6_MCE; /* iMON PAD or MCE */
  1699. rdev->change_protocol = imon_ir_change_protocol;
  1700. rdev->driver_name = MOD_NAME;
  1701. /* Enable front-panel buttons and/or knobs */
  1702. memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
  1703. ret = send_packet(ictx);
  1704. /* Not fatal, but warn about it */
  1705. if (ret)
  1706. dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
  1707. if (ictx->product == 0xffdc) {
  1708. imon_get_ffdc_type(ictx);
  1709. rdev->allowed_protocols = ictx->rc_type;
  1710. }
  1711. imon_set_display_type(ictx);
  1712. if (ictx->rc_type == RC_BIT_RC6_MCE)
  1713. rdev->map_name = RC_MAP_IMON_MCE;
  1714. else
  1715. rdev->map_name = RC_MAP_IMON_PAD;
  1716. ret = rc_register_device(rdev);
  1717. if (ret < 0) {
  1718. dev_err(ictx->dev, "remote input dev register failed\n");
  1719. goto out;
  1720. }
  1721. return rdev;
  1722. out:
  1723. rc_free_device(rdev);
  1724. return NULL;
  1725. }
  1726. static struct input_dev *imon_init_idev(struct imon_context *ictx)
  1727. {
  1728. struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
  1729. struct input_dev *idev;
  1730. int ret, i;
  1731. idev = input_allocate_device();
  1732. if (!idev)
  1733. goto out;
  1734. snprintf(ictx->name_idev, sizeof(ictx->name_idev),
  1735. "iMON Panel, Knob and Mouse(%04x:%04x)",
  1736. ictx->vendor, ictx->product);
  1737. idev->name = ictx->name_idev;
  1738. usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
  1739. sizeof(ictx->phys_idev));
  1740. strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
  1741. idev->phys = ictx->phys_idev;
  1742. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
  1743. idev->keybit[BIT_WORD(BTN_MOUSE)] =
  1744. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1745. idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
  1746. BIT_MASK(REL_WHEEL);
  1747. /* panel and/or knob code support */
  1748. for (i = 0; key_table[i].hw_code != 0; i++) {
  1749. u32 kc = key_table[i].keycode;
  1750. __set_bit(kc, idev->keybit);
  1751. }
  1752. usb_to_input_id(ictx->usbdev_intf0, &idev->id);
  1753. idev->dev.parent = ictx->dev;
  1754. input_set_drvdata(idev, ictx);
  1755. ret = input_register_device(idev);
  1756. if (ret < 0) {
  1757. dev_err(ictx->dev, "input dev register failed\n");
  1758. goto out;
  1759. }
  1760. return idev;
  1761. out:
  1762. input_free_device(idev);
  1763. return NULL;
  1764. }
  1765. static struct input_dev *imon_init_touch(struct imon_context *ictx)
  1766. {
  1767. struct input_dev *touch;
  1768. int ret;
  1769. touch = input_allocate_device();
  1770. if (!touch)
  1771. goto touch_alloc_failed;
  1772. snprintf(ictx->name_touch, sizeof(ictx->name_touch),
  1773. "iMON USB Touchscreen (%04x:%04x)",
  1774. ictx->vendor, ictx->product);
  1775. touch->name = ictx->name_touch;
  1776. usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
  1777. sizeof(ictx->phys_touch));
  1778. strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
  1779. touch->phys = ictx->phys_touch;
  1780. touch->evbit[0] =
  1781. BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1782. touch->keybit[BIT_WORD(BTN_TOUCH)] =
  1783. BIT_MASK(BTN_TOUCH);
  1784. input_set_abs_params(touch, ABS_X,
  1785. 0x00, 0xfff, 0, 0);
  1786. input_set_abs_params(touch, ABS_Y,
  1787. 0x00, 0xfff, 0, 0);
  1788. input_set_drvdata(touch, ictx);
  1789. usb_to_input_id(ictx->usbdev_intf1, &touch->id);
  1790. touch->dev.parent = ictx->dev;
  1791. ret = input_register_device(touch);
  1792. if (ret < 0) {
  1793. dev_info(ictx->dev, "touchscreen input dev register failed\n");
  1794. goto touch_register_failed;
  1795. }
  1796. return touch;
  1797. touch_register_failed:
  1798. input_free_device(touch);
  1799. touch_alloc_failed:
  1800. return NULL;
  1801. }
  1802. static bool imon_find_endpoints(struct imon_context *ictx,
  1803. struct usb_host_interface *iface_desc)
  1804. {
  1805. struct usb_endpoint_descriptor *ep;
  1806. struct usb_endpoint_descriptor *rx_endpoint = NULL;
  1807. struct usb_endpoint_descriptor *tx_endpoint = NULL;
  1808. int ifnum = iface_desc->desc.bInterfaceNumber;
  1809. int num_endpts = iface_desc->desc.bNumEndpoints;
  1810. int i, ep_dir, ep_type;
  1811. bool ir_ep_found = false;
  1812. bool display_ep_found = false;
  1813. bool tx_control = false;
  1814. /*
  1815. * Scan the endpoint list and set:
  1816. * first input endpoint = IR endpoint
  1817. * first output endpoint = display endpoint
  1818. */
  1819. for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
  1820. ep = &iface_desc->endpoint[i].desc;
  1821. ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
  1822. ep_type = usb_endpoint_type(ep);
  1823. if (!ir_ep_found && ep_dir == USB_DIR_IN &&
  1824. ep_type == USB_ENDPOINT_XFER_INT) {
  1825. rx_endpoint = ep;
  1826. ir_ep_found = true;
  1827. dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
  1828. } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
  1829. ep_type == USB_ENDPOINT_XFER_INT) {
  1830. tx_endpoint = ep;
  1831. display_ep_found = true;
  1832. dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
  1833. }
  1834. }
  1835. if (ifnum == 0) {
  1836. ictx->rx_endpoint_intf0 = rx_endpoint;
  1837. /*
  1838. * tx is used to send characters to lcd/vfd, associate RF
  1839. * remotes, set IR protocol, and maybe more...
  1840. */
  1841. ictx->tx_endpoint = tx_endpoint;
  1842. } else {
  1843. ictx->rx_endpoint_intf1 = rx_endpoint;
  1844. }
  1845. /*
  1846. * If we didn't find a display endpoint, this is probably one of the
  1847. * newer iMON devices that use control urb instead of interrupt
  1848. */
  1849. if (!display_ep_found) {
  1850. tx_control = true;
  1851. display_ep_found = true;
  1852. dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
  1853. "interface OUT endpoint\n", __func__);
  1854. }
  1855. /*
  1856. * Some iMON receivers have no display. Unfortunately, it seems
  1857. * that SoundGraph recycles device IDs between devices both with
  1858. * and without... :\
  1859. */
  1860. if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
  1861. display_ep_found = false;
  1862. dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
  1863. }
  1864. /*
  1865. * iMON Touch devices have a VGA touchscreen, but no "display", as
  1866. * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
  1867. */
  1868. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1869. display_ep_found = false;
  1870. dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
  1871. }
  1872. /* Input endpoint is mandatory */
  1873. if (!ir_ep_found)
  1874. pr_err("no valid input (IR) endpoint found\n");
  1875. ictx->tx_control = tx_control;
  1876. if (display_ep_found)
  1877. ictx->display_supported = true;
  1878. return ir_ep_found;
  1879. }
  1880. static struct imon_context *imon_init_intf0(struct usb_interface *intf,
  1881. const struct usb_device_id *id)
  1882. {
  1883. struct imon_context *ictx;
  1884. struct urb *rx_urb;
  1885. struct urb *tx_urb;
  1886. struct device *dev = &intf->dev;
  1887. struct usb_host_interface *iface_desc;
  1888. int ret = -ENOMEM;
  1889. ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
  1890. if (!ictx) {
  1891. dev_err(dev, "%s: kzalloc failed for context", __func__);
  1892. goto exit;
  1893. }
  1894. rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1895. if (!rx_urb)
  1896. goto rx_urb_alloc_failed;
  1897. tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1898. if (!tx_urb)
  1899. goto tx_urb_alloc_failed;
  1900. mutex_init(&ictx->lock);
  1901. spin_lock_init(&ictx->kc_lock);
  1902. mutex_lock(&ictx->lock);
  1903. ictx->dev = dev;
  1904. ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
  1905. ictx->rx_urb_intf0 = rx_urb;
  1906. ictx->tx_urb = tx_urb;
  1907. ictx->rf_device = false;
  1908. ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
  1909. ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
  1910. /* save drive info for later accessing the panel/knob key table */
  1911. ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
  1912. /* default send_packet delay is 5ms but some devices need more */
  1913. ictx->send_packet_delay = ictx->dev_descr->flags &
  1914. IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
  1915. ret = -ENODEV;
  1916. iface_desc = intf->cur_altsetting;
  1917. if (!imon_find_endpoints(ictx, iface_desc)) {
  1918. goto find_endpoint_failed;
  1919. }
  1920. usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
  1921. usb_rcvintpipe(ictx->usbdev_intf0,
  1922. ictx->rx_endpoint_intf0->bEndpointAddress),
  1923. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  1924. usb_rx_callback_intf0, ictx,
  1925. ictx->rx_endpoint_intf0->bInterval);
  1926. ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
  1927. if (ret) {
  1928. pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
  1929. goto urb_submit_failed;
  1930. }
  1931. ictx->idev = imon_init_idev(ictx);
  1932. if (!ictx->idev) {
  1933. dev_err(dev, "%s: input device setup failed\n", __func__);
  1934. goto idev_setup_failed;
  1935. }
  1936. ictx->rdev = imon_init_rdev(ictx);
  1937. if (!ictx->rdev) {
  1938. dev_err(dev, "%s: rc device setup failed\n", __func__);
  1939. goto rdev_setup_failed;
  1940. }
  1941. ictx->dev_present_intf0 = true;
  1942. mutex_unlock(&ictx->lock);
  1943. return ictx;
  1944. rdev_setup_failed:
  1945. input_unregister_device(ictx->idev);
  1946. idev_setup_failed:
  1947. usb_kill_urb(ictx->rx_urb_intf0);
  1948. urb_submit_failed:
  1949. find_endpoint_failed:
  1950. usb_put_dev(ictx->usbdev_intf0);
  1951. mutex_unlock(&ictx->lock);
  1952. usb_free_urb(tx_urb);
  1953. tx_urb_alloc_failed:
  1954. usb_free_urb(rx_urb);
  1955. rx_urb_alloc_failed:
  1956. kfree(ictx);
  1957. exit:
  1958. dev_err(dev, "unable to initialize intf0, err %d\n", ret);
  1959. return NULL;
  1960. }
  1961. static struct imon_context *imon_init_intf1(struct usb_interface *intf,
  1962. struct imon_context *ictx)
  1963. {
  1964. struct urb *rx_urb;
  1965. struct usb_host_interface *iface_desc;
  1966. int ret = -ENOMEM;
  1967. rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1968. if (!rx_urb)
  1969. goto rx_urb_alloc_failed;
  1970. mutex_lock(&ictx->lock);
  1971. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1972. init_timer(&ictx->ttimer);
  1973. ictx->ttimer.data = (unsigned long)ictx;
  1974. ictx->ttimer.function = imon_touch_display_timeout;
  1975. }
  1976. ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
  1977. ictx->rx_urb_intf1 = rx_urb;
  1978. ret = -ENODEV;
  1979. iface_desc = intf->cur_altsetting;
  1980. if (!imon_find_endpoints(ictx, iface_desc))
  1981. goto find_endpoint_failed;
  1982. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  1983. ictx->touch = imon_init_touch(ictx);
  1984. if (!ictx->touch)
  1985. goto touch_setup_failed;
  1986. } else
  1987. ictx->touch = NULL;
  1988. usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
  1989. usb_rcvintpipe(ictx->usbdev_intf1,
  1990. ictx->rx_endpoint_intf1->bEndpointAddress),
  1991. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  1992. usb_rx_callback_intf1, ictx,
  1993. ictx->rx_endpoint_intf1->bInterval);
  1994. ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
  1995. if (ret) {
  1996. pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
  1997. goto urb_submit_failed;
  1998. }
  1999. ictx->dev_present_intf1 = true;
  2000. mutex_unlock(&ictx->lock);
  2001. return ictx;
  2002. urb_submit_failed:
  2003. if (ictx->touch)
  2004. input_unregister_device(ictx->touch);
  2005. touch_setup_failed:
  2006. find_endpoint_failed:
  2007. usb_put_dev(ictx->usbdev_intf1);
  2008. mutex_unlock(&ictx->lock);
  2009. usb_free_urb(rx_urb);
  2010. rx_urb_alloc_failed:
  2011. dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
  2012. return NULL;
  2013. }
  2014. static void imon_init_display(struct imon_context *ictx,
  2015. struct usb_interface *intf)
  2016. {
  2017. int ret;
  2018. dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
  2019. /* set up sysfs entry for built-in clock */
  2020. ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
  2021. if (ret)
  2022. dev_err(ictx->dev, "Could not create display sysfs "
  2023. "entries(%d)", ret);
  2024. if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
  2025. ret = usb_register_dev(intf, &imon_lcd_class);
  2026. else
  2027. ret = usb_register_dev(intf, &imon_vfd_class);
  2028. if (ret)
  2029. /* Not a fatal error, so ignore */
  2030. dev_info(ictx->dev, "could not get a minor number for "
  2031. "display\n");
  2032. }
  2033. /**
  2034. * Callback function for USB core API: Probe
  2035. */
  2036. static int imon_probe(struct usb_interface *interface,
  2037. const struct usb_device_id *id)
  2038. {
  2039. struct usb_device *usbdev = NULL;
  2040. struct usb_host_interface *iface_desc = NULL;
  2041. struct usb_interface *first_if;
  2042. struct device *dev = &interface->dev;
  2043. int ifnum, sysfs_err;
  2044. int ret = 0;
  2045. struct imon_context *ictx = NULL;
  2046. struct imon_context *first_if_ctx = NULL;
  2047. u16 vendor, product;
  2048. usbdev = usb_get_dev(interface_to_usbdev(interface));
  2049. iface_desc = interface->cur_altsetting;
  2050. ifnum = iface_desc->desc.bInterfaceNumber;
  2051. vendor = le16_to_cpu(usbdev->descriptor.idVendor);
  2052. product = le16_to_cpu(usbdev->descriptor.idProduct);
  2053. dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
  2054. __func__, vendor, product, ifnum);
  2055. /* prevent races probing devices w/multiple interfaces */
  2056. mutex_lock(&driver_lock);
  2057. first_if = usb_ifnum_to_if(usbdev, 0);
  2058. if (!first_if) {
  2059. ret = -ENODEV;
  2060. goto fail;
  2061. }
  2062. first_if_ctx = usb_get_intfdata(first_if);
  2063. if (ifnum == 0) {
  2064. ictx = imon_init_intf0(interface, id);
  2065. if (!ictx) {
  2066. pr_err("failed to initialize context!\n");
  2067. ret = -ENODEV;
  2068. goto fail;
  2069. }
  2070. } else {
  2071. /* this is the secondary interface on the device */
  2072. /* fail early if first intf failed to register */
  2073. if (!first_if_ctx) {
  2074. ret = -ENODEV;
  2075. goto fail;
  2076. }
  2077. ictx = imon_init_intf1(interface, first_if_ctx);
  2078. if (!ictx) {
  2079. pr_err("failed to attach to context!\n");
  2080. ret = -ENODEV;
  2081. goto fail;
  2082. }
  2083. }
  2084. usb_set_intfdata(interface, ictx);
  2085. if (ifnum == 0) {
  2086. mutex_lock(&ictx->lock);
  2087. if (product == 0xffdc && ictx->rf_device) {
  2088. sysfs_err = sysfs_create_group(&interface->dev.kobj,
  2089. &imon_rf_attr_group);
  2090. if (sysfs_err)
  2091. pr_err("Could not create RF sysfs entries(%d)\n",
  2092. sysfs_err);
  2093. }
  2094. if (ictx->display_supported)
  2095. imon_init_display(ictx, interface);
  2096. mutex_unlock(&ictx->lock);
  2097. }
  2098. dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
  2099. "usb<%d:%d> initialized\n", vendor, product, ifnum,
  2100. usbdev->bus->busnum, usbdev->devnum);
  2101. mutex_unlock(&driver_lock);
  2102. usb_put_dev(usbdev);
  2103. return 0;
  2104. fail:
  2105. mutex_unlock(&driver_lock);
  2106. usb_put_dev(usbdev);
  2107. dev_err(dev, "unable to register, err %d\n", ret);
  2108. return ret;
  2109. }
  2110. /**
  2111. * Callback function for USB core API: disconnect
  2112. */
  2113. static void imon_disconnect(struct usb_interface *interface)
  2114. {
  2115. struct imon_context *ictx;
  2116. struct device *dev;
  2117. int ifnum;
  2118. /* prevent races with multi-interface device probing and display_open */
  2119. mutex_lock(&driver_lock);
  2120. ictx = usb_get_intfdata(interface);
  2121. dev = ictx->dev;
  2122. ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
  2123. /*
  2124. * sysfs_remove_group is safe to call even if sysfs_create_group
  2125. * hasn't been called
  2126. */
  2127. sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
  2128. sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
  2129. usb_set_intfdata(interface, NULL);
  2130. /* Abort ongoing write */
  2131. if (ictx->tx.busy) {
  2132. usb_kill_urb(ictx->tx_urb);
  2133. complete_all(&ictx->tx.finished);
  2134. }
  2135. if (ifnum == 0) {
  2136. ictx->dev_present_intf0 = false;
  2137. usb_kill_urb(ictx->rx_urb_intf0);
  2138. usb_put_dev(ictx->usbdev_intf0);
  2139. input_unregister_device(ictx->idev);
  2140. rc_unregister_device(ictx->rdev);
  2141. if (ictx->display_supported) {
  2142. if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
  2143. usb_deregister_dev(interface, &imon_lcd_class);
  2144. else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
  2145. usb_deregister_dev(interface, &imon_vfd_class);
  2146. }
  2147. } else {
  2148. ictx->dev_present_intf1 = false;
  2149. usb_kill_urb(ictx->rx_urb_intf1);
  2150. usb_put_dev(ictx->usbdev_intf1);
  2151. if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
  2152. input_unregister_device(ictx->touch);
  2153. del_timer_sync(&ictx->ttimer);
  2154. }
  2155. }
  2156. if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
  2157. free_imon_context(ictx);
  2158. mutex_unlock(&driver_lock);
  2159. dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
  2160. __func__, ifnum);
  2161. }
  2162. static int imon_suspend(struct usb_interface *intf, pm_message_t message)
  2163. {
  2164. struct imon_context *ictx = usb_get_intfdata(intf);
  2165. int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  2166. if (ifnum == 0)
  2167. usb_kill_urb(ictx->rx_urb_intf0);
  2168. else
  2169. usb_kill_urb(ictx->rx_urb_intf1);
  2170. return 0;
  2171. }
  2172. static int imon_resume(struct usb_interface *intf)
  2173. {
  2174. int rc = 0;
  2175. struct imon_context *ictx = usb_get_intfdata(intf);
  2176. int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  2177. if (ifnum == 0) {
  2178. usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
  2179. usb_rcvintpipe(ictx->usbdev_intf0,
  2180. ictx->rx_endpoint_intf0->bEndpointAddress),
  2181. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  2182. usb_rx_callback_intf0, ictx,
  2183. ictx->rx_endpoint_intf0->bInterval);
  2184. rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
  2185. } else {
  2186. usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
  2187. usb_rcvintpipe(ictx->usbdev_intf1,
  2188. ictx->rx_endpoint_intf1->bEndpointAddress),
  2189. ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
  2190. usb_rx_callback_intf1, ictx,
  2191. ictx->rx_endpoint_intf1->bInterval);
  2192. rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
  2193. }
  2194. return rc;
  2195. }
  2196. module_usb_driver(imon_driver);