ims-pcu.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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 "/*(DEBLOBBED)*/"
  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 = reject_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 = reject_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");