ims-pcu.c 47 KB

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