ims-pcu.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /*
  2. * Driver for IMS Passenger Control Unit Devices
  3. *
  4. * Copyright (C) 2013 The IMS Company
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/completion.h>
  11. #include <linux/device.h>
  12. #include <linux/firmware.h>
  13. #include <linux/ihex.h>
  14. #include <linux/input.h>
  15. #include <linux/kernel.h>
  16. #include <linux/leds.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <linux/usb/input.h>
  21. #include <linux/usb/cdc.h>
  22. #include <asm/unaligned.h>
  23. #define IMS_PCU_KEYMAP_LEN 32
  24. struct ims_pcu_buttons {
  25. struct input_dev *input;
  26. char name[32];
  27. char phys[32];
  28. unsigned short keymap[IMS_PCU_KEYMAP_LEN];
  29. };
  30. struct ims_pcu_gamepad {
  31. struct input_dev *input;
  32. char name[32];
  33. char phys[32];
  34. };
  35. struct ims_pcu_backlight {
  36. struct led_classdev cdev;
  37. struct work_struct work;
  38. enum led_brightness desired_brightness;
  39. char name[32];
  40. };
  41. #define IMS_PCU_PART_NUMBER_LEN 15
  42. #define IMS_PCU_SERIAL_NUMBER_LEN 8
  43. #define IMS_PCU_DOM_LEN 8
  44. #define IMS_PCU_FW_VERSION_LEN (9 + 1)
  45. #define IMS_PCU_BL_VERSION_LEN (9 + 1)
  46. #define IMS_PCU_BL_RESET_REASON_LEN (2 + 1)
  47. #define IMS_PCU_PCU_B_DEVICE_ID 5
  48. #define IMS_PCU_BUF_SIZE 128
  49. struct ims_pcu {
  50. struct usb_device *udev;
  51. struct device *dev; /* control interface's device, used for logging */
  52. unsigned int device_no;
  53. bool bootloader_mode;
  54. char part_number[IMS_PCU_PART_NUMBER_LEN];
  55. char serial_number[IMS_PCU_SERIAL_NUMBER_LEN];
  56. char date_of_manufacturing[IMS_PCU_DOM_LEN];
  57. char fw_version[IMS_PCU_FW_VERSION_LEN];
  58. char bl_version[IMS_PCU_BL_VERSION_LEN];
  59. char reset_reason[IMS_PCU_BL_RESET_REASON_LEN];
  60. int update_firmware_status;
  61. u8 device_id;
  62. u8 ofn_reg_addr;
  63. struct usb_interface *ctrl_intf;
  64. struct usb_endpoint_descriptor *ep_ctrl;
  65. struct urb *urb_ctrl;
  66. u8 *urb_ctrl_buf;
  67. dma_addr_t ctrl_dma;
  68. size_t max_ctrl_size;
  69. struct usb_interface *data_intf;
  70. struct usb_endpoint_descriptor *ep_in;
  71. struct urb *urb_in;
  72. u8 *urb_in_buf;
  73. dma_addr_t read_dma;
  74. size_t max_in_size;
  75. struct usb_endpoint_descriptor *ep_out;
  76. u8 *urb_out_buf;
  77. size_t max_out_size;
  78. u8 read_buf[IMS_PCU_BUF_SIZE];
  79. u8 read_pos;
  80. u8 check_sum;
  81. bool have_stx;
  82. bool have_dle;
  83. u8 cmd_buf[IMS_PCU_BUF_SIZE];
  84. u8 ack_id;
  85. u8 expected_response;
  86. u8 cmd_buf_len;
  87. struct completion cmd_done;
  88. struct mutex cmd_mutex;
  89. u32 fw_start_addr;
  90. u32 fw_end_addr;
  91. struct completion async_firmware_done;
  92. struct ims_pcu_buttons buttons;
  93. struct ims_pcu_gamepad *gamepad;
  94. struct ims_pcu_backlight backlight;
  95. bool setup_complete; /* Input and LED devices have been created */
  96. };
  97. /*********************************************************************
  98. * Buttons Input device support *
  99. *********************************************************************/
  100. static const unsigned short ims_pcu_keymap_1[] = {
  101. [1] = KEY_ATTENDANT_OFF,
  102. [2] = KEY_ATTENDANT_ON,
  103. [3] = KEY_LIGHTS_TOGGLE,
  104. [4] = KEY_VOLUMEUP,
  105. [5] = KEY_VOLUMEDOWN,
  106. [6] = KEY_INFO,
  107. };
  108. static const unsigned short ims_pcu_keymap_2[] = {
  109. [4] = KEY_VOLUMEUP,
  110. [5] = KEY_VOLUMEDOWN,
  111. [6] = KEY_INFO,
  112. };
  113. static const unsigned short ims_pcu_keymap_3[] = {
  114. [1] = KEY_HOMEPAGE,
  115. [2] = KEY_ATTENDANT_TOGGLE,
  116. [3] = KEY_LIGHTS_TOGGLE,
  117. [4] = KEY_VOLUMEUP,
  118. [5] = KEY_VOLUMEDOWN,
  119. [6] = KEY_DISPLAYTOGGLE,
  120. [18] = KEY_PLAYPAUSE,
  121. };
  122. static const unsigned short ims_pcu_keymap_4[] = {
  123. [1] = KEY_ATTENDANT_OFF,
  124. [2] = KEY_ATTENDANT_ON,
  125. [3] = KEY_LIGHTS_TOGGLE,
  126. [4] = KEY_VOLUMEUP,
  127. [5] = KEY_VOLUMEDOWN,
  128. [6] = KEY_INFO,
  129. [18] = KEY_PLAYPAUSE,
  130. };
  131. static const unsigned short ims_pcu_keymap_5[] = {
  132. [1] = KEY_ATTENDANT_OFF,
  133. [2] = KEY_ATTENDANT_ON,
  134. [3] = KEY_LIGHTS_TOGGLE,
  135. };
  136. struct ims_pcu_device_info {
  137. const unsigned short *keymap;
  138. size_t keymap_len;
  139. bool has_gamepad;
  140. };
  141. #define IMS_PCU_DEVINFO(_n, _gamepad) \
  142. [_n] = { \
  143. .keymap = ims_pcu_keymap_##_n, \
  144. .keymap_len = ARRAY_SIZE(ims_pcu_keymap_##_n), \
  145. .has_gamepad = _gamepad, \
  146. }
  147. static const struct ims_pcu_device_info ims_pcu_device_info[] = {
  148. IMS_PCU_DEVINFO(1, true),
  149. IMS_PCU_DEVINFO(2, true),
  150. IMS_PCU_DEVINFO(3, true),
  151. IMS_PCU_DEVINFO(4, true),
  152. IMS_PCU_DEVINFO(5, false),
  153. };
  154. static void ims_pcu_buttons_report(struct ims_pcu *pcu, u32 data)
  155. {
  156. struct ims_pcu_buttons *buttons = &pcu->buttons;
  157. struct input_dev *input = buttons->input;
  158. int i;
  159. for (i = 0; i < 32; i++) {
  160. unsigned short keycode = buttons->keymap[i];
  161. if (keycode != KEY_RESERVED)
  162. input_report_key(input, keycode, data & (1UL << i));
  163. }
  164. input_sync(input);
  165. }
  166. static int ims_pcu_setup_buttons(struct ims_pcu *pcu,
  167. const unsigned short *keymap,
  168. size_t keymap_len)
  169. {
  170. struct ims_pcu_buttons *buttons = &pcu->buttons;
  171. struct input_dev *input;
  172. int i;
  173. int error;
  174. input = input_allocate_device();
  175. if (!input) {
  176. dev_err(pcu->dev,
  177. "Not enough memory for input input device\n");
  178. return -ENOMEM;
  179. }
  180. snprintf(buttons->name, sizeof(buttons->name),
  181. "IMS PCU#%d Button Interface", pcu->device_no);
  182. usb_make_path(pcu->udev, buttons->phys, sizeof(buttons->phys));
  183. strlcat(buttons->phys, "/input0", sizeof(buttons->phys));
  184. memcpy(buttons->keymap, keymap, sizeof(*keymap) * keymap_len);
  185. input->name = buttons->name;
  186. input->phys = buttons->phys;
  187. usb_to_input_id(pcu->udev, &input->id);
  188. input->dev.parent = &pcu->ctrl_intf->dev;
  189. input->keycode = buttons->keymap;
  190. input->keycodemax = ARRAY_SIZE(buttons->keymap);
  191. input->keycodesize = sizeof(buttons->keymap[0]);
  192. __set_bit(EV_KEY, input->evbit);
  193. for (i = 0; i < IMS_PCU_KEYMAP_LEN; i++)
  194. __set_bit(buttons->keymap[i], input->keybit);
  195. __clear_bit(KEY_RESERVED, input->keybit);
  196. error = input_register_device(input);
  197. if (error) {
  198. dev_err(pcu->dev,
  199. "Failed to register buttons input device: %d\n",
  200. error);
  201. input_free_device(input);
  202. return error;
  203. }
  204. buttons->input = input;
  205. return 0;
  206. }
  207. static void ims_pcu_destroy_buttons(struct ims_pcu *pcu)
  208. {
  209. struct ims_pcu_buttons *buttons = &pcu->buttons;
  210. input_unregister_device(buttons->input);
  211. }
  212. /*********************************************************************
  213. * Gamepad Input device support *
  214. *********************************************************************/
  215. static void ims_pcu_gamepad_report(struct ims_pcu *pcu, u32 data)
  216. {
  217. struct ims_pcu_gamepad *gamepad = pcu->gamepad;
  218. struct input_dev *input = gamepad->input;
  219. int x, y;
  220. x = !!(data & (1 << 14)) - !!(data & (1 << 13));
  221. y = !!(data & (1 << 12)) - !!(data & (1 << 11));
  222. input_report_abs(input, ABS_X, x);
  223. input_report_abs(input, ABS_Y, y);
  224. input_report_key(input, BTN_A, data & (1 << 7));
  225. input_report_key(input, BTN_B, data & (1 << 8));
  226. input_report_key(input, BTN_X, data & (1 << 9));
  227. input_report_key(input, BTN_Y, data & (1 << 10));
  228. input_report_key(input, BTN_START, data & (1 << 15));
  229. input_report_key(input, BTN_SELECT, data & (1 << 16));
  230. input_sync(input);
  231. }
  232. static int ims_pcu_setup_gamepad(struct ims_pcu *pcu)
  233. {
  234. struct ims_pcu_gamepad *gamepad;
  235. struct input_dev *input;
  236. int error;
  237. gamepad = kzalloc(sizeof(struct ims_pcu_gamepad), GFP_KERNEL);
  238. input = input_allocate_device();
  239. if (!gamepad || !input) {
  240. dev_err(pcu->dev,
  241. "Not enough memory for gamepad device\n");
  242. error = -ENOMEM;
  243. goto err_free_mem;
  244. }
  245. gamepad->input = input;
  246. snprintf(gamepad->name, sizeof(gamepad->name),
  247. "IMS PCU#%d Gamepad Interface", pcu->device_no);
  248. usb_make_path(pcu->udev, gamepad->phys, sizeof(gamepad->phys));
  249. strlcat(gamepad->phys, "/input1", sizeof(gamepad->phys));
  250. input->name = gamepad->name;
  251. input->phys = gamepad->phys;
  252. usb_to_input_id(pcu->udev, &input->id);
  253. input->dev.parent = &pcu->ctrl_intf->dev;
  254. __set_bit(EV_KEY, input->evbit);
  255. __set_bit(BTN_A, input->keybit);
  256. __set_bit(BTN_B, input->keybit);
  257. __set_bit(BTN_X, input->keybit);
  258. __set_bit(BTN_Y, input->keybit);
  259. __set_bit(BTN_START, input->keybit);
  260. __set_bit(BTN_SELECT, input->keybit);
  261. __set_bit(EV_ABS, input->evbit);
  262. input_set_abs_params(input, ABS_X, -1, 1, 0, 0);
  263. input_set_abs_params(input, ABS_Y, -1, 1, 0, 0);
  264. error = input_register_device(input);
  265. if (error) {
  266. dev_err(pcu->dev,
  267. "Failed to register gamepad input device: %d\n",
  268. error);
  269. goto err_free_mem;
  270. }
  271. pcu->gamepad = gamepad;
  272. return 0;
  273. err_free_mem:
  274. input_free_device(input);
  275. kfree(gamepad);
  276. return -ENOMEM;
  277. }
  278. static void ims_pcu_destroy_gamepad(struct ims_pcu *pcu)
  279. {
  280. struct ims_pcu_gamepad *gamepad = pcu->gamepad;
  281. input_unregister_device(gamepad->input);
  282. kfree(gamepad);
  283. }
  284. /*********************************************************************
  285. * PCU Communication protocol handling *
  286. *********************************************************************/
  287. #define IMS_PCU_PROTOCOL_STX 0x02
  288. #define IMS_PCU_PROTOCOL_ETX 0x03
  289. #define IMS_PCU_PROTOCOL_DLE 0x10
  290. /* PCU commands */
  291. #define IMS_PCU_CMD_STATUS 0xa0
  292. #define IMS_PCU_CMD_PCU_RESET 0xa1
  293. #define IMS_PCU_CMD_RESET_REASON 0xa2
  294. #define IMS_PCU_CMD_SEND_BUTTONS 0xa3
  295. #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4
  296. #define IMS_PCU_CMD_GET_INFO 0xa5
  297. #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6
  298. #define IMS_PCU_CMD_EEPROM 0xa7
  299. #define IMS_PCU_CMD_GET_FW_VERSION 0xa8
  300. #define IMS_PCU_CMD_GET_BL_VERSION 0xa9
  301. #define IMS_PCU_CMD_SET_INFO 0xab
  302. #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac
  303. #define IMS_PCU_CMD_GET_DEVICE_ID 0xae
  304. #define IMS_PCU_CMD_SPECIAL_INFO 0xb0
  305. #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */
  306. #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3
  307. #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4
  308. /* PCU responses */
  309. #define IMS_PCU_RSP_STATUS 0xc0
  310. #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
  311. #define IMS_PCU_RSP_RESET_REASON 0xc2
  312. #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
  313. #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
  314. #define IMS_PCU_RSP_GET_INFO 0xc5
  315. #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
  316. #define IMS_PCU_RSP_EEPROM 0xc7
  317. #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
  318. #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
  319. #define IMS_PCU_RSP_SET_INFO 0xcb
  320. #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
  321. #define IMS_PCU_RSP_CMD_INVALID 0xcd
  322. #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
  323. #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
  324. #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
  325. #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2
  326. #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3
  327. #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
  328. #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
  329. #define IMS_PCU_MIN_PACKET_LEN 3
  330. #define IMS_PCU_DATA_OFFSET 2
  331. #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
  332. #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
  333. static void ims_pcu_report_events(struct ims_pcu *pcu)
  334. {
  335. u32 data = get_unaligned_be32(&pcu->read_buf[3]);
  336. ims_pcu_buttons_report(pcu, data & ~IMS_PCU_GAMEPAD_MASK);
  337. if (pcu->gamepad)
  338. ims_pcu_gamepad_report(pcu, data);
  339. }
  340. static void ims_pcu_handle_response(struct ims_pcu *pcu)
  341. {
  342. switch (pcu->read_buf[0]) {
  343. case IMS_PCU_RSP_EVNT_BUTTONS:
  344. if (likely(pcu->setup_complete))
  345. ims_pcu_report_events(pcu);
  346. break;
  347. default:
  348. /*
  349. * See if we got command completion.
  350. * If both the sequence and response code match save
  351. * the data and signal completion.
  352. */
  353. if (pcu->read_buf[0] == pcu->expected_response &&
  354. pcu->read_buf[1] == pcu->ack_id - 1) {
  355. memcpy(pcu->cmd_buf, pcu->read_buf, pcu->read_pos);
  356. pcu->cmd_buf_len = pcu->read_pos;
  357. complete(&pcu->cmd_done);
  358. }
  359. break;
  360. }
  361. }
  362. static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb)
  363. {
  364. int i;
  365. for (i = 0; i < urb->actual_length; i++) {
  366. u8 data = pcu->urb_in_buf[i];
  367. /* Skip everything until we get Start Xmit */
  368. if (!pcu->have_stx && data != IMS_PCU_PROTOCOL_STX)
  369. continue;
  370. if (pcu->have_dle) {
  371. pcu->have_dle = false;
  372. pcu->read_buf[pcu->read_pos++] = data;
  373. pcu->check_sum += data;
  374. continue;
  375. }
  376. switch (data) {
  377. case IMS_PCU_PROTOCOL_STX:
  378. if (pcu->have_stx)
  379. dev_warn(pcu->dev,
  380. "Unexpected STX at byte %d, discarding old data\n",
  381. pcu->read_pos);
  382. pcu->have_stx = true;
  383. pcu->have_dle = false;
  384. pcu->read_pos = 0;
  385. pcu->check_sum = 0;
  386. break;
  387. case IMS_PCU_PROTOCOL_DLE:
  388. pcu->have_dle = true;
  389. break;
  390. case IMS_PCU_PROTOCOL_ETX:
  391. if (pcu->read_pos < IMS_PCU_MIN_PACKET_LEN) {
  392. dev_warn(pcu->dev,
  393. "Short packet received (%d bytes), ignoring\n",
  394. pcu->read_pos);
  395. } else if (pcu->check_sum != 0) {
  396. dev_warn(pcu->dev,
  397. "Invalid checksum in packet (%d bytes), ignoring\n",
  398. pcu->read_pos);
  399. } else {
  400. ims_pcu_handle_response(pcu);
  401. }
  402. pcu->have_stx = false;
  403. pcu->have_dle = false;
  404. pcu->read_pos = 0;
  405. break;
  406. default:
  407. pcu->read_buf[pcu->read_pos++] = data;
  408. pcu->check_sum += data;
  409. break;
  410. }
  411. }
  412. }
  413. static bool ims_pcu_byte_needs_escape(u8 byte)
  414. {
  415. return byte == IMS_PCU_PROTOCOL_STX ||
  416. byte == IMS_PCU_PROTOCOL_ETX ||
  417. byte == IMS_PCU_PROTOCOL_DLE;
  418. }
  419. static int ims_pcu_send_cmd_chunk(struct ims_pcu *pcu,
  420. u8 command, int chunk, int len)
  421. {
  422. int error;
  423. error = usb_bulk_msg(pcu->udev,
  424. usb_sndbulkpipe(pcu->udev,
  425. pcu->ep_out->bEndpointAddress),
  426. pcu->urb_out_buf, len,
  427. NULL, IMS_PCU_CMD_WRITE_TIMEOUT);
  428. if (error < 0) {
  429. dev_dbg(pcu->dev,
  430. "Sending 0x%02x command failed at chunk %d: %d\n",
  431. command, chunk, error);
  432. return error;
  433. }
  434. return 0;
  435. }
  436. static int ims_pcu_send_command(struct ims_pcu *pcu,
  437. u8 command, const u8 *data, int len)
  438. {
  439. int count = 0;
  440. int chunk = 0;
  441. int delta;
  442. int i;
  443. int error;
  444. u8 csum = 0;
  445. u8 ack_id;
  446. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_STX;
  447. /* We know the command need not be escaped */
  448. pcu->urb_out_buf[count++] = command;
  449. csum += command;
  450. ack_id = pcu->ack_id++;
  451. if (ack_id == 0xff)
  452. ack_id = pcu->ack_id++;
  453. if (ims_pcu_byte_needs_escape(ack_id))
  454. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  455. pcu->urb_out_buf[count++] = ack_id;
  456. csum += ack_id;
  457. for (i = 0; i < len; i++) {
  458. delta = ims_pcu_byte_needs_escape(data[i]) ? 2 : 1;
  459. if (count + delta >= pcu->max_out_size) {
  460. error = ims_pcu_send_cmd_chunk(pcu, command,
  461. ++chunk, count);
  462. if (error)
  463. return error;
  464. count = 0;
  465. }
  466. if (delta == 2)
  467. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  468. pcu->urb_out_buf[count++] = data[i];
  469. csum += data[i];
  470. }
  471. csum = 1 + ~csum;
  472. delta = ims_pcu_byte_needs_escape(csum) ? 3 : 2;
  473. if (count + delta >= pcu->max_out_size) {
  474. error = ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
  475. if (error)
  476. return error;
  477. count = 0;
  478. }
  479. if (delta == 3)
  480. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  481. pcu->urb_out_buf[count++] = csum;
  482. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_ETX;
  483. return ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
  484. }
  485. static int __ims_pcu_execute_command(struct ims_pcu *pcu,
  486. u8 command, const void *data, size_t len,
  487. u8 expected_response, int response_time)
  488. {
  489. int error;
  490. pcu->expected_response = expected_response;
  491. init_completion(&pcu->cmd_done);
  492. error = ims_pcu_send_command(pcu, command, data, len);
  493. if (error)
  494. return error;
  495. if (expected_response &&
  496. !wait_for_completion_timeout(&pcu->cmd_done,
  497. msecs_to_jiffies(response_time))) {
  498. dev_dbg(pcu->dev, "Command 0x%02x timed out\n", command);
  499. return -ETIMEDOUT;
  500. }
  501. return 0;
  502. }
  503. #define ims_pcu_execute_command(pcu, code, data, len) \
  504. __ims_pcu_execute_command(pcu, \
  505. IMS_PCU_CMD_##code, data, len, \
  506. IMS_PCU_RSP_##code, \
  507. IMS_PCU_CMD_RESPONSE_TIMEOUT)
  508. #define ims_pcu_execute_query(pcu, code) \
  509. ims_pcu_execute_command(pcu, code, NULL, 0)
  510. /* Bootloader commands */
  511. #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
  512. #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
  513. #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
  514. #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
  515. #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
  516. #define IMS_PCU_BL_CMD_READ_APP 0xa6
  517. #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
  518. #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
  519. /* Bootloader commands */
  520. #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
  521. #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
  522. #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
  523. #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
  524. #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
  525. #define IMS_PCU_BL_RSP_READ_APP 0xc6
  526. #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
  527. #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
  528. #define IMS_PCU_BL_DATA_OFFSET 3
  529. static int __ims_pcu_execute_bl_command(struct ims_pcu *pcu,
  530. u8 command, const void *data, size_t len,
  531. u8 expected_response, int response_time)
  532. {
  533. int error;
  534. pcu->cmd_buf[0] = command;
  535. if (data)
  536. memcpy(&pcu->cmd_buf[1], data, len);
  537. error = __ims_pcu_execute_command(pcu,
  538. IMS_PCU_CMD_BOOTLOADER, pcu->cmd_buf, len + 1,
  539. expected_response ? IMS_PCU_RSP_BOOTLOADER : 0,
  540. response_time);
  541. if (error) {
  542. dev_err(pcu->dev,
  543. "Failure when sending 0x%02x command to bootloader, error: %d\n",
  544. pcu->cmd_buf[0], error);
  545. return error;
  546. }
  547. if (expected_response && pcu->cmd_buf[2] != expected_response) {
  548. dev_err(pcu->dev,
  549. "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
  550. pcu->cmd_buf[2], expected_response);
  551. return -EINVAL;
  552. }
  553. return 0;
  554. }
  555. #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
  556. __ims_pcu_execute_bl_command(pcu, \
  557. IMS_PCU_BL_CMD_##code, data, len, \
  558. IMS_PCU_BL_RSP_##code, timeout) \
  559. #define IMS_PCU_INFO_PART_OFFSET 2
  560. #define IMS_PCU_INFO_DOM_OFFSET 17
  561. #define IMS_PCU_INFO_SERIAL_OFFSET 25
  562. #define IMS_PCU_SET_INFO_SIZE 31
  563. static int ims_pcu_get_info(struct ims_pcu *pcu)
  564. {
  565. int error;
  566. error = ims_pcu_execute_query(pcu, GET_INFO);
  567. if (error) {
  568. dev_err(pcu->dev,
  569. "GET_INFO command failed, error: %d\n", error);
  570. return error;
  571. }
  572. memcpy(pcu->part_number,
  573. &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
  574. sizeof(pcu->part_number));
  575. memcpy(pcu->date_of_manufacturing,
  576. &pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
  577. sizeof(pcu->date_of_manufacturing));
  578. memcpy(pcu->serial_number,
  579. &pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
  580. sizeof(pcu->serial_number));
  581. return 0;
  582. }
  583. static int ims_pcu_set_info(struct ims_pcu *pcu)
  584. {
  585. int error;
  586. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
  587. pcu->part_number, sizeof(pcu->part_number));
  588. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
  589. pcu->date_of_manufacturing, sizeof(pcu->date_of_manufacturing));
  590. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
  591. pcu->serial_number, sizeof(pcu->serial_number));
  592. error = ims_pcu_execute_command(pcu, SET_INFO,
  593. &pcu->cmd_buf[IMS_PCU_DATA_OFFSET],
  594. IMS_PCU_SET_INFO_SIZE);
  595. if (error) {
  596. dev_err(pcu->dev,
  597. "Failed to update device information, error: %d\n",
  598. error);
  599. return error;
  600. }
  601. return 0;
  602. }
  603. static int ims_pcu_switch_to_bootloader(struct ims_pcu *pcu)
  604. {
  605. int error;
  606. /* Execute jump to the bootoloader */
  607. error = ims_pcu_execute_command(pcu, JUMP_TO_BTLDR, NULL, 0);
  608. if (error) {
  609. dev_err(pcu->dev,
  610. "Failure when sending JUMP TO BOOLTLOADER command, error: %d\n",
  611. error);
  612. return error;
  613. }
  614. return 0;
  615. }
  616. /*********************************************************************
  617. * Firmware Update handling *
  618. *********************************************************************/
  619. #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
  620. struct ims_pcu_flash_fmt {
  621. __le32 addr;
  622. u8 len;
  623. u8 data[];
  624. };
  625. static unsigned int ims_pcu_count_fw_records(const struct firmware *fw)
  626. {
  627. const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
  628. unsigned int count = 0;
  629. while (rec) {
  630. count++;
  631. rec = ihex_next_binrec(rec);
  632. }
  633. return count;
  634. }
  635. static int ims_pcu_verify_block(struct ims_pcu *pcu,
  636. u32 addr, u8 len, const u8 *data)
  637. {
  638. struct ims_pcu_flash_fmt *fragment;
  639. int error;
  640. fragment = (void *)&pcu->cmd_buf[1];
  641. put_unaligned_le32(addr, &fragment->addr);
  642. fragment->len = len;
  643. error = ims_pcu_execute_bl_command(pcu, READ_APP, NULL, 5,
  644. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  645. if (error) {
  646. dev_err(pcu->dev,
  647. "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
  648. addr, len, error);
  649. return error;
  650. }
  651. fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET];
  652. if (get_unaligned_le32(&fragment->addr) != addr ||
  653. fragment->len != len) {
  654. dev_err(pcu->dev,
  655. "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
  656. addr, get_unaligned_le32(&fragment->addr),
  657. len, fragment->len);
  658. return -EINVAL;
  659. }
  660. if (memcmp(fragment->data, data, len)) {
  661. dev_err(pcu->dev,
  662. "Mismatch in block at 0x%08x, len %d\n",
  663. addr, len);
  664. return -EINVAL;
  665. }
  666. return 0;
  667. }
  668. static int ims_pcu_flash_firmware(struct ims_pcu *pcu,
  669. const struct firmware *fw,
  670. unsigned int n_fw_records)
  671. {
  672. const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
  673. struct ims_pcu_flash_fmt *fragment;
  674. unsigned int count = 0;
  675. u32 addr;
  676. u8 len;
  677. int error;
  678. error = ims_pcu_execute_bl_command(pcu, ERASE_APP, NULL, 0, 2000);
  679. if (error) {
  680. dev_err(pcu->dev,
  681. "Failed to erase application image, error: %d\n",
  682. error);
  683. return error;
  684. }
  685. while (rec) {
  686. /*
  687. * The firmware format is messed up for some reason.
  688. * The address twice that of what is needed for some
  689. * reason and we end up overwriting half of the data
  690. * with the next record.
  691. */
  692. addr = be32_to_cpu(rec->addr) / 2;
  693. len = be16_to_cpu(rec->len);
  694. fragment = (void *)&pcu->cmd_buf[1];
  695. put_unaligned_le32(addr, &fragment->addr);
  696. fragment->len = len;
  697. memcpy(fragment->data, rec->data, len);
  698. error = ims_pcu_execute_bl_command(pcu, PROGRAM_DEVICE,
  699. NULL, len + 5,
  700. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  701. if (error) {
  702. dev_err(pcu->dev,
  703. "Failed to write block at 0x%08x, len %d, error: %d\n",
  704. addr, len, error);
  705. return error;
  706. }
  707. if (addr >= pcu->fw_start_addr && addr < pcu->fw_end_addr) {
  708. error = ims_pcu_verify_block(pcu, addr, len, rec->data);
  709. if (error)
  710. return error;
  711. }
  712. count++;
  713. pcu->update_firmware_status = (count * 100) / n_fw_records;
  714. rec = ihex_next_binrec(rec);
  715. }
  716. error = ims_pcu_execute_bl_command(pcu, PROGRAM_COMPLETE,
  717. NULL, 0, 2000);
  718. if (error)
  719. dev_err(pcu->dev,
  720. "Failed to send PROGRAM_COMPLETE, error: %d\n",
  721. error);
  722. return 0;
  723. }
  724. static int ims_pcu_handle_firmware_update(struct ims_pcu *pcu,
  725. const struct firmware *fw)
  726. {
  727. unsigned int n_fw_records;
  728. int retval;
  729. dev_info(pcu->dev, "Updating firmware %s, size: %zu\n",
  730. IMS_PCU_FIRMWARE_NAME, fw->size);
  731. n_fw_records = ims_pcu_count_fw_records(fw);
  732. retval = ims_pcu_flash_firmware(pcu, fw, n_fw_records);
  733. if (retval)
  734. goto out;
  735. retval = ims_pcu_execute_bl_command(pcu, LAUNCH_APP, NULL, 0, 0);
  736. if (retval)
  737. dev_err(pcu->dev,
  738. "Failed to start application image, error: %d\n",
  739. retval);
  740. out:
  741. pcu->update_firmware_status = retval;
  742. sysfs_notify(&pcu->dev->kobj, NULL, "update_firmware_status");
  743. return retval;
  744. }
  745. static void ims_pcu_process_async_firmware(const struct firmware *fw,
  746. void *context)
  747. {
  748. struct ims_pcu *pcu = context;
  749. int error;
  750. if (!fw) {
  751. dev_err(pcu->dev, "Failed to get firmware %s\n",
  752. IMS_PCU_FIRMWARE_NAME);
  753. goto out;
  754. }
  755. error = ihex_validate_fw(fw);
  756. if (error) {
  757. dev_err(pcu->dev, "Firmware %s is invalid\n",
  758. IMS_PCU_FIRMWARE_NAME);
  759. goto out;
  760. }
  761. mutex_lock(&pcu->cmd_mutex);
  762. ims_pcu_handle_firmware_update(pcu, fw);
  763. mutex_unlock(&pcu->cmd_mutex);
  764. release_firmware(fw);
  765. out:
  766. complete(&pcu->async_firmware_done);
  767. }
  768. /*********************************************************************
  769. * Backlight LED device support *
  770. *********************************************************************/
  771. #define IMS_PCU_MAX_BRIGHTNESS 31998
  772. static void ims_pcu_backlight_work(struct work_struct *work)
  773. {
  774. struct ims_pcu_backlight *backlight =
  775. container_of(work, struct ims_pcu_backlight, work);
  776. struct ims_pcu *pcu =
  777. container_of(backlight, struct ims_pcu, backlight);
  778. int desired_brightness = backlight->desired_brightness;
  779. __le16 br_val = cpu_to_le16(desired_brightness);
  780. int error;
  781. mutex_lock(&pcu->cmd_mutex);
  782. error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS,
  783. &br_val, sizeof(br_val));
  784. if (error && error != -ENODEV)
  785. dev_warn(pcu->dev,
  786. "Failed to set desired brightness %u, error: %d\n",
  787. desired_brightness, error);
  788. mutex_unlock(&pcu->cmd_mutex);
  789. }
  790. static void ims_pcu_backlight_set_brightness(struct led_classdev *cdev,
  791. enum led_brightness value)
  792. {
  793. struct ims_pcu_backlight *backlight =
  794. container_of(cdev, struct ims_pcu_backlight, cdev);
  795. backlight->desired_brightness = value;
  796. schedule_work(&backlight->work);
  797. }
  798. static enum led_brightness
  799. ims_pcu_backlight_get_brightness(struct led_classdev *cdev)
  800. {
  801. struct ims_pcu_backlight *backlight =
  802. container_of(cdev, struct ims_pcu_backlight, cdev);
  803. struct ims_pcu *pcu =
  804. container_of(backlight, struct ims_pcu, backlight);
  805. int brightness;
  806. int error;
  807. mutex_lock(&pcu->cmd_mutex);
  808. error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS);
  809. if (error) {
  810. dev_warn(pcu->dev,
  811. "Failed to get current brightness, error: %d\n",
  812. error);
  813. /* Assume the LED is OFF */
  814. brightness = LED_OFF;
  815. } else {
  816. brightness =
  817. get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
  818. }
  819. mutex_unlock(&pcu->cmd_mutex);
  820. return brightness;
  821. }
  822. static int ims_pcu_setup_backlight(struct ims_pcu *pcu)
  823. {
  824. struct ims_pcu_backlight *backlight = &pcu->backlight;
  825. int error;
  826. INIT_WORK(&backlight->work, ims_pcu_backlight_work);
  827. snprintf(backlight->name, sizeof(backlight->name),
  828. "pcu%d::kbd_backlight", pcu->device_no);
  829. backlight->cdev.name = backlight->name;
  830. backlight->cdev.max_brightness = IMS_PCU_MAX_BRIGHTNESS;
  831. backlight->cdev.brightness_get = ims_pcu_backlight_get_brightness;
  832. backlight->cdev.brightness_set = ims_pcu_backlight_set_brightness;
  833. error = led_classdev_register(pcu->dev, &backlight->cdev);
  834. if (error) {
  835. dev_err(pcu->dev,
  836. "Failed to register backlight LED device, error: %d\n",
  837. error);
  838. return error;
  839. }
  840. return 0;
  841. }
  842. static void ims_pcu_destroy_backlight(struct ims_pcu *pcu)
  843. {
  844. struct ims_pcu_backlight *backlight = &pcu->backlight;
  845. led_classdev_unregister(&backlight->cdev);
  846. cancel_work_sync(&backlight->work);
  847. }
  848. /*********************************************************************
  849. * Sysfs attributes handling *
  850. *********************************************************************/
  851. struct ims_pcu_attribute {
  852. struct device_attribute dattr;
  853. size_t field_offset;
  854. int field_length;
  855. };
  856. static ssize_t ims_pcu_attribute_show(struct device *dev,
  857. struct device_attribute *dattr,
  858. char *buf)
  859. {
  860. struct usb_interface *intf = to_usb_interface(dev);
  861. struct ims_pcu *pcu = usb_get_intfdata(intf);
  862. struct ims_pcu_attribute *attr =
  863. container_of(dattr, struct ims_pcu_attribute, dattr);
  864. char *field = (char *)pcu + attr->field_offset;
  865. return scnprintf(buf, PAGE_SIZE, "%.*s\n", attr->field_length, field);
  866. }
  867. static ssize_t ims_pcu_attribute_store(struct device *dev,
  868. struct device_attribute *dattr,
  869. const char *buf, size_t count)
  870. {
  871. struct usb_interface *intf = to_usb_interface(dev);
  872. struct ims_pcu *pcu = usb_get_intfdata(intf);
  873. struct ims_pcu_attribute *attr =
  874. container_of(dattr, struct ims_pcu_attribute, dattr);
  875. char *field = (char *)pcu + attr->field_offset;
  876. size_t data_len;
  877. int error;
  878. if (count > attr->field_length)
  879. return -EINVAL;
  880. data_len = strnlen(buf, attr->field_length);
  881. if (data_len > attr->field_length)
  882. return -EINVAL;
  883. error = mutex_lock_interruptible(&pcu->cmd_mutex);
  884. if (error)
  885. return error;
  886. memset(field, 0, attr->field_length);
  887. memcpy(field, buf, data_len);
  888. error = ims_pcu_set_info(pcu);
  889. /*
  890. * Even if update failed, let's fetch the info again as we just
  891. * clobbered one of the fields.
  892. */
  893. ims_pcu_get_info(pcu);
  894. mutex_unlock(&pcu->cmd_mutex);
  895. return error < 0 ? error : count;
  896. }
  897. #define IMS_PCU_ATTR(_field, _mode) \
  898. struct ims_pcu_attribute ims_pcu_attr_##_field = { \
  899. .dattr = __ATTR(_field, _mode, \
  900. ims_pcu_attribute_show, \
  901. ims_pcu_attribute_store), \
  902. .field_offset = offsetof(struct ims_pcu, _field), \
  903. .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
  904. }
  905. #define IMS_PCU_RO_ATTR(_field) \
  906. IMS_PCU_ATTR(_field, S_IRUGO)
  907. #define IMS_PCU_RW_ATTR(_field) \
  908. IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
  909. static IMS_PCU_RW_ATTR(part_number);
  910. static IMS_PCU_RW_ATTR(serial_number);
  911. static IMS_PCU_RW_ATTR(date_of_manufacturing);
  912. static IMS_PCU_RO_ATTR(fw_version);
  913. static IMS_PCU_RO_ATTR(bl_version);
  914. static IMS_PCU_RO_ATTR(reset_reason);
  915. static ssize_t ims_pcu_reset_device(struct device *dev,
  916. struct device_attribute *dattr,
  917. const char *buf, size_t count)
  918. {
  919. static const u8 reset_byte = 1;
  920. struct usb_interface *intf = to_usb_interface(dev);
  921. struct ims_pcu *pcu = usb_get_intfdata(intf);
  922. int value;
  923. int error;
  924. error = kstrtoint(buf, 0, &value);
  925. if (error)
  926. return error;
  927. if (value != 1)
  928. return -EINVAL;
  929. dev_info(pcu->dev, "Attempting to reset device\n");
  930. error = ims_pcu_execute_command(pcu, PCU_RESET, &reset_byte, 1);
  931. if (error) {
  932. dev_info(pcu->dev,
  933. "Failed to reset device, error: %d\n",
  934. error);
  935. return error;
  936. }
  937. return count;
  938. }
  939. static DEVICE_ATTR(reset_device, S_IWUSR, NULL, ims_pcu_reset_device);
  940. static ssize_t ims_pcu_update_firmware_store(struct device *dev,
  941. struct device_attribute *dattr,
  942. const char *buf, size_t count)
  943. {
  944. struct usb_interface *intf = to_usb_interface(dev);
  945. struct ims_pcu *pcu = usb_get_intfdata(intf);
  946. const struct firmware *fw = NULL;
  947. int value;
  948. int error;
  949. error = kstrtoint(buf, 0, &value);
  950. if (error)
  951. return error;
  952. if (value != 1)
  953. return -EINVAL;
  954. error = mutex_lock_interruptible(&pcu->cmd_mutex);
  955. if (error)
  956. return error;
  957. error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev);
  958. if (error) {
  959. dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n",
  960. IMS_PCU_FIRMWARE_NAME, error);
  961. goto out;
  962. }
  963. /*
  964. * If we are already in bootloader mode we can proceed with
  965. * flashing the firmware.
  966. *
  967. * If we are in application mode, then we need to switch into
  968. * bootloader mode, which will cause the device to disconnect
  969. * and reconnect as different device.
  970. */
  971. if (pcu->bootloader_mode)
  972. error = ims_pcu_handle_firmware_update(pcu, fw);
  973. else
  974. error = ims_pcu_switch_to_bootloader(pcu);
  975. release_firmware(fw);
  976. out:
  977. mutex_unlock(&pcu->cmd_mutex);
  978. return error ?: count;
  979. }
  980. static DEVICE_ATTR(update_firmware, S_IWUSR,
  981. NULL, ims_pcu_update_firmware_store);
  982. static ssize_t
  983. ims_pcu_update_firmware_status_show(struct device *dev,
  984. struct device_attribute *dattr,
  985. char *buf)
  986. {
  987. struct usb_interface *intf = to_usb_interface(dev);
  988. struct ims_pcu *pcu = usb_get_intfdata(intf);
  989. return scnprintf(buf, PAGE_SIZE, "%d\n", pcu->update_firmware_status);
  990. }
  991. static DEVICE_ATTR(update_firmware_status, S_IRUGO,
  992. ims_pcu_update_firmware_status_show, NULL);
  993. static struct attribute *ims_pcu_attrs[] = {
  994. &ims_pcu_attr_part_number.dattr.attr,
  995. &ims_pcu_attr_serial_number.dattr.attr,
  996. &ims_pcu_attr_date_of_manufacturing.dattr.attr,
  997. &ims_pcu_attr_fw_version.dattr.attr,
  998. &ims_pcu_attr_bl_version.dattr.attr,
  999. &ims_pcu_attr_reset_reason.dattr.attr,
  1000. &dev_attr_reset_device.attr,
  1001. &dev_attr_update_firmware.attr,
  1002. &dev_attr_update_firmware_status.attr,
  1003. NULL
  1004. };
  1005. static umode_t ims_pcu_is_attr_visible(struct kobject *kobj,
  1006. struct attribute *attr, int n)
  1007. {
  1008. struct device *dev = container_of(kobj, struct device, kobj);
  1009. struct usb_interface *intf = to_usb_interface(dev);
  1010. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1011. umode_t mode = attr->mode;
  1012. if (pcu->bootloader_mode) {
  1013. if (attr != &dev_attr_update_firmware_status.attr &&
  1014. attr != &dev_attr_update_firmware.attr &&
  1015. attr != &dev_attr_reset_device.attr) {
  1016. mode = 0;
  1017. }
  1018. } else {
  1019. if (attr == &dev_attr_update_firmware_status.attr)
  1020. mode = 0;
  1021. }
  1022. return mode;
  1023. }
  1024. static struct attribute_group ims_pcu_attr_group = {
  1025. .is_visible = ims_pcu_is_attr_visible,
  1026. .attrs = ims_pcu_attrs,
  1027. };
  1028. /* Support for a separate OFN attribute group */
  1029. #define OFN_REG_RESULT_OFFSET 2
  1030. static int ims_pcu_read_ofn_config(struct ims_pcu *pcu, u8 addr, u8 *data)
  1031. {
  1032. int error;
  1033. s16 result;
  1034. error = ims_pcu_execute_command(pcu, OFN_GET_CONFIG,
  1035. &addr, sizeof(addr));
  1036. if (error)
  1037. return error;
  1038. result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
  1039. if (result < 0)
  1040. return -EIO;
  1041. /* We only need LSB */
  1042. *data = pcu->cmd_buf[OFN_REG_RESULT_OFFSET];
  1043. return 0;
  1044. }
  1045. static int ims_pcu_write_ofn_config(struct ims_pcu *pcu, u8 addr, u8 data)
  1046. {
  1047. u8 buffer[] = { addr, data };
  1048. int error;
  1049. s16 result;
  1050. error = ims_pcu_execute_command(pcu, OFN_SET_CONFIG,
  1051. &buffer, sizeof(buffer));
  1052. if (error)
  1053. return error;
  1054. result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
  1055. if (result < 0)
  1056. return -EIO;
  1057. return 0;
  1058. }
  1059. static ssize_t ims_pcu_ofn_reg_data_show(struct device *dev,
  1060. struct device_attribute *dattr,
  1061. char *buf)
  1062. {
  1063. struct usb_interface *intf = to_usb_interface(dev);
  1064. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1065. int error;
  1066. u8 data;
  1067. mutex_lock(&pcu->cmd_mutex);
  1068. error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data);
  1069. mutex_unlock(&pcu->cmd_mutex);
  1070. if (error)
  1071. return error;
  1072. return scnprintf(buf, PAGE_SIZE, "%x\n", data);
  1073. }
  1074. static ssize_t ims_pcu_ofn_reg_data_store(struct device *dev,
  1075. struct device_attribute *dattr,
  1076. const char *buf, size_t count)
  1077. {
  1078. struct usb_interface *intf = to_usb_interface(dev);
  1079. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1080. int error;
  1081. u8 value;
  1082. error = kstrtou8(buf, 0, &value);
  1083. if (error)
  1084. return error;
  1085. mutex_lock(&pcu->cmd_mutex);
  1086. error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value);
  1087. mutex_unlock(&pcu->cmd_mutex);
  1088. return error ?: count;
  1089. }
  1090. static DEVICE_ATTR(reg_data, S_IRUGO | S_IWUSR,
  1091. ims_pcu_ofn_reg_data_show, ims_pcu_ofn_reg_data_store);
  1092. static ssize_t ims_pcu_ofn_reg_addr_show(struct device *dev,
  1093. struct device_attribute *dattr,
  1094. char *buf)
  1095. {
  1096. struct usb_interface *intf = to_usb_interface(dev);
  1097. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1098. int error;
  1099. mutex_lock(&pcu->cmd_mutex);
  1100. error = scnprintf(buf, PAGE_SIZE, "%x\n", pcu->ofn_reg_addr);
  1101. mutex_unlock(&pcu->cmd_mutex);
  1102. return error;
  1103. }
  1104. static ssize_t ims_pcu_ofn_reg_addr_store(struct device *dev,
  1105. struct device_attribute *dattr,
  1106. const char *buf, size_t count)
  1107. {
  1108. struct usb_interface *intf = to_usb_interface(dev);
  1109. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1110. int error;
  1111. u8 value;
  1112. error = kstrtou8(buf, 0, &value);
  1113. if (error)
  1114. return error;
  1115. mutex_lock(&pcu->cmd_mutex);
  1116. pcu->ofn_reg_addr = value;
  1117. mutex_unlock(&pcu->cmd_mutex);
  1118. return count;
  1119. }
  1120. static DEVICE_ATTR(reg_addr, S_IRUGO | S_IWUSR,
  1121. ims_pcu_ofn_reg_addr_show, ims_pcu_ofn_reg_addr_store);
  1122. struct ims_pcu_ofn_bit_attribute {
  1123. struct device_attribute dattr;
  1124. u8 addr;
  1125. u8 nr;
  1126. };
  1127. static ssize_t ims_pcu_ofn_bit_show(struct device *dev,
  1128. struct device_attribute *dattr,
  1129. char *buf)
  1130. {
  1131. struct usb_interface *intf = to_usb_interface(dev);
  1132. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1133. struct ims_pcu_ofn_bit_attribute *attr =
  1134. container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr);
  1135. int error;
  1136. u8 data;
  1137. mutex_lock(&pcu->cmd_mutex);
  1138. error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
  1139. mutex_unlock(&pcu->cmd_mutex);
  1140. if (error)
  1141. return error;
  1142. return scnprintf(buf, PAGE_SIZE, "%d\n", !!(data & (1 << attr->nr)));
  1143. }
  1144. static ssize_t ims_pcu_ofn_bit_store(struct device *dev,
  1145. struct device_attribute *dattr,
  1146. const char *buf, size_t count)
  1147. {
  1148. struct usb_interface *intf = to_usb_interface(dev);
  1149. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1150. struct ims_pcu_ofn_bit_attribute *attr =
  1151. container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr);
  1152. int error;
  1153. int value;
  1154. u8 data;
  1155. error = kstrtoint(buf, 0, &value);
  1156. if (error)
  1157. return error;
  1158. if (value > 1)
  1159. return -EINVAL;
  1160. mutex_lock(&pcu->cmd_mutex);
  1161. error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
  1162. if (!error) {
  1163. if (value)
  1164. data |= 1U << attr->nr;
  1165. else
  1166. data &= ~(1U << attr->nr);
  1167. error = ims_pcu_write_ofn_config(pcu, attr->addr, data);
  1168. }
  1169. mutex_unlock(&pcu->cmd_mutex);
  1170. return error ?: count;
  1171. }
  1172. #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \
  1173. struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \
  1174. .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \
  1175. ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \
  1176. .addr = _addr, \
  1177. .nr = _nr, \
  1178. }
  1179. static IMS_PCU_OFN_BIT_ATTR(engine_enable, 0x60, 7);
  1180. static IMS_PCU_OFN_BIT_ATTR(speed_enable, 0x60, 6);
  1181. static IMS_PCU_OFN_BIT_ATTR(assert_enable, 0x60, 5);
  1182. static IMS_PCU_OFN_BIT_ATTR(xyquant_enable, 0x60, 4);
  1183. static IMS_PCU_OFN_BIT_ATTR(xyscale_enable, 0x60, 1);
  1184. static IMS_PCU_OFN_BIT_ATTR(scale_x2, 0x63, 6);
  1185. static IMS_PCU_OFN_BIT_ATTR(scale_y2, 0x63, 7);
  1186. static struct attribute *ims_pcu_ofn_attrs[] = {
  1187. &dev_attr_reg_data.attr,
  1188. &dev_attr_reg_addr.attr,
  1189. &ims_pcu_ofn_attr_engine_enable.dattr.attr,
  1190. &ims_pcu_ofn_attr_speed_enable.dattr.attr,
  1191. &ims_pcu_ofn_attr_assert_enable.dattr.attr,
  1192. &ims_pcu_ofn_attr_xyquant_enable.dattr.attr,
  1193. &ims_pcu_ofn_attr_xyscale_enable.dattr.attr,
  1194. &ims_pcu_ofn_attr_scale_x2.dattr.attr,
  1195. &ims_pcu_ofn_attr_scale_y2.dattr.attr,
  1196. NULL
  1197. };
  1198. static struct attribute_group ims_pcu_ofn_attr_group = {
  1199. .name = "ofn",
  1200. .attrs = ims_pcu_ofn_attrs,
  1201. };
  1202. static void ims_pcu_irq(struct urb *urb)
  1203. {
  1204. struct ims_pcu *pcu = urb->context;
  1205. int retval, status;
  1206. status = urb->status;
  1207. switch (status) {
  1208. case 0:
  1209. /* success */
  1210. break;
  1211. case -ECONNRESET:
  1212. case -ENOENT:
  1213. case -ESHUTDOWN:
  1214. /* this urb is terminated, clean up */
  1215. dev_dbg(pcu->dev, "%s - urb shutting down with status: %d\n",
  1216. __func__, status);
  1217. return;
  1218. default:
  1219. dev_dbg(pcu->dev, "%s - nonzero urb status received: %d\n",
  1220. __func__, status);
  1221. goto exit;
  1222. }
  1223. dev_dbg(pcu->dev, "%s: received %d: %*ph\n", __func__,
  1224. urb->actual_length, urb->actual_length, pcu->urb_in_buf);
  1225. if (urb == pcu->urb_in)
  1226. ims_pcu_process_data(pcu, urb);
  1227. exit:
  1228. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1229. if (retval && retval != -ENODEV)
  1230. dev_err(pcu->dev, "%s - usb_submit_urb failed with result %d\n",
  1231. __func__, retval);
  1232. }
  1233. static int ims_pcu_buffers_alloc(struct ims_pcu *pcu)
  1234. {
  1235. int error;
  1236. pcu->urb_in_buf = usb_alloc_coherent(pcu->udev, pcu->max_in_size,
  1237. GFP_KERNEL, &pcu->read_dma);
  1238. if (!pcu->urb_in_buf) {
  1239. dev_err(pcu->dev,
  1240. "Failed to allocate memory for read buffer\n");
  1241. return -ENOMEM;
  1242. }
  1243. pcu->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  1244. if (!pcu->urb_in) {
  1245. dev_err(pcu->dev, "Failed to allocate input URB\n");
  1246. error = -ENOMEM;
  1247. goto err_free_urb_in_buf;
  1248. }
  1249. pcu->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1250. pcu->urb_in->transfer_dma = pcu->read_dma;
  1251. usb_fill_bulk_urb(pcu->urb_in, pcu->udev,
  1252. usb_rcvbulkpipe(pcu->udev,
  1253. pcu->ep_in->bEndpointAddress),
  1254. pcu->urb_in_buf, pcu->max_in_size,
  1255. ims_pcu_irq, pcu);
  1256. /*
  1257. * We are using usb_bulk_msg() for sending so there is no point
  1258. * in allocating memory with usb_alloc_coherent().
  1259. */
  1260. pcu->urb_out_buf = kmalloc(pcu->max_out_size, GFP_KERNEL);
  1261. if (!pcu->urb_out_buf) {
  1262. dev_err(pcu->dev, "Failed to allocate memory for write buffer\n");
  1263. error = -ENOMEM;
  1264. goto err_free_in_urb;
  1265. }
  1266. pcu->urb_ctrl_buf = usb_alloc_coherent(pcu->udev, pcu->max_ctrl_size,
  1267. GFP_KERNEL, &pcu->ctrl_dma);
  1268. if (!pcu->urb_ctrl_buf) {
  1269. dev_err(pcu->dev,
  1270. "Failed to allocate memory for read buffer\n");
  1271. error = -ENOMEM;
  1272. goto err_free_urb_out_buf;
  1273. }
  1274. pcu->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL);
  1275. if (!pcu->urb_ctrl) {
  1276. dev_err(pcu->dev, "Failed to allocate input URB\n");
  1277. error = -ENOMEM;
  1278. goto err_free_urb_ctrl_buf;
  1279. }
  1280. pcu->urb_ctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1281. pcu->urb_ctrl->transfer_dma = pcu->ctrl_dma;
  1282. usb_fill_int_urb(pcu->urb_ctrl, pcu->udev,
  1283. usb_rcvintpipe(pcu->udev,
  1284. pcu->ep_ctrl->bEndpointAddress),
  1285. pcu->urb_ctrl_buf, pcu->max_ctrl_size,
  1286. ims_pcu_irq, pcu, pcu->ep_ctrl->bInterval);
  1287. return 0;
  1288. err_free_urb_ctrl_buf:
  1289. usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
  1290. pcu->urb_ctrl_buf, pcu->ctrl_dma);
  1291. err_free_urb_out_buf:
  1292. kfree(pcu->urb_out_buf);
  1293. err_free_in_urb:
  1294. usb_free_urb(pcu->urb_in);
  1295. err_free_urb_in_buf:
  1296. usb_free_coherent(pcu->udev, pcu->max_in_size,
  1297. pcu->urb_in_buf, pcu->read_dma);
  1298. return error;
  1299. }
  1300. static void ims_pcu_buffers_free(struct ims_pcu *pcu)
  1301. {
  1302. usb_kill_urb(pcu->urb_in);
  1303. usb_free_urb(pcu->urb_in);
  1304. usb_free_coherent(pcu->udev, pcu->max_out_size,
  1305. pcu->urb_in_buf, pcu->read_dma);
  1306. kfree(pcu->urb_out_buf);
  1307. usb_kill_urb(pcu->urb_ctrl);
  1308. usb_free_urb(pcu->urb_ctrl);
  1309. usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
  1310. pcu->urb_ctrl_buf, pcu->ctrl_dma);
  1311. }
  1312. static const struct usb_cdc_union_desc *
  1313. ims_pcu_get_cdc_union_desc(struct usb_interface *intf)
  1314. {
  1315. const void *buf = intf->altsetting->extra;
  1316. size_t buflen = intf->altsetting->extralen;
  1317. struct usb_cdc_union_desc *union_desc;
  1318. if (!buf) {
  1319. dev_err(&intf->dev, "Missing descriptor data\n");
  1320. return NULL;
  1321. }
  1322. if (!buflen) {
  1323. dev_err(&intf->dev, "Zero length descriptor\n");
  1324. return NULL;
  1325. }
  1326. while (buflen >= sizeof(*union_desc)) {
  1327. union_desc = (struct usb_cdc_union_desc *)buf;
  1328. if (union_desc->bLength > buflen) {
  1329. dev_err(&intf->dev, "Too large descriptor\n");
  1330. return NULL;
  1331. }
  1332. if (union_desc->bDescriptorType == USB_DT_CS_INTERFACE &&
  1333. union_desc->bDescriptorSubType == USB_CDC_UNION_TYPE) {
  1334. dev_dbg(&intf->dev, "Found union header\n");
  1335. if (union_desc->bLength >= sizeof(*union_desc))
  1336. return union_desc;
  1337. dev_err(&intf->dev,
  1338. "Union descriptor to short (%d vs %zd\n)",
  1339. union_desc->bLength, sizeof(*union_desc));
  1340. return NULL;
  1341. }
  1342. buflen -= union_desc->bLength;
  1343. buf += union_desc->bLength;
  1344. }
  1345. dev_err(&intf->dev, "Missing CDC union descriptor\n");
  1346. return NULL;
  1347. }
  1348. static int ims_pcu_parse_cdc_data(struct usb_interface *intf, struct ims_pcu *pcu)
  1349. {
  1350. const struct usb_cdc_union_desc *union_desc;
  1351. struct usb_host_interface *alt;
  1352. union_desc = ims_pcu_get_cdc_union_desc(intf);
  1353. if (!union_desc)
  1354. return -EINVAL;
  1355. pcu->ctrl_intf = usb_ifnum_to_if(pcu->udev,
  1356. union_desc->bMasterInterface0);
  1357. if (!pcu->ctrl_intf)
  1358. return -EINVAL;
  1359. alt = pcu->ctrl_intf->cur_altsetting;
  1360. if (alt->desc.bNumEndpoints < 1)
  1361. return -ENODEV;
  1362. pcu->ep_ctrl = &alt->endpoint[0].desc;
  1363. pcu->max_ctrl_size = usb_endpoint_maxp(pcu->ep_ctrl);
  1364. pcu->data_intf = usb_ifnum_to_if(pcu->udev,
  1365. union_desc->bSlaveInterface0);
  1366. if (!pcu->data_intf)
  1367. return -EINVAL;
  1368. alt = pcu->data_intf->cur_altsetting;
  1369. if (alt->desc.bNumEndpoints != 2) {
  1370. dev_err(pcu->dev,
  1371. "Incorrect number of endpoints on data interface (%d)\n",
  1372. alt->desc.bNumEndpoints);
  1373. return -EINVAL;
  1374. }
  1375. pcu->ep_out = &alt->endpoint[0].desc;
  1376. if (!usb_endpoint_is_bulk_out(pcu->ep_out)) {
  1377. dev_err(pcu->dev,
  1378. "First endpoint on data interface is not BULK OUT\n");
  1379. return -EINVAL;
  1380. }
  1381. pcu->max_out_size = usb_endpoint_maxp(pcu->ep_out);
  1382. if (pcu->max_out_size < 8) {
  1383. dev_err(pcu->dev,
  1384. "Max OUT packet size is too small (%zd)\n",
  1385. pcu->max_out_size);
  1386. return -EINVAL;
  1387. }
  1388. pcu->ep_in = &alt->endpoint[1].desc;
  1389. if (!usb_endpoint_is_bulk_in(pcu->ep_in)) {
  1390. dev_err(pcu->dev,
  1391. "Second endpoint on data interface is not BULK IN\n");
  1392. return -EINVAL;
  1393. }
  1394. pcu->max_in_size = usb_endpoint_maxp(pcu->ep_in);
  1395. if (pcu->max_in_size < 8) {
  1396. dev_err(pcu->dev,
  1397. "Max IN packet size is too small (%zd)\n",
  1398. pcu->max_in_size);
  1399. return -EINVAL;
  1400. }
  1401. return 0;
  1402. }
  1403. static int ims_pcu_start_io(struct ims_pcu *pcu)
  1404. {
  1405. int error;
  1406. error = usb_submit_urb(pcu->urb_ctrl, GFP_KERNEL);
  1407. if (error) {
  1408. dev_err(pcu->dev,
  1409. "Failed to start control IO - usb_submit_urb failed with result: %d\n",
  1410. error);
  1411. return -EIO;
  1412. }
  1413. error = usb_submit_urb(pcu->urb_in, GFP_KERNEL);
  1414. if (error) {
  1415. dev_err(pcu->dev,
  1416. "Failed to start IO - usb_submit_urb failed with result: %d\n",
  1417. error);
  1418. usb_kill_urb(pcu->urb_ctrl);
  1419. return -EIO;
  1420. }
  1421. return 0;
  1422. }
  1423. static void ims_pcu_stop_io(struct ims_pcu *pcu)
  1424. {
  1425. usb_kill_urb(pcu->urb_in);
  1426. usb_kill_urb(pcu->urb_ctrl);
  1427. }
  1428. static int ims_pcu_line_setup(struct ims_pcu *pcu)
  1429. {
  1430. struct usb_host_interface *interface = pcu->ctrl_intf->cur_altsetting;
  1431. struct usb_cdc_line_coding *line = (void *)pcu->cmd_buf;
  1432. int error;
  1433. memset(line, 0, sizeof(*line));
  1434. line->dwDTERate = cpu_to_le32(57600);
  1435. line->bDataBits = 8;
  1436. error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
  1437. USB_CDC_REQ_SET_LINE_CODING,
  1438. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1439. 0, interface->desc.bInterfaceNumber,
  1440. line, sizeof(struct usb_cdc_line_coding),
  1441. 5000);
  1442. if (error < 0) {
  1443. dev_err(pcu->dev, "Failed to set line coding, error: %d\n",
  1444. error);
  1445. return error;
  1446. }
  1447. error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
  1448. USB_CDC_REQ_SET_CONTROL_LINE_STATE,
  1449. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1450. 0x03, interface->desc.bInterfaceNumber,
  1451. NULL, 0, 5000);
  1452. if (error < 0) {
  1453. dev_err(pcu->dev, "Failed to set line state, error: %d\n",
  1454. error);
  1455. return error;
  1456. }
  1457. return 0;
  1458. }
  1459. static int ims_pcu_get_device_info(struct ims_pcu *pcu)
  1460. {
  1461. int error;
  1462. error = ims_pcu_get_info(pcu);
  1463. if (error)
  1464. return error;
  1465. error = ims_pcu_execute_query(pcu, GET_FW_VERSION);
  1466. if (error) {
  1467. dev_err(pcu->dev,
  1468. "GET_FW_VERSION command failed, error: %d\n", error);
  1469. return error;
  1470. }
  1471. snprintf(pcu->fw_version, sizeof(pcu->fw_version),
  1472. "%02d%02d%02d%02d.%c%c",
  1473. pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
  1474. pcu->cmd_buf[6], pcu->cmd_buf[7]);
  1475. error = ims_pcu_execute_query(pcu, GET_BL_VERSION);
  1476. if (error) {
  1477. dev_err(pcu->dev,
  1478. "GET_BL_VERSION command failed, error: %d\n", error);
  1479. return error;
  1480. }
  1481. snprintf(pcu->bl_version, sizeof(pcu->bl_version),
  1482. "%02d%02d%02d%02d.%c%c",
  1483. pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
  1484. pcu->cmd_buf[6], pcu->cmd_buf[7]);
  1485. error = ims_pcu_execute_query(pcu, RESET_REASON);
  1486. if (error) {
  1487. dev_err(pcu->dev,
  1488. "RESET_REASON command failed, error: %d\n", error);
  1489. return error;
  1490. }
  1491. snprintf(pcu->reset_reason, sizeof(pcu->reset_reason),
  1492. "%02x", pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
  1493. dev_dbg(pcu->dev,
  1494. "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
  1495. pcu->part_number,
  1496. pcu->date_of_manufacturing,
  1497. pcu->serial_number,
  1498. pcu->fw_version,
  1499. pcu->bl_version,
  1500. pcu->reset_reason);
  1501. return 0;
  1502. }
  1503. static int ims_pcu_identify_type(struct ims_pcu *pcu, u8 *device_id)
  1504. {
  1505. int error;
  1506. error = ims_pcu_execute_query(pcu, GET_DEVICE_ID);
  1507. if (error) {
  1508. dev_err(pcu->dev,
  1509. "GET_DEVICE_ID command failed, error: %d\n", error);
  1510. return error;
  1511. }
  1512. *device_id = pcu->cmd_buf[IMS_PCU_DATA_OFFSET];
  1513. dev_dbg(pcu->dev, "Detected device ID: %d\n", *device_id);
  1514. return 0;
  1515. }
  1516. static int ims_pcu_init_application_mode(struct ims_pcu *pcu)
  1517. {
  1518. static atomic_t device_no = ATOMIC_INIT(-1);
  1519. const struct ims_pcu_device_info *info;
  1520. int error;
  1521. error = ims_pcu_get_device_info(pcu);
  1522. if (error) {
  1523. /* Device does not respond to basic queries, hopeless */
  1524. return error;
  1525. }
  1526. error = ims_pcu_identify_type(pcu, &pcu->device_id);
  1527. if (error) {
  1528. dev_err(pcu->dev,
  1529. "Failed to identify device, error: %d\n", error);
  1530. /*
  1531. * Do not signal error, but do not create input nor
  1532. * backlight devices either, let userspace figure this
  1533. * out (flash a new firmware?).
  1534. */
  1535. return 0;
  1536. }
  1537. if (pcu->device_id >= ARRAY_SIZE(ims_pcu_device_info) ||
  1538. !ims_pcu_device_info[pcu->device_id].keymap) {
  1539. dev_err(pcu->dev, "Device ID %d is not valid\n", pcu->device_id);
  1540. /* Same as above, punt to userspace */
  1541. return 0;
  1542. }
  1543. /* Device appears to be operable, complete initialization */
  1544. pcu->device_no = atomic_inc_return(&device_no);
  1545. /*
  1546. * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor
  1547. */
  1548. if (pcu->device_id != IMS_PCU_PCU_B_DEVICE_ID) {
  1549. error = sysfs_create_group(&pcu->dev->kobj,
  1550. &ims_pcu_ofn_attr_group);
  1551. if (error)
  1552. return error;
  1553. }
  1554. error = ims_pcu_setup_backlight(pcu);
  1555. if (error)
  1556. return error;
  1557. info = &ims_pcu_device_info[pcu->device_id];
  1558. error = ims_pcu_setup_buttons(pcu, info->keymap, info->keymap_len);
  1559. if (error)
  1560. goto err_destroy_backlight;
  1561. if (info->has_gamepad) {
  1562. error = ims_pcu_setup_gamepad(pcu);
  1563. if (error)
  1564. goto err_destroy_buttons;
  1565. }
  1566. pcu->setup_complete = true;
  1567. return 0;
  1568. err_destroy_buttons:
  1569. ims_pcu_destroy_buttons(pcu);
  1570. err_destroy_backlight:
  1571. ims_pcu_destroy_backlight(pcu);
  1572. return error;
  1573. }
  1574. static void ims_pcu_destroy_application_mode(struct ims_pcu *pcu)
  1575. {
  1576. if (pcu->setup_complete) {
  1577. pcu->setup_complete = false;
  1578. mb(); /* make sure flag setting is not reordered */
  1579. if (pcu->gamepad)
  1580. ims_pcu_destroy_gamepad(pcu);
  1581. ims_pcu_destroy_buttons(pcu);
  1582. ims_pcu_destroy_backlight(pcu);
  1583. if (pcu->device_id != IMS_PCU_PCU_B_DEVICE_ID)
  1584. sysfs_remove_group(&pcu->dev->kobj,
  1585. &ims_pcu_ofn_attr_group);
  1586. }
  1587. }
  1588. static int ims_pcu_init_bootloader_mode(struct ims_pcu *pcu)
  1589. {
  1590. int error;
  1591. error = ims_pcu_execute_bl_command(pcu, QUERY_DEVICE, NULL, 0,
  1592. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  1593. if (error) {
  1594. dev_err(pcu->dev, "Bootloader does not respond, aborting\n");
  1595. return error;
  1596. }
  1597. pcu->fw_start_addr =
  1598. get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 11]);
  1599. pcu->fw_end_addr =
  1600. get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 15]);
  1601. dev_info(pcu->dev,
  1602. "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
  1603. pcu->fw_start_addr, pcu->fw_end_addr);
  1604. error = request_firmware_nowait(THIS_MODULE, true,
  1605. IMS_PCU_FIRMWARE_NAME,
  1606. pcu->dev, GFP_KERNEL, pcu,
  1607. ims_pcu_process_async_firmware);
  1608. if (error) {
  1609. /* This error is not fatal, let userspace have another chance */
  1610. complete(&pcu->async_firmware_done);
  1611. }
  1612. return 0;
  1613. }
  1614. static void ims_pcu_destroy_bootloader_mode(struct ims_pcu *pcu)
  1615. {
  1616. /* Make sure our initial firmware request has completed */
  1617. wait_for_completion(&pcu->async_firmware_done);
  1618. }
  1619. #define IMS_PCU_APPLICATION_MODE 0
  1620. #define IMS_PCU_BOOTLOADER_MODE 1
  1621. static struct usb_driver ims_pcu_driver;
  1622. static int ims_pcu_probe(struct usb_interface *intf,
  1623. const struct usb_device_id *id)
  1624. {
  1625. struct usb_device *udev = interface_to_usbdev(intf);
  1626. struct ims_pcu *pcu;
  1627. int error;
  1628. pcu = kzalloc(sizeof(struct ims_pcu), GFP_KERNEL);
  1629. if (!pcu)
  1630. return -ENOMEM;
  1631. pcu->dev = &intf->dev;
  1632. pcu->udev = udev;
  1633. pcu->bootloader_mode = id->driver_info == IMS_PCU_BOOTLOADER_MODE;
  1634. mutex_init(&pcu->cmd_mutex);
  1635. init_completion(&pcu->cmd_done);
  1636. init_completion(&pcu->async_firmware_done);
  1637. error = ims_pcu_parse_cdc_data(intf, pcu);
  1638. if (error)
  1639. goto err_free_mem;
  1640. error = usb_driver_claim_interface(&ims_pcu_driver,
  1641. pcu->data_intf, pcu);
  1642. if (error) {
  1643. dev_err(&intf->dev,
  1644. "Unable to claim corresponding data interface: %d\n",
  1645. error);
  1646. goto err_free_mem;
  1647. }
  1648. usb_set_intfdata(pcu->ctrl_intf, pcu);
  1649. usb_set_intfdata(pcu->data_intf, pcu);
  1650. error = ims_pcu_buffers_alloc(pcu);
  1651. if (error)
  1652. goto err_unclaim_intf;
  1653. error = ims_pcu_start_io(pcu);
  1654. if (error)
  1655. goto err_free_buffers;
  1656. error = ims_pcu_line_setup(pcu);
  1657. if (error)
  1658. goto err_stop_io;
  1659. error = sysfs_create_group(&intf->dev.kobj, &ims_pcu_attr_group);
  1660. if (error)
  1661. goto err_stop_io;
  1662. error = pcu->bootloader_mode ?
  1663. ims_pcu_init_bootloader_mode(pcu) :
  1664. ims_pcu_init_application_mode(pcu);
  1665. if (error)
  1666. goto err_remove_sysfs;
  1667. return 0;
  1668. err_remove_sysfs:
  1669. sysfs_remove_group(&intf->dev.kobj, &ims_pcu_attr_group);
  1670. err_stop_io:
  1671. ims_pcu_stop_io(pcu);
  1672. err_free_buffers:
  1673. ims_pcu_buffers_free(pcu);
  1674. err_unclaim_intf:
  1675. usb_driver_release_interface(&ims_pcu_driver, pcu->data_intf);
  1676. err_free_mem:
  1677. kfree(pcu);
  1678. return error;
  1679. }
  1680. static void ims_pcu_disconnect(struct usb_interface *intf)
  1681. {
  1682. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1683. struct usb_host_interface *alt = intf->cur_altsetting;
  1684. usb_set_intfdata(intf, NULL);
  1685. /*
  1686. * See if we are dealing with control or data interface. The cleanup
  1687. * happens when we unbind primary (control) interface.
  1688. */
  1689. if (alt->desc.bInterfaceClass != USB_CLASS_COMM)
  1690. return;
  1691. sysfs_remove_group(&intf->dev.kobj, &ims_pcu_attr_group);
  1692. ims_pcu_stop_io(pcu);
  1693. if (pcu->bootloader_mode)
  1694. ims_pcu_destroy_bootloader_mode(pcu);
  1695. else
  1696. ims_pcu_destroy_application_mode(pcu);
  1697. ims_pcu_buffers_free(pcu);
  1698. kfree(pcu);
  1699. }
  1700. #ifdef CONFIG_PM
  1701. static int ims_pcu_suspend(struct usb_interface *intf,
  1702. pm_message_t message)
  1703. {
  1704. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1705. struct usb_host_interface *alt = intf->cur_altsetting;
  1706. if (alt->desc.bInterfaceClass == USB_CLASS_COMM)
  1707. ims_pcu_stop_io(pcu);
  1708. return 0;
  1709. }
  1710. static int ims_pcu_resume(struct usb_interface *intf)
  1711. {
  1712. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1713. struct usb_host_interface *alt = intf->cur_altsetting;
  1714. int retval = 0;
  1715. if (alt->desc.bInterfaceClass == USB_CLASS_COMM) {
  1716. retval = ims_pcu_start_io(pcu);
  1717. if (retval == 0)
  1718. retval = ims_pcu_line_setup(pcu);
  1719. }
  1720. return retval;
  1721. }
  1722. #endif
  1723. static const struct usb_device_id ims_pcu_id_table[] = {
  1724. {
  1725. USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
  1726. USB_CLASS_COMM,
  1727. USB_CDC_SUBCLASS_ACM,
  1728. USB_CDC_ACM_PROTO_AT_V25TER),
  1729. .driver_info = IMS_PCU_APPLICATION_MODE,
  1730. },
  1731. {
  1732. USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
  1733. USB_CLASS_COMM,
  1734. USB_CDC_SUBCLASS_ACM,
  1735. USB_CDC_ACM_PROTO_AT_V25TER),
  1736. .driver_info = IMS_PCU_BOOTLOADER_MODE,
  1737. },
  1738. { }
  1739. };
  1740. static struct usb_driver ims_pcu_driver = {
  1741. .name = "ims_pcu",
  1742. .id_table = ims_pcu_id_table,
  1743. .probe = ims_pcu_probe,
  1744. .disconnect = ims_pcu_disconnect,
  1745. #ifdef CONFIG_PM
  1746. .suspend = ims_pcu_suspend,
  1747. .resume = ims_pcu_resume,
  1748. .reset_resume = ims_pcu_resume,
  1749. #endif
  1750. };
  1751. module_usb_driver(ims_pcu_driver);
  1752. MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
  1753. MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
  1754. MODULE_LICENSE("GPL");