ims-pcu.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for IMS Passenger Control Unit Devices
  4. *
  5. * Copyright (C) 2013 The IMS Company
  6. */
  7. #include <linux/completion.h>
  8. #include <linux/device.h>
  9. #include <linux/firmware.h>
  10. #include <linux/ihex.h>
  11. #include <linux/input.h>
  12. #include <linux/kernel.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/usb/input.h>
  18. #include <linux/usb/cdc.h>
  19. #include <linux/unaligned.h>
  20. #define IMS_PCU_KEYMAP_LEN 32
  21. struct ims_pcu_buttons {
  22. struct input_dev *input;
  23. char name[32];
  24. char phys[32];
  25. unsigned short keymap[IMS_PCU_KEYMAP_LEN];
  26. };
  27. struct ims_pcu_gamepad {
  28. struct input_dev *input;
  29. char name[32];
  30. char phys[32];
  31. };
  32. struct ims_pcu_backlight {
  33. struct led_classdev cdev;
  34. char name[32];
  35. };
  36. #define IMS_PCU_PART_NUMBER_LEN 15
  37. #define IMS_PCU_SERIAL_NUMBER_LEN 8
  38. #define IMS_PCU_DOM_LEN 8
  39. #define IMS_PCU_FW_VERSION_LEN 16
  40. #define IMS_PCU_BL_VERSION_LEN 16
  41. #define IMS_PCU_BL_RESET_REASON_LEN (2 + 1)
  42. #define IMS_PCU_PCU_B_DEVICE_ID 5
  43. #define IMS_PCU_BUF_SIZE 128
  44. struct ims_pcu {
  45. struct usb_device *udev;
  46. struct device *dev; /* control interface's device, used for logging */
  47. unsigned int device_no;
  48. bool bootloader_mode;
  49. char part_number[IMS_PCU_PART_NUMBER_LEN];
  50. char serial_number[IMS_PCU_SERIAL_NUMBER_LEN];
  51. char date_of_manufacturing[IMS_PCU_DOM_LEN];
  52. char fw_version[IMS_PCU_FW_VERSION_LEN];
  53. char bl_version[IMS_PCU_BL_VERSION_LEN];
  54. char reset_reason[IMS_PCU_BL_RESET_REASON_LEN];
  55. int update_firmware_status;
  56. u8 device_id;
  57. u8 ofn_reg_addr;
  58. struct usb_interface *ctrl_intf;
  59. struct usb_endpoint_descriptor *ep_ctrl;
  60. struct urb *urb_ctrl;
  61. u8 *urb_ctrl_buf;
  62. dma_addr_t ctrl_dma;
  63. size_t max_ctrl_size;
  64. struct usb_interface *data_intf;
  65. struct usb_endpoint_descriptor *ep_in;
  66. struct urb *urb_in;
  67. u8 *urb_in_buf;
  68. dma_addr_t read_dma;
  69. size_t max_in_size;
  70. struct usb_endpoint_descriptor *ep_out;
  71. u8 *urb_out_buf;
  72. size_t max_out_size;
  73. u8 read_buf[IMS_PCU_BUF_SIZE];
  74. u8 read_pos;
  75. u8 check_sum;
  76. bool have_stx;
  77. bool have_dle;
  78. u8 cmd_buf[IMS_PCU_BUF_SIZE];
  79. u8 ack_id;
  80. u8 expected_response;
  81. u8 cmd_buf_len;
  82. struct completion cmd_done;
  83. struct mutex cmd_mutex;
  84. u32 fw_start_addr;
  85. u32 fw_end_addr;
  86. struct completion async_firmware_done;
  87. struct ims_pcu_buttons buttons;
  88. struct ims_pcu_gamepad *gamepad;
  89. struct ims_pcu_backlight backlight;
  90. bool setup_complete; /* Input and LED devices have been created */
  91. };
  92. /*********************************************************************
  93. * Buttons Input device support *
  94. *********************************************************************/
  95. static const unsigned short ims_pcu_keymap_1[] = {
  96. [1] = KEY_ATTENDANT_OFF,
  97. [2] = KEY_ATTENDANT_ON,
  98. [3] = KEY_LIGHTS_TOGGLE,
  99. [4] = KEY_VOLUMEUP,
  100. [5] = KEY_VOLUMEDOWN,
  101. [6] = KEY_INFO,
  102. };
  103. static const unsigned short ims_pcu_keymap_2[] = {
  104. [4] = KEY_VOLUMEUP,
  105. [5] = KEY_VOLUMEDOWN,
  106. [6] = KEY_INFO,
  107. };
  108. static const unsigned short ims_pcu_keymap_3[] = {
  109. [1] = KEY_HOMEPAGE,
  110. [2] = KEY_ATTENDANT_TOGGLE,
  111. [3] = KEY_LIGHTS_TOGGLE,
  112. [4] = KEY_VOLUMEUP,
  113. [5] = KEY_VOLUMEDOWN,
  114. [6] = KEY_DISPLAYTOGGLE,
  115. [18] = KEY_PLAYPAUSE,
  116. };
  117. static const unsigned short ims_pcu_keymap_4[] = {
  118. [1] = KEY_ATTENDANT_OFF,
  119. [2] = KEY_ATTENDANT_ON,
  120. [3] = KEY_LIGHTS_TOGGLE,
  121. [4] = KEY_VOLUMEUP,
  122. [5] = KEY_VOLUMEDOWN,
  123. [6] = KEY_INFO,
  124. [18] = KEY_PLAYPAUSE,
  125. };
  126. static const unsigned short ims_pcu_keymap_5[] = {
  127. [1] = KEY_ATTENDANT_OFF,
  128. [2] = KEY_ATTENDANT_ON,
  129. [3] = KEY_LIGHTS_TOGGLE,
  130. };
  131. struct ims_pcu_device_info {
  132. const unsigned short *keymap;
  133. size_t keymap_len;
  134. bool has_gamepad;
  135. };
  136. #define IMS_PCU_DEVINFO(_n, _gamepad) \
  137. [_n] = { \
  138. .keymap = ims_pcu_keymap_##_n, \
  139. .keymap_len = ARRAY_SIZE(ims_pcu_keymap_##_n), \
  140. .has_gamepad = _gamepad, \
  141. }
  142. static const struct ims_pcu_device_info ims_pcu_device_info[] = {
  143. IMS_PCU_DEVINFO(1, true),
  144. IMS_PCU_DEVINFO(2, true),
  145. IMS_PCU_DEVINFO(3, true),
  146. IMS_PCU_DEVINFO(4, true),
  147. IMS_PCU_DEVINFO(5, false),
  148. };
  149. static void ims_pcu_buttons_report(struct ims_pcu *pcu, u32 data)
  150. {
  151. struct ims_pcu_buttons *buttons = &pcu->buttons;
  152. struct input_dev *input = buttons->input;
  153. int i;
  154. for (i = 0; i < 32; i++) {
  155. unsigned short keycode = buttons->keymap[i];
  156. if (keycode != KEY_RESERVED)
  157. input_report_key(input, keycode, data & (1UL << i));
  158. }
  159. input_sync(input);
  160. }
  161. static int ims_pcu_setup_buttons(struct ims_pcu *pcu,
  162. const unsigned short *keymap,
  163. size_t keymap_len)
  164. {
  165. struct ims_pcu_buttons *buttons = &pcu->buttons;
  166. struct input_dev *input;
  167. int i;
  168. int error;
  169. input = input_allocate_device();
  170. if (!input) {
  171. dev_err(pcu->dev, "Not enough memory for input device\n");
  172. return -ENOMEM;
  173. }
  174. snprintf(buttons->name, sizeof(buttons->name),
  175. "IMS PCU#%d Button Interface", pcu->device_no);
  176. usb_make_path(pcu->udev, buttons->phys, sizeof(buttons->phys));
  177. strlcat(buttons->phys, "/input0", sizeof(buttons->phys));
  178. memcpy(buttons->keymap, keymap, sizeof(*keymap) * keymap_len);
  179. input->name = buttons->name;
  180. input->phys = buttons->phys;
  181. usb_to_input_id(pcu->udev, &input->id);
  182. input->dev.parent = &pcu->ctrl_intf->dev;
  183. input->keycode = buttons->keymap;
  184. input->keycodemax = ARRAY_SIZE(buttons->keymap);
  185. input->keycodesize = sizeof(buttons->keymap[0]);
  186. __set_bit(EV_KEY, input->evbit);
  187. for (i = 0; i < IMS_PCU_KEYMAP_LEN; i++)
  188. __set_bit(buttons->keymap[i], input->keybit);
  189. __clear_bit(KEY_RESERVED, input->keybit);
  190. error = input_register_device(input);
  191. if (error) {
  192. dev_err(pcu->dev,
  193. "Failed to register buttons input device: %d\n",
  194. error);
  195. input_free_device(input);
  196. return error;
  197. }
  198. buttons->input = input;
  199. return 0;
  200. }
  201. static void ims_pcu_destroy_buttons(struct ims_pcu *pcu)
  202. {
  203. struct ims_pcu_buttons *buttons = &pcu->buttons;
  204. input_unregister_device(buttons->input);
  205. }
  206. /*********************************************************************
  207. * Gamepad Input device support *
  208. *********************************************************************/
  209. static void ims_pcu_gamepad_report(struct ims_pcu *pcu, u32 data)
  210. {
  211. struct ims_pcu_gamepad *gamepad = pcu->gamepad;
  212. struct input_dev *input = gamepad->input;
  213. int x, y;
  214. x = !!(data & (1 << 14)) - !!(data & (1 << 13));
  215. y = !!(data & (1 << 12)) - !!(data & (1 << 11));
  216. input_report_abs(input, ABS_X, x);
  217. input_report_abs(input, ABS_Y, y);
  218. input_report_key(input, BTN_A, data & (1 << 7));
  219. input_report_key(input, BTN_B, data & (1 << 8));
  220. input_report_key(input, BTN_X, data & (1 << 9));
  221. input_report_key(input, BTN_Y, data & (1 << 10));
  222. input_report_key(input, BTN_START, data & (1 << 15));
  223. input_report_key(input, BTN_SELECT, data & (1 << 16));
  224. input_sync(input);
  225. }
  226. static int ims_pcu_setup_gamepad(struct ims_pcu *pcu)
  227. {
  228. struct ims_pcu_gamepad *gamepad;
  229. struct input_dev *input;
  230. int error;
  231. gamepad = kzalloc(sizeof(*gamepad), GFP_KERNEL);
  232. input = input_allocate_device();
  233. if (!gamepad || !input) {
  234. dev_err(pcu->dev,
  235. "Not enough memory for gamepad device\n");
  236. error = -ENOMEM;
  237. goto err_free_mem;
  238. }
  239. gamepad->input = input;
  240. snprintf(gamepad->name, sizeof(gamepad->name),
  241. "IMS PCU#%d Gamepad Interface", pcu->device_no);
  242. usb_make_path(pcu->udev, gamepad->phys, sizeof(gamepad->phys));
  243. strlcat(gamepad->phys, "/input1", sizeof(gamepad->phys));
  244. input->name = gamepad->name;
  245. input->phys = gamepad->phys;
  246. usb_to_input_id(pcu->udev, &input->id);
  247. input->dev.parent = &pcu->ctrl_intf->dev;
  248. __set_bit(EV_KEY, input->evbit);
  249. __set_bit(BTN_A, input->keybit);
  250. __set_bit(BTN_B, input->keybit);
  251. __set_bit(BTN_X, input->keybit);
  252. __set_bit(BTN_Y, input->keybit);
  253. __set_bit(BTN_START, input->keybit);
  254. __set_bit(BTN_SELECT, input->keybit);
  255. __set_bit(EV_ABS, input->evbit);
  256. input_set_abs_params(input, ABS_X, -1, 1, 0, 0);
  257. input_set_abs_params(input, ABS_Y, -1, 1, 0, 0);
  258. error = input_register_device(input);
  259. if (error) {
  260. dev_err(pcu->dev,
  261. "Failed to register gamepad input device: %d\n",
  262. error);
  263. goto err_free_mem;
  264. }
  265. pcu->gamepad = gamepad;
  266. return 0;
  267. err_free_mem:
  268. input_free_device(input);
  269. kfree(gamepad);
  270. return error;
  271. }
  272. static void ims_pcu_destroy_gamepad(struct ims_pcu *pcu)
  273. {
  274. struct ims_pcu_gamepad *gamepad = pcu->gamepad;
  275. input_unregister_device(gamepad->input);
  276. kfree(gamepad);
  277. }
  278. /*********************************************************************
  279. * PCU Communication protocol handling *
  280. *********************************************************************/
  281. #define IMS_PCU_PROTOCOL_STX 0x02
  282. #define IMS_PCU_PROTOCOL_ETX 0x03
  283. #define IMS_PCU_PROTOCOL_DLE 0x10
  284. /* PCU commands */
  285. #define IMS_PCU_CMD_STATUS 0xa0
  286. #define IMS_PCU_CMD_PCU_RESET 0xa1
  287. #define IMS_PCU_CMD_RESET_REASON 0xa2
  288. #define IMS_PCU_CMD_SEND_BUTTONS 0xa3
  289. #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4
  290. #define IMS_PCU_CMD_GET_INFO 0xa5
  291. #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6
  292. #define IMS_PCU_CMD_EEPROM 0xa7
  293. #define IMS_PCU_CMD_GET_FW_VERSION 0xa8
  294. #define IMS_PCU_CMD_GET_BL_VERSION 0xa9
  295. #define IMS_PCU_CMD_SET_INFO 0xab
  296. #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac
  297. #define IMS_PCU_CMD_GET_DEVICE_ID 0xae
  298. #define IMS_PCU_CMD_SPECIAL_INFO 0xb0
  299. #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */
  300. #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3
  301. #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4
  302. /* PCU responses */
  303. #define IMS_PCU_RSP_STATUS 0xc0
  304. #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
  305. #define IMS_PCU_RSP_RESET_REASON 0xc2
  306. #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
  307. #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
  308. #define IMS_PCU_RSP_GET_INFO 0xc5
  309. #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
  310. #define IMS_PCU_RSP_EEPROM 0xc7
  311. #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
  312. #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
  313. #define IMS_PCU_RSP_SET_INFO 0xcb
  314. #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
  315. #define IMS_PCU_RSP_CMD_INVALID 0xcd
  316. #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
  317. #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
  318. #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
  319. #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2
  320. #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3
  321. #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
  322. #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
  323. #define IMS_PCU_MIN_PACKET_LEN 3
  324. #define IMS_PCU_DATA_OFFSET 2
  325. #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
  326. #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
  327. static void ims_pcu_report_events(struct ims_pcu *pcu)
  328. {
  329. u32 data = get_unaligned_be32(&pcu->read_buf[3]);
  330. ims_pcu_buttons_report(pcu, data & ~IMS_PCU_GAMEPAD_MASK);
  331. if (pcu->gamepad)
  332. ims_pcu_gamepad_report(pcu, data);
  333. }
  334. static void ims_pcu_handle_response(struct ims_pcu *pcu)
  335. {
  336. switch (pcu->read_buf[0]) {
  337. case IMS_PCU_RSP_EVNT_BUTTONS:
  338. if (likely(pcu->setup_complete))
  339. ims_pcu_report_events(pcu);
  340. break;
  341. default:
  342. /*
  343. * See if we got command completion.
  344. * If both the sequence and response code match save
  345. * the data and signal completion.
  346. */
  347. if (pcu->read_buf[0] == pcu->expected_response &&
  348. pcu->read_buf[1] == pcu->ack_id - 1) {
  349. memcpy(pcu->cmd_buf, pcu->read_buf, pcu->read_pos);
  350. pcu->cmd_buf_len = pcu->read_pos;
  351. complete(&pcu->cmd_done);
  352. }
  353. break;
  354. }
  355. }
  356. static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb)
  357. {
  358. int i;
  359. for (i = 0; i < urb->actual_length; i++) {
  360. u8 data = pcu->urb_in_buf[i];
  361. /* Skip everything until we get Start Xmit */
  362. if (!pcu->have_stx && data != IMS_PCU_PROTOCOL_STX)
  363. continue;
  364. if (pcu->have_dle) {
  365. pcu->have_dle = false;
  366. pcu->read_buf[pcu->read_pos++] = data;
  367. pcu->check_sum += data;
  368. continue;
  369. }
  370. switch (data) {
  371. case IMS_PCU_PROTOCOL_STX:
  372. if (pcu->have_stx)
  373. dev_warn(pcu->dev,
  374. "Unexpected STX at byte %d, discarding old data\n",
  375. pcu->read_pos);
  376. pcu->have_stx = true;
  377. pcu->have_dle = false;
  378. pcu->read_pos = 0;
  379. pcu->check_sum = 0;
  380. break;
  381. case IMS_PCU_PROTOCOL_DLE:
  382. pcu->have_dle = true;
  383. break;
  384. case IMS_PCU_PROTOCOL_ETX:
  385. if (pcu->read_pos < IMS_PCU_MIN_PACKET_LEN) {
  386. dev_warn(pcu->dev,
  387. "Short packet received (%d bytes), ignoring\n",
  388. pcu->read_pos);
  389. } else if (pcu->check_sum != 0) {
  390. dev_warn(pcu->dev,
  391. "Invalid checksum in packet (%d bytes), ignoring\n",
  392. pcu->read_pos);
  393. } else {
  394. ims_pcu_handle_response(pcu);
  395. }
  396. pcu->have_stx = false;
  397. pcu->have_dle = false;
  398. pcu->read_pos = 0;
  399. break;
  400. default:
  401. pcu->read_buf[pcu->read_pos++] = data;
  402. pcu->check_sum += data;
  403. break;
  404. }
  405. }
  406. }
  407. static bool ims_pcu_byte_needs_escape(u8 byte)
  408. {
  409. return byte == IMS_PCU_PROTOCOL_STX ||
  410. byte == IMS_PCU_PROTOCOL_ETX ||
  411. byte == IMS_PCU_PROTOCOL_DLE;
  412. }
  413. static int ims_pcu_send_cmd_chunk(struct ims_pcu *pcu,
  414. u8 command, int chunk, int len)
  415. {
  416. int error;
  417. error = usb_bulk_msg(pcu->udev,
  418. usb_sndbulkpipe(pcu->udev,
  419. pcu->ep_out->bEndpointAddress),
  420. pcu->urb_out_buf, len,
  421. NULL, IMS_PCU_CMD_WRITE_TIMEOUT);
  422. if (error < 0) {
  423. dev_dbg(pcu->dev,
  424. "Sending 0x%02x command failed at chunk %d: %d\n",
  425. command, chunk, error);
  426. return error;
  427. }
  428. return 0;
  429. }
  430. static int ims_pcu_send_command(struct ims_pcu *pcu,
  431. u8 command, const u8 *data, int len)
  432. {
  433. int count = 0;
  434. int chunk = 0;
  435. int delta;
  436. int i;
  437. int error;
  438. u8 csum = 0;
  439. u8 ack_id;
  440. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_STX;
  441. /* We know the command need not be escaped */
  442. pcu->urb_out_buf[count++] = command;
  443. csum += command;
  444. ack_id = pcu->ack_id++;
  445. if (ack_id == 0xff)
  446. ack_id = pcu->ack_id++;
  447. if (ims_pcu_byte_needs_escape(ack_id))
  448. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  449. pcu->urb_out_buf[count++] = ack_id;
  450. csum += ack_id;
  451. for (i = 0; i < len; i++) {
  452. delta = ims_pcu_byte_needs_escape(data[i]) ? 2 : 1;
  453. if (count + delta >= pcu->max_out_size) {
  454. error = ims_pcu_send_cmd_chunk(pcu, command,
  455. ++chunk, count);
  456. if (error)
  457. return error;
  458. count = 0;
  459. }
  460. if (delta == 2)
  461. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  462. pcu->urb_out_buf[count++] = data[i];
  463. csum += data[i];
  464. }
  465. csum = 1 + ~csum;
  466. delta = ims_pcu_byte_needs_escape(csum) ? 3 : 2;
  467. if (count + delta >= pcu->max_out_size) {
  468. error = ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
  469. if (error)
  470. return error;
  471. count = 0;
  472. }
  473. if (delta == 3)
  474. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
  475. pcu->urb_out_buf[count++] = csum;
  476. pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_ETX;
  477. return ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
  478. }
  479. static int __ims_pcu_execute_command(struct ims_pcu *pcu,
  480. u8 command, const void *data, size_t len,
  481. u8 expected_response, int response_time)
  482. {
  483. int error;
  484. pcu->expected_response = expected_response;
  485. init_completion(&pcu->cmd_done);
  486. error = ims_pcu_send_command(pcu, command, data, len);
  487. if (error)
  488. return error;
  489. if (expected_response &&
  490. !wait_for_completion_timeout(&pcu->cmd_done,
  491. msecs_to_jiffies(response_time))) {
  492. dev_dbg(pcu->dev, "Command 0x%02x timed out\n", command);
  493. return -ETIMEDOUT;
  494. }
  495. return 0;
  496. }
  497. #define ims_pcu_execute_command(pcu, code, data, len) \
  498. __ims_pcu_execute_command(pcu, \
  499. IMS_PCU_CMD_##code, data, len, \
  500. IMS_PCU_RSP_##code, \
  501. IMS_PCU_CMD_RESPONSE_TIMEOUT)
  502. #define ims_pcu_execute_query(pcu, code) \
  503. ims_pcu_execute_command(pcu, code, NULL, 0)
  504. /* Bootloader commands */
  505. #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
  506. #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
  507. #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
  508. #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
  509. #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
  510. #define IMS_PCU_BL_CMD_READ_APP 0xa6
  511. #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
  512. #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
  513. /* Bootloader commands */
  514. #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
  515. #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
  516. #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
  517. #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
  518. #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
  519. #define IMS_PCU_BL_RSP_READ_APP 0xc6
  520. #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
  521. #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
  522. #define IMS_PCU_BL_DATA_OFFSET 3
  523. static int __ims_pcu_execute_bl_command(struct ims_pcu *pcu,
  524. u8 command, const void *data, size_t len,
  525. u8 expected_response, int response_time)
  526. {
  527. int error;
  528. pcu->cmd_buf[0] = command;
  529. if (data)
  530. memcpy(&pcu->cmd_buf[1], data, len);
  531. error = __ims_pcu_execute_command(pcu,
  532. IMS_PCU_CMD_BOOTLOADER, pcu->cmd_buf, len + 1,
  533. expected_response ? IMS_PCU_RSP_BOOTLOADER : 0,
  534. response_time);
  535. if (error) {
  536. dev_err(pcu->dev,
  537. "Failure when sending 0x%02x command to bootloader, error: %d\n",
  538. pcu->cmd_buf[0], error);
  539. return error;
  540. }
  541. if (expected_response && pcu->cmd_buf[2] != expected_response) {
  542. dev_err(pcu->dev,
  543. "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
  544. pcu->cmd_buf[2], expected_response);
  545. return -EINVAL;
  546. }
  547. return 0;
  548. }
  549. #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
  550. __ims_pcu_execute_bl_command(pcu, \
  551. IMS_PCU_BL_CMD_##code, data, len, \
  552. IMS_PCU_BL_RSP_##code, timeout) \
  553. #define IMS_PCU_INFO_PART_OFFSET 2
  554. #define IMS_PCU_INFO_DOM_OFFSET 17
  555. #define IMS_PCU_INFO_SERIAL_OFFSET 25
  556. #define IMS_PCU_SET_INFO_SIZE 31
  557. static int ims_pcu_get_info(struct ims_pcu *pcu)
  558. {
  559. int error;
  560. error = ims_pcu_execute_query(pcu, GET_INFO);
  561. if (error) {
  562. dev_err(pcu->dev,
  563. "GET_INFO command failed, error: %d\n", error);
  564. return error;
  565. }
  566. memcpy(pcu->part_number,
  567. &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
  568. sizeof(pcu->part_number));
  569. memcpy(pcu->date_of_manufacturing,
  570. &pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
  571. sizeof(pcu->date_of_manufacturing));
  572. memcpy(pcu->serial_number,
  573. &pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
  574. sizeof(pcu->serial_number));
  575. return 0;
  576. }
  577. static int ims_pcu_set_info(struct ims_pcu *pcu)
  578. {
  579. int error;
  580. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
  581. pcu->part_number, sizeof(pcu->part_number));
  582. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
  583. pcu->date_of_manufacturing, sizeof(pcu->date_of_manufacturing));
  584. memcpy(&pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
  585. pcu->serial_number, sizeof(pcu->serial_number));
  586. error = ims_pcu_execute_command(pcu, SET_INFO,
  587. &pcu->cmd_buf[IMS_PCU_DATA_OFFSET],
  588. IMS_PCU_SET_INFO_SIZE);
  589. if (error) {
  590. dev_err(pcu->dev,
  591. "Failed to update device information, error: %d\n",
  592. error);
  593. return error;
  594. }
  595. return 0;
  596. }
  597. static int ims_pcu_switch_to_bootloader(struct ims_pcu *pcu)
  598. {
  599. int error;
  600. /* Execute jump to the bootoloader */
  601. error = ims_pcu_execute_command(pcu, JUMP_TO_BTLDR, NULL, 0);
  602. if (error) {
  603. dev_err(pcu->dev,
  604. "Failure when sending JUMP TO BOOTLOADER command, error: %d\n",
  605. error);
  606. return error;
  607. }
  608. return 0;
  609. }
  610. /*********************************************************************
  611. * Firmware Update handling *
  612. *********************************************************************/
  613. #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
  614. struct ims_pcu_flash_fmt {
  615. __le32 addr;
  616. u8 len;
  617. u8 data[] __counted_by(len);
  618. };
  619. static unsigned int ims_pcu_count_fw_records(const struct firmware *fw)
  620. {
  621. const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
  622. unsigned int count = 0;
  623. while (rec) {
  624. count++;
  625. rec = ihex_next_binrec(rec);
  626. }
  627. return count;
  628. }
  629. static int ims_pcu_verify_block(struct ims_pcu *pcu,
  630. u32 addr, u8 len, const u8 *data)
  631. {
  632. struct ims_pcu_flash_fmt *fragment;
  633. int error;
  634. fragment = (void *)&pcu->cmd_buf[1];
  635. put_unaligned_le32(addr, &fragment->addr);
  636. fragment->len = len;
  637. error = ims_pcu_execute_bl_command(pcu, READ_APP, NULL, 5,
  638. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  639. if (error) {
  640. dev_err(pcu->dev,
  641. "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
  642. addr, len, error);
  643. return error;
  644. }
  645. fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET];
  646. if (get_unaligned_le32(&fragment->addr) != addr ||
  647. fragment->len != len) {
  648. dev_err(pcu->dev,
  649. "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
  650. addr, get_unaligned_le32(&fragment->addr),
  651. len, fragment->len);
  652. return -EINVAL;
  653. }
  654. if (memcmp(fragment->data, data, len)) {
  655. dev_err(pcu->dev,
  656. "Mismatch in block at 0x%08x, len %d\n",
  657. addr, len);
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static int ims_pcu_flash_firmware(struct ims_pcu *pcu,
  663. const struct firmware *fw,
  664. unsigned int n_fw_records)
  665. {
  666. const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
  667. struct ims_pcu_flash_fmt *fragment;
  668. unsigned int count = 0;
  669. u32 addr;
  670. u8 len;
  671. int error;
  672. error = ims_pcu_execute_bl_command(pcu, ERASE_APP, NULL, 0, 2000);
  673. if (error) {
  674. dev_err(pcu->dev,
  675. "Failed to erase application image, error: %d\n",
  676. error);
  677. return error;
  678. }
  679. while (rec) {
  680. /*
  681. * The firmware format is messed up for some reason.
  682. * The address twice that of what is needed for some
  683. * reason and we end up overwriting half of the data
  684. * with the next record.
  685. */
  686. addr = be32_to_cpu(rec->addr) / 2;
  687. len = be16_to_cpu(rec->len);
  688. if (len > sizeof(pcu->cmd_buf) - 1 - sizeof(*fragment)) {
  689. dev_err(pcu->dev,
  690. "Invalid record length in firmware: %d\n", len);
  691. return -EINVAL;
  692. }
  693. fragment = (void *)&pcu->cmd_buf[1];
  694. put_unaligned_le32(addr, &fragment->addr);
  695. fragment->len = len;
  696. memcpy(fragment->data, rec->data, len);
  697. error = ims_pcu_execute_bl_command(pcu, PROGRAM_DEVICE,
  698. NULL, len + 5,
  699. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  700. if (error) {
  701. dev_err(pcu->dev,
  702. "Failed to write block at 0x%08x, len %d, error: %d\n",
  703. addr, len, error);
  704. return error;
  705. }
  706. if (addr >= pcu->fw_start_addr && addr < pcu->fw_end_addr) {
  707. error = ims_pcu_verify_block(pcu, addr, len, rec->data);
  708. if (error)
  709. return error;
  710. }
  711. count++;
  712. pcu->update_firmware_status = (count * 100) / n_fw_records;
  713. rec = ihex_next_binrec(rec);
  714. }
  715. error = ims_pcu_execute_bl_command(pcu, PROGRAM_COMPLETE,
  716. NULL, 0, 2000);
  717. if (error)
  718. dev_err(pcu->dev,
  719. "Failed to send PROGRAM_COMPLETE, error: %d\n",
  720. error);
  721. return 0;
  722. }
  723. static int ims_pcu_handle_firmware_update(struct ims_pcu *pcu,
  724. const struct firmware *fw)
  725. {
  726. unsigned int n_fw_records;
  727. int retval;
  728. dev_info(pcu->dev, "Updating firmware %s, size: %zu\n",
  729. IMS_PCU_FIRMWARE_NAME, fw->size);
  730. n_fw_records = ims_pcu_count_fw_records(fw);
  731. retval = ims_pcu_flash_firmware(pcu, fw, n_fw_records);
  732. if (retval)
  733. goto out;
  734. retval = ims_pcu_execute_bl_command(pcu, LAUNCH_APP, NULL, 0, 0);
  735. if (retval)
  736. dev_err(pcu->dev,
  737. "Failed to start application image, error: %d\n",
  738. retval);
  739. out:
  740. pcu->update_firmware_status = retval;
  741. sysfs_notify(&pcu->dev->kobj, NULL, "update_firmware_status");
  742. return retval;
  743. }
  744. static void ims_pcu_process_async_firmware(const struct firmware *fw,
  745. void *context)
  746. {
  747. struct ims_pcu *pcu = context;
  748. int error;
  749. if (!fw) {
  750. dev_err(pcu->dev, "Failed to get firmware %s\n",
  751. IMS_PCU_FIRMWARE_NAME);
  752. goto out;
  753. }
  754. error = ihex_validate_fw(fw);
  755. if (error) {
  756. dev_err(pcu->dev, "Firmware %s is invalid\n",
  757. IMS_PCU_FIRMWARE_NAME);
  758. goto out;
  759. }
  760. scoped_guard(mutex, &pcu->cmd_mutex)
  761. ims_pcu_handle_firmware_update(pcu, fw);
  762. release_firmware(fw);
  763. out:
  764. complete(&pcu->async_firmware_done);
  765. }
  766. /*********************************************************************
  767. * Backlight LED device support *
  768. *********************************************************************/
  769. #define IMS_PCU_MAX_BRIGHTNESS 31998
  770. static int ims_pcu_backlight_set_brightness(struct led_classdev *cdev,
  771. enum led_brightness value)
  772. {
  773. struct ims_pcu_backlight *backlight =
  774. container_of(cdev, struct ims_pcu_backlight, cdev);
  775. struct ims_pcu *pcu =
  776. container_of(backlight, struct ims_pcu, backlight);
  777. __le16 br_val = cpu_to_le16(value);
  778. int error;
  779. guard(mutex)(&pcu->cmd_mutex);
  780. error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS,
  781. &br_val, sizeof(br_val));
  782. if (error && error != -ENODEV)
  783. dev_warn(pcu->dev,
  784. "Failed to set desired brightness %u, error: %d\n",
  785. value, error);
  786. return error;
  787. }
  788. static enum led_brightness
  789. ims_pcu_backlight_get_brightness(struct led_classdev *cdev)
  790. {
  791. struct ims_pcu_backlight *backlight =
  792. container_of(cdev, struct ims_pcu_backlight, cdev);
  793. struct ims_pcu *pcu =
  794. container_of(backlight, struct ims_pcu, backlight);
  795. int brightness;
  796. int error;
  797. guard(mutex)(&pcu->cmd_mutex);
  798. error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS);
  799. if (error) {
  800. dev_warn(pcu->dev,
  801. "Failed to get current brightness, error: %d\n",
  802. error);
  803. /* Assume the LED is OFF */
  804. brightness = LED_OFF;
  805. } else {
  806. brightness =
  807. get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
  808. }
  809. return brightness;
  810. }
  811. static int ims_pcu_setup_backlight(struct ims_pcu *pcu)
  812. {
  813. struct ims_pcu_backlight *backlight = &pcu->backlight;
  814. int error;
  815. snprintf(backlight->name, sizeof(backlight->name),
  816. "pcu%d::kbd_backlight", pcu->device_no);
  817. backlight->cdev.name = backlight->name;
  818. backlight->cdev.max_brightness = IMS_PCU_MAX_BRIGHTNESS;
  819. backlight->cdev.brightness_get = ims_pcu_backlight_get_brightness;
  820. backlight->cdev.brightness_set_blocking =
  821. ims_pcu_backlight_set_brightness;
  822. error = led_classdev_register(pcu->dev, &backlight->cdev);
  823. if (error) {
  824. dev_err(pcu->dev,
  825. "Failed to register backlight LED device, error: %d\n",
  826. error);
  827. return error;
  828. }
  829. return 0;
  830. }
  831. static void ims_pcu_destroy_backlight(struct ims_pcu *pcu)
  832. {
  833. struct ims_pcu_backlight *backlight = &pcu->backlight;
  834. led_classdev_unregister(&backlight->cdev);
  835. }
  836. /*********************************************************************
  837. * Sysfs attributes handling *
  838. *********************************************************************/
  839. struct ims_pcu_attribute {
  840. struct device_attribute dattr;
  841. size_t field_offset;
  842. int field_length;
  843. };
  844. static ssize_t ims_pcu_attribute_show(struct device *dev,
  845. struct device_attribute *dattr,
  846. char *buf)
  847. {
  848. struct usb_interface *intf = to_usb_interface(dev);
  849. struct ims_pcu *pcu = usb_get_intfdata(intf);
  850. struct ims_pcu_attribute *attr =
  851. container_of(dattr, struct ims_pcu_attribute, dattr);
  852. char *field = (char *)pcu + attr->field_offset;
  853. return sysfs_emit(buf, "%.*s\n", attr->field_length, field);
  854. }
  855. static ssize_t ims_pcu_attribute_store(struct device *dev,
  856. struct device_attribute *dattr,
  857. const char *buf, size_t count)
  858. {
  859. struct usb_interface *intf = to_usb_interface(dev);
  860. struct ims_pcu *pcu = usb_get_intfdata(intf);
  861. struct ims_pcu_attribute *attr =
  862. container_of(dattr, struct ims_pcu_attribute, dattr);
  863. char *field = (char *)pcu + attr->field_offset;
  864. size_t data_len;
  865. int error;
  866. if (count > attr->field_length)
  867. return -EINVAL;
  868. data_len = strnlen(buf, attr->field_length);
  869. if (data_len > attr->field_length)
  870. return -EINVAL;
  871. scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) {
  872. memset(field, 0, attr->field_length);
  873. memcpy(field, buf, data_len);
  874. error = ims_pcu_set_info(pcu);
  875. /*
  876. * Even if update failed, let's fetch the info again as we just
  877. * clobbered one of the fields.
  878. */
  879. ims_pcu_get_info(pcu);
  880. if (error)
  881. return error;
  882. }
  883. return count;
  884. }
  885. #define IMS_PCU_ATTR(_field, _mode) \
  886. struct ims_pcu_attribute ims_pcu_attr_##_field = { \
  887. .dattr = __ATTR(_field, _mode, \
  888. ims_pcu_attribute_show, \
  889. ims_pcu_attribute_store), \
  890. .field_offset = offsetof(struct ims_pcu, _field), \
  891. .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
  892. }
  893. #define IMS_PCU_RO_ATTR(_field) \
  894. IMS_PCU_ATTR(_field, S_IRUGO)
  895. #define IMS_PCU_RW_ATTR(_field) \
  896. IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
  897. static IMS_PCU_RW_ATTR(part_number);
  898. static IMS_PCU_RW_ATTR(serial_number);
  899. static IMS_PCU_RW_ATTR(date_of_manufacturing);
  900. static IMS_PCU_RO_ATTR(fw_version);
  901. static IMS_PCU_RO_ATTR(bl_version);
  902. static IMS_PCU_RO_ATTR(reset_reason);
  903. static ssize_t ims_pcu_reset_device(struct device *dev,
  904. struct device_attribute *dattr,
  905. const char *buf, size_t count)
  906. {
  907. static const u8 reset_byte = 1;
  908. struct usb_interface *intf = to_usb_interface(dev);
  909. struct ims_pcu *pcu = usb_get_intfdata(intf);
  910. int value;
  911. int error;
  912. error = kstrtoint(buf, 0, &value);
  913. if (error)
  914. return error;
  915. if (value != 1)
  916. return -EINVAL;
  917. dev_info(pcu->dev, "Attempting to reset device\n");
  918. error = ims_pcu_execute_command(pcu, PCU_RESET, &reset_byte, 1);
  919. if (error) {
  920. dev_info(pcu->dev,
  921. "Failed to reset device, error: %d\n",
  922. error);
  923. return error;
  924. }
  925. return count;
  926. }
  927. static DEVICE_ATTR(reset_device, S_IWUSR, NULL, ims_pcu_reset_device);
  928. static ssize_t ims_pcu_update_firmware_store(struct device *dev,
  929. struct device_attribute *dattr,
  930. const char *buf, size_t count)
  931. {
  932. struct usb_interface *intf = to_usb_interface(dev);
  933. struct ims_pcu *pcu = usb_get_intfdata(intf);
  934. int value;
  935. int error;
  936. error = kstrtoint(buf, 0, &value);
  937. if (error)
  938. return error;
  939. if (value != 1)
  940. return -EINVAL;
  941. const struct firmware *fw __free(firmware) = NULL;
  942. error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev);
  943. if (error) {
  944. dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n",
  945. IMS_PCU_FIRMWARE_NAME, error);
  946. return error;
  947. }
  948. scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) {
  949. /*
  950. * If we are already in bootloader mode we can proceed with
  951. * flashing the firmware.
  952. *
  953. * If we are in application mode, then we need to switch into
  954. * bootloader mode, which will cause the device to disconnect
  955. * and reconnect as different device.
  956. */
  957. if (pcu->bootloader_mode)
  958. error = ims_pcu_handle_firmware_update(pcu, fw);
  959. else
  960. error = ims_pcu_switch_to_bootloader(pcu);
  961. if (error)
  962. return error;
  963. }
  964. return count;
  965. }
  966. static DEVICE_ATTR(update_firmware, S_IWUSR,
  967. NULL, ims_pcu_update_firmware_store);
  968. static ssize_t
  969. ims_pcu_update_firmware_status_show(struct device *dev,
  970. struct device_attribute *dattr,
  971. char *buf)
  972. {
  973. struct usb_interface *intf = to_usb_interface(dev);
  974. struct ims_pcu *pcu = usb_get_intfdata(intf);
  975. return sysfs_emit(buf, "%d\n", pcu->update_firmware_status);
  976. }
  977. static DEVICE_ATTR(update_firmware_status, S_IRUGO,
  978. ims_pcu_update_firmware_status_show, NULL);
  979. static struct attribute *ims_pcu_attrs[] = {
  980. &ims_pcu_attr_part_number.dattr.attr,
  981. &ims_pcu_attr_serial_number.dattr.attr,
  982. &ims_pcu_attr_date_of_manufacturing.dattr.attr,
  983. &ims_pcu_attr_fw_version.dattr.attr,
  984. &ims_pcu_attr_bl_version.dattr.attr,
  985. &ims_pcu_attr_reset_reason.dattr.attr,
  986. &dev_attr_reset_device.attr,
  987. &dev_attr_update_firmware.attr,
  988. &dev_attr_update_firmware_status.attr,
  989. NULL
  990. };
  991. static umode_t ims_pcu_is_attr_visible(struct kobject *kobj,
  992. struct attribute *attr, int n)
  993. {
  994. struct device *dev = kobj_to_dev(kobj);
  995. struct usb_interface *intf = to_usb_interface(dev);
  996. struct ims_pcu *pcu = usb_get_intfdata(intf);
  997. umode_t mode = attr->mode;
  998. if (pcu->bootloader_mode) {
  999. if (attr != &dev_attr_update_firmware_status.attr &&
  1000. attr != &dev_attr_update_firmware.attr &&
  1001. attr != &dev_attr_reset_device.attr) {
  1002. mode = 0;
  1003. }
  1004. } else {
  1005. if (attr == &dev_attr_update_firmware_status.attr)
  1006. mode = 0;
  1007. }
  1008. return mode;
  1009. }
  1010. static const struct attribute_group ims_pcu_attr_group = {
  1011. .is_visible = ims_pcu_is_attr_visible,
  1012. .attrs = ims_pcu_attrs,
  1013. };
  1014. /* Support for a separate OFN attribute group */
  1015. #define OFN_REG_RESULT_OFFSET 2
  1016. static int ims_pcu_read_ofn_config(struct ims_pcu *pcu, u8 addr, u8 *data)
  1017. {
  1018. int error;
  1019. s16 result;
  1020. error = ims_pcu_execute_command(pcu, OFN_GET_CONFIG,
  1021. &addr, sizeof(addr));
  1022. if (error)
  1023. return error;
  1024. result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
  1025. if (result < 0)
  1026. return -EIO;
  1027. /* We only need LSB */
  1028. *data = pcu->cmd_buf[OFN_REG_RESULT_OFFSET];
  1029. return 0;
  1030. }
  1031. static int ims_pcu_write_ofn_config(struct ims_pcu *pcu, u8 addr, u8 data)
  1032. {
  1033. u8 buffer[] = { addr, data };
  1034. int error;
  1035. s16 result;
  1036. error = ims_pcu_execute_command(pcu, OFN_SET_CONFIG,
  1037. &buffer, sizeof(buffer));
  1038. if (error)
  1039. return error;
  1040. result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
  1041. if (result < 0)
  1042. return -EIO;
  1043. return 0;
  1044. }
  1045. static ssize_t ims_pcu_ofn_reg_data_show(struct device *dev,
  1046. struct device_attribute *dattr,
  1047. char *buf)
  1048. {
  1049. struct usb_interface *intf = to_usb_interface(dev);
  1050. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1051. int error;
  1052. u8 data;
  1053. scoped_guard(mutex, &pcu->cmd_mutex) {
  1054. error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data);
  1055. if (error)
  1056. return error;
  1057. }
  1058. return sysfs_emit(buf, "%x\n", data);
  1059. }
  1060. static ssize_t ims_pcu_ofn_reg_data_store(struct device *dev,
  1061. struct device_attribute *dattr,
  1062. const char *buf, size_t count)
  1063. {
  1064. struct usb_interface *intf = to_usb_interface(dev);
  1065. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1066. int error;
  1067. u8 value;
  1068. error = kstrtou8(buf, 0, &value);
  1069. if (error)
  1070. return error;
  1071. guard(mutex)(&pcu->cmd_mutex);
  1072. error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value);
  1073. if (error)
  1074. return error;
  1075. return count;
  1076. }
  1077. static DEVICE_ATTR(reg_data, S_IRUGO | S_IWUSR,
  1078. ims_pcu_ofn_reg_data_show, ims_pcu_ofn_reg_data_store);
  1079. static ssize_t ims_pcu_ofn_reg_addr_show(struct device *dev,
  1080. struct device_attribute *dattr,
  1081. char *buf)
  1082. {
  1083. struct usb_interface *intf = to_usb_interface(dev);
  1084. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1085. guard(mutex)(&pcu->cmd_mutex);
  1086. return sysfs_emit(buf, "%x\n", pcu->ofn_reg_addr);
  1087. }
  1088. static ssize_t ims_pcu_ofn_reg_addr_store(struct device *dev,
  1089. struct device_attribute *dattr,
  1090. const char *buf, size_t count)
  1091. {
  1092. struct usb_interface *intf = to_usb_interface(dev);
  1093. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1094. int error;
  1095. u8 value;
  1096. error = kstrtou8(buf, 0, &value);
  1097. if (error)
  1098. return error;
  1099. guard(mutex)(&pcu->cmd_mutex);
  1100. pcu->ofn_reg_addr = value;
  1101. return count;
  1102. }
  1103. static DEVICE_ATTR(reg_addr, S_IRUGO | S_IWUSR,
  1104. ims_pcu_ofn_reg_addr_show, ims_pcu_ofn_reg_addr_store);
  1105. struct ims_pcu_ofn_bit_attribute {
  1106. struct device_attribute dattr;
  1107. u8 addr;
  1108. u8 nr;
  1109. };
  1110. static ssize_t ims_pcu_ofn_bit_show(struct device *dev,
  1111. struct device_attribute *dattr,
  1112. char *buf)
  1113. {
  1114. struct usb_interface *intf = to_usb_interface(dev);
  1115. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1116. struct ims_pcu_ofn_bit_attribute *attr =
  1117. container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr);
  1118. int error;
  1119. u8 data;
  1120. scoped_guard(mutex, &pcu->cmd_mutex) {
  1121. error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
  1122. if (error)
  1123. return error;
  1124. }
  1125. return sysfs_emit(buf, "%d\n", !!(data & (1 << attr->nr)));
  1126. }
  1127. static ssize_t ims_pcu_ofn_bit_store(struct device *dev,
  1128. struct device_attribute *dattr,
  1129. const char *buf, size_t count)
  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. int value;
  1137. u8 data;
  1138. error = kstrtoint(buf, 0, &value);
  1139. if (error)
  1140. return error;
  1141. if (value > 1)
  1142. return -EINVAL;
  1143. scoped_guard(mutex, &pcu->cmd_mutex) {
  1144. error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
  1145. if (error)
  1146. return error;
  1147. if (value)
  1148. data |= 1U << attr->nr;
  1149. else
  1150. data &= ~(1U << attr->nr);
  1151. error = ims_pcu_write_ofn_config(pcu, attr->addr, data);
  1152. if (error)
  1153. return error;
  1154. }
  1155. return count;
  1156. }
  1157. #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \
  1158. struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \
  1159. .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \
  1160. ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \
  1161. .addr = _addr, \
  1162. .nr = _nr, \
  1163. }
  1164. static IMS_PCU_OFN_BIT_ATTR(engine_enable, 0x60, 7);
  1165. static IMS_PCU_OFN_BIT_ATTR(speed_enable, 0x60, 6);
  1166. static IMS_PCU_OFN_BIT_ATTR(assert_enable, 0x60, 5);
  1167. static IMS_PCU_OFN_BIT_ATTR(xyquant_enable, 0x60, 4);
  1168. static IMS_PCU_OFN_BIT_ATTR(xyscale_enable, 0x60, 1);
  1169. static IMS_PCU_OFN_BIT_ATTR(scale_x2, 0x63, 6);
  1170. static IMS_PCU_OFN_BIT_ATTR(scale_y2, 0x63, 7);
  1171. static struct attribute *ims_pcu_ofn_attrs[] = {
  1172. &dev_attr_reg_data.attr,
  1173. &dev_attr_reg_addr.attr,
  1174. &ims_pcu_ofn_attr_engine_enable.dattr.attr,
  1175. &ims_pcu_ofn_attr_speed_enable.dattr.attr,
  1176. &ims_pcu_ofn_attr_assert_enable.dattr.attr,
  1177. &ims_pcu_ofn_attr_xyquant_enable.dattr.attr,
  1178. &ims_pcu_ofn_attr_xyscale_enable.dattr.attr,
  1179. &ims_pcu_ofn_attr_scale_x2.dattr.attr,
  1180. &ims_pcu_ofn_attr_scale_y2.dattr.attr,
  1181. NULL
  1182. };
  1183. static umode_t ims_pcu_ofn_is_attr_visible(struct kobject *kobj,
  1184. struct attribute *attr, int n)
  1185. {
  1186. struct device *dev = kobj_to_dev(kobj);
  1187. struct usb_interface *intf = to_usb_interface(dev);
  1188. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1189. umode_t mode = attr->mode;
  1190. /*
  1191. * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor.
  1192. */
  1193. if (pcu->bootloader_mode || pcu->device_id == IMS_PCU_PCU_B_DEVICE_ID)
  1194. mode = 0;
  1195. return mode;
  1196. }
  1197. static const struct attribute_group ims_pcu_ofn_attr_group = {
  1198. .name = "ofn",
  1199. .is_visible = ims_pcu_ofn_is_attr_visible,
  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 too 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. error = ims_pcu_setup_backlight(pcu);
  1546. if (error)
  1547. return error;
  1548. info = &ims_pcu_device_info[pcu->device_id];
  1549. error = ims_pcu_setup_buttons(pcu, info->keymap, info->keymap_len);
  1550. if (error)
  1551. goto err_destroy_backlight;
  1552. if (info->has_gamepad) {
  1553. error = ims_pcu_setup_gamepad(pcu);
  1554. if (error)
  1555. goto err_destroy_buttons;
  1556. }
  1557. pcu->setup_complete = true;
  1558. return 0;
  1559. err_destroy_buttons:
  1560. ims_pcu_destroy_buttons(pcu);
  1561. err_destroy_backlight:
  1562. ims_pcu_destroy_backlight(pcu);
  1563. return error;
  1564. }
  1565. static void ims_pcu_destroy_application_mode(struct ims_pcu *pcu)
  1566. {
  1567. if (pcu->setup_complete) {
  1568. pcu->setup_complete = false;
  1569. mb(); /* make sure flag setting is not reordered */
  1570. if (pcu->gamepad)
  1571. ims_pcu_destroy_gamepad(pcu);
  1572. ims_pcu_destroy_buttons(pcu);
  1573. ims_pcu_destroy_backlight(pcu);
  1574. }
  1575. }
  1576. static int ims_pcu_init_bootloader_mode(struct ims_pcu *pcu)
  1577. {
  1578. int error;
  1579. error = ims_pcu_execute_bl_command(pcu, QUERY_DEVICE, NULL, 0,
  1580. IMS_PCU_CMD_RESPONSE_TIMEOUT);
  1581. if (error) {
  1582. dev_err(pcu->dev, "Bootloader does not respond, aborting\n");
  1583. return error;
  1584. }
  1585. pcu->fw_start_addr =
  1586. get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 11]);
  1587. pcu->fw_end_addr =
  1588. get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 15]);
  1589. dev_info(pcu->dev,
  1590. "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
  1591. pcu->fw_start_addr, pcu->fw_end_addr);
  1592. error = request_firmware_nowait(THIS_MODULE, true,
  1593. IMS_PCU_FIRMWARE_NAME,
  1594. pcu->dev, GFP_KERNEL, pcu,
  1595. ims_pcu_process_async_firmware);
  1596. if (error) {
  1597. /* This error is not fatal, let userspace have another chance */
  1598. complete(&pcu->async_firmware_done);
  1599. }
  1600. return 0;
  1601. }
  1602. static void ims_pcu_destroy_bootloader_mode(struct ims_pcu *pcu)
  1603. {
  1604. /* Make sure our initial firmware request has completed */
  1605. wait_for_completion(&pcu->async_firmware_done);
  1606. }
  1607. #define IMS_PCU_APPLICATION_MODE 0
  1608. #define IMS_PCU_BOOTLOADER_MODE 1
  1609. static struct usb_driver ims_pcu_driver;
  1610. static int ims_pcu_probe(struct usb_interface *intf,
  1611. const struct usb_device_id *id)
  1612. {
  1613. struct usb_device *udev = interface_to_usbdev(intf);
  1614. struct ims_pcu *pcu;
  1615. int error;
  1616. pcu = kzalloc(sizeof(*pcu), GFP_KERNEL);
  1617. if (!pcu)
  1618. return -ENOMEM;
  1619. pcu->dev = &intf->dev;
  1620. pcu->udev = udev;
  1621. pcu->bootloader_mode = id->driver_info == IMS_PCU_BOOTLOADER_MODE;
  1622. mutex_init(&pcu->cmd_mutex);
  1623. init_completion(&pcu->cmd_done);
  1624. init_completion(&pcu->async_firmware_done);
  1625. error = ims_pcu_parse_cdc_data(intf, pcu);
  1626. if (error)
  1627. goto err_free_mem;
  1628. error = usb_driver_claim_interface(&ims_pcu_driver,
  1629. pcu->data_intf, pcu);
  1630. if (error) {
  1631. dev_err(&intf->dev,
  1632. "Unable to claim corresponding data interface: %d\n",
  1633. error);
  1634. goto err_free_mem;
  1635. }
  1636. usb_set_intfdata(pcu->ctrl_intf, pcu);
  1637. error = ims_pcu_buffers_alloc(pcu);
  1638. if (error)
  1639. goto err_unclaim_intf;
  1640. error = ims_pcu_start_io(pcu);
  1641. if (error)
  1642. goto err_free_buffers;
  1643. error = ims_pcu_line_setup(pcu);
  1644. if (error)
  1645. goto err_stop_io;
  1646. error = pcu->bootloader_mode ?
  1647. ims_pcu_init_bootloader_mode(pcu) :
  1648. ims_pcu_init_application_mode(pcu);
  1649. if (error)
  1650. goto err_stop_io;
  1651. return 0;
  1652. err_stop_io:
  1653. ims_pcu_stop_io(pcu);
  1654. err_free_buffers:
  1655. ims_pcu_buffers_free(pcu);
  1656. err_unclaim_intf:
  1657. usb_driver_release_interface(&ims_pcu_driver, pcu->data_intf);
  1658. err_free_mem:
  1659. kfree(pcu);
  1660. return error;
  1661. }
  1662. static void ims_pcu_disconnect(struct usb_interface *intf)
  1663. {
  1664. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1665. struct usb_host_interface *alt = intf->cur_altsetting;
  1666. usb_set_intfdata(intf, NULL);
  1667. /*
  1668. * See if we are dealing with control or data interface. The cleanup
  1669. * happens when we unbind primary (control) interface.
  1670. */
  1671. if (alt->desc.bInterfaceClass != USB_CLASS_COMM)
  1672. return;
  1673. ims_pcu_stop_io(pcu);
  1674. if (pcu->bootloader_mode)
  1675. ims_pcu_destroy_bootloader_mode(pcu);
  1676. else
  1677. ims_pcu_destroy_application_mode(pcu);
  1678. ims_pcu_buffers_free(pcu);
  1679. kfree(pcu);
  1680. }
  1681. #ifdef CONFIG_PM
  1682. static int ims_pcu_suspend(struct usb_interface *intf,
  1683. pm_message_t message)
  1684. {
  1685. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1686. struct usb_host_interface *alt = intf->cur_altsetting;
  1687. if (alt->desc.bInterfaceClass == USB_CLASS_COMM)
  1688. ims_pcu_stop_io(pcu);
  1689. return 0;
  1690. }
  1691. static int ims_pcu_resume(struct usb_interface *intf)
  1692. {
  1693. struct ims_pcu *pcu = usb_get_intfdata(intf);
  1694. struct usb_host_interface *alt = intf->cur_altsetting;
  1695. int retval = 0;
  1696. if (alt->desc.bInterfaceClass == USB_CLASS_COMM) {
  1697. retval = ims_pcu_start_io(pcu);
  1698. if (retval == 0)
  1699. retval = ims_pcu_line_setup(pcu);
  1700. }
  1701. return retval;
  1702. }
  1703. #endif
  1704. static const struct usb_device_id ims_pcu_id_table[] = {
  1705. {
  1706. USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
  1707. USB_CLASS_COMM,
  1708. USB_CDC_SUBCLASS_ACM,
  1709. USB_CDC_ACM_PROTO_AT_V25TER),
  1710. .driver_info = IMS_PCU_APPLICATION_MODE,
  1711. },
  1712. {
  1713. USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
  1714. USB_CLASS_COMM,
  1715. USB_CDC_SUBCLASS_ACM,
  1716. USB_CDC_ACM_PROTO_AT_V25TER),
  1717. .driver_info = IMS_PCU_BOOTLOADER_MODE,
  1718. },
  1719. { }
  1720. };
  1721. static const struct attribute_group *ims_pcu_sysfs_groups[] = {
  1722. &ims_pcu_attr_group,
  1723. &ims_pcu_ofn_attr_group,
  1724. NULL
  1725. };
  1726. static struct usb_driver ims_pcu_driver = {
  1727. .name = "ims_pcu",
  1728. .id_table = ims_pcu_id_table,
  1729. .dev_groups = ims_pcu_sysfs_groups,
  1730. .probe = ims_pcu_probe,
  1731. .disconnect = ims_pcu_disconnect,
  1732. #ifdef CONFIG_PM
  1733. .suspend = ims_pcu_suspend,
  1734. .resume = ims_pcu_resume,
  1735. .reset_resume = ims_pcu_resume,
  1736. #endif
  1737. };
  1738. module_usb_driver(ims_pcu_driver);
  1739. MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
  1740. MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
  1741. MODULE_LICENSE("GPL");