ims-pcu.c 47 KB

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