Lukasz Majewski | b819ddb | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * f_dfu.c -- Device Firmware Update USB function |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com> |
| 6 | * Lukasz Majewski <l.majewski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <common.h> |
| 25 | #include <malloc.h> |
| 26 | |
| 27 | #include <linux/usb/ch9.h> |
| 28 | #include <usbdescriptors.h> |
| 29 | #include <linux/usb/gadget.h> |
| 30 | #include <linux/usb/composite.h> |
| 31 | |
| 32 | #include <dfu.h> |
| 33 | #include "f_dfu.h" |
| 34 | |
| 35 | struct f_dfu { |
| 36 | struct usb_function usb_function; |
| 37 | |
| 38 | struct usb_descriptor_header **function; |
| 39 | struct usb_string *strings; |
| 40 | |
| 41 | /* when configured, we have one config */ |
| 42 | u8 config; |
| 43 | u8 altsetting; |
| 44 | enum dfu_state dfu_state; |
| 45 | unsigned int dfu_status; |
| 46 | |
| 47 | /* Send/received block number is handy for data integrity check */ |
| 48 | int blk_seq_num; |
| 49 | }; |
| 50 | |
| 51 | typedef int (*dfu_state_fn) (struct f_dfu *, |
| 52 | const struct usb_ctrlrequest *, |
| 53 | struct usb_gadget *, |
| 54 | struct usb_request *); |
| 55 | |
| 56 | static inline struct f_dfu *func_to_dfu(struct usb_function *f) |
| 57 | { |
| 58 | return container_of(f, struct f_dfu, usb_function); |
| 59 | } |
| 60 | |
| 61 | static const struct dfu_function_descriptor dfu_func = { |
| 62 | .bLength = sizeof dfu_func, |
| 63 | .bDescriptorType = DFU_DT_FUNC, |
| 64 | .bmAttributes = DFU_BIT_WILL_DETACH | |
| 65 | DFU_BIT_MANIFESTATION_TOLERANT | |
| 66 | DFU_BIT_CAN_UPLOAD | |
| 67 | DFU_BIT_CAN_DNLOAD, |
| 68 | .wDetachTimeOut = 0, |
| 69 | .wTransferSize = DFU_USB_BUFSIZ, |
| 70 | .bcdDFUVersion = __constant_cpu_to_le16(0x0110), |
| 71 | }; |
| 72 | |
| 73 | static struct usb_interface_descriptor dfu_intf_runtime = { |
| 74 | .bLength = sizeof dfu_intf_runtime, |
| 75 | .bDescriptorType = USB_DT_INTERFACE, |
| 76 | .bNumEndpoints = 0, |
| 77 | .bInterfaceClass = USB_CLASS_APP_SPEC, |
| 78 | .bInterfaceSubClass = 1, |
| 79 | .bInterfaceProtocol = 1, |
| 80 | /* .iInterface = DYNAMIC */ |
| 81 | }; |
| 82 | |
| 83 | static struct usb_descriptor_header *dfu_runtime_descs[] = { |
| 84 | (struct usb_descriptor_header *) &dfu_intf_runtime, |
| 85 | NULL, |
| 86 | }; |
| 87 | |
| 88 | static const struct usb_qualifier_descriptor dev_qualifier = { |
| 89 | .bLength = sizeof dev_qualifier, |
| 90 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 91 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 92 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, |
| 93 | .bNumConfigurations = 1, |
| 94 | }; |
| 95 | |
| 96 | static const char dfu_name[] = "Device Firmware Upgrade"; |
| 97 | |
| 98 | /* |
| 99 | * static strings, in UTF-8 |
| 100 | * |
| 101 | * dfu_generic configuration |
| 102 | */ |
| 103 | static struct usb_string strings_dfu_generic[] = { |
| 104 | [0].s = dfu_name, |
| 105 | { } /* end of list */ |
| 106 | }; |
| 107 | |
| 108 | static struct usb_gadget_strings stringtab_dfu_generic = { |
| 109 | .language = 0x0409, /* en-us */ |
| 110 | .strings = strings_dfu_generic, |
| 111 | }; |
| 112 | |
| 113 | static struct usb_gadget_strings *dfu_generic_strings[] = { |
| 114 | &stringtab_dfu_generic, |
| 115 | NULL, |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * usb_function specific |
| 120 | */ |
| 121 | static struct usb_gadget_strings stringtab_dfu = { |
| 122 | .language = 0x0409, /* en-us */ |
| 123 | /* |
| 124 | * .strings |
| 125 | * |
| 126 | * assigned during initialization, |
| 127 | * depends on number of flash entities |
| 128 | * |
| 129 | */ |
| 130 | }; |
| 131 | |
| 132 | static struct usb_gadget_strings *dfu_strings[] = { |
| 133 | &stringtab_dfu, |
| 134 | NULL, |
| 135 | }; |
| 136 | |
| 137 | /*-------------------------------------------------------------------------*/ |
| 138 | |
| 139 | static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req) |
| 140 | { |
| 141 | struct f_dfu *f_dfu = req->context; |
| 142 | |
| 143 | dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf, |
| 144 | req->length, f_dfu->blk_seq_num); |
| 145 | |
| 146 | if (req->length == 0) |
| 147 | puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n"); |
| 148 | } |
| 149 | |
| 150 | static void handle_getstatus(struct usb_request *req) |
| 151 | { |
| 152 | struct dfu_status *dstat = (struct dfu_status *)req->buf; |
| 153 | struct f_dfu *f_dfu = req->context; |
| 154 | |
| 155 | switch (f_dfu->dfu_state) { |
| 156 | case DFU_STATE_dfuDNLOAD_SYNC: |
| 157 | case DFU_STATE_dfuDNBUSY: |
| 158 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE; |
| 159 | break; |
| 160 | case DFU_STATE_dfuMANIFEST_SYNC: |
| 161 | break; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | /* send status response */ |
| 167 | dstat->bStatus = f_dfu->dfu_status; |
| 168 | dstat->bState = f_dfu->dfu_state; |
| 169 | dstat->iString = 0; |
| 170 | } |
| 171 | |
| 172 | static void handle_getstate(struct usb_request *req) |
| 173 | { |
| 174 | struct f_dfu *f_dfu = req->context; |
| 175 | |
| 176 | ((u8 *)req->buf)[0] = f_dfu->dfu_state; |
| 177 | req->actual = sizeof(u8); |
| 178 | } |
| 179 | |
| 180 | static inline void to_dfu_mode(struct f_dfu *f_dfu) |
| 181 | { |
| 182 | f_dfu->usb_function.strings = dfu_strings; |
| 183 | f_dfu->usb_function.hs_descriptors = f_dfu->function; |
| 184 | } |
| 185 | |
| 186 | static inline void to_runtime_mode(struct f_dfu *f_dfu) |
| 187 | { |
| 188 | f_dfu->usb_function.strings = NULL; |
| 189 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
| 190 | } |
| 191 | |
| 192 | static int handle_upload(struct usb_request *req, u16 len) |
| 193 | { |
| 194 | struct f_dfu *f_dfu = req->context; |
| 195 | |
| 196 | return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf, |
| 197 | req->length, f_dfu->blk_seq_num); |
| 198 | } |
| 199 | |
| 200 | static int handle_dnload(struct usb_gadget *gadget, u16 len) |
| 201 | { |
| 202 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 203 | struct usb_request *req = cdev->req; |
| 204 | struct f_dfu *f_dfu = req->context; |
| 205 | |
| 206 | if (len == 0) |
| 207 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC; |
| 208 | |
| 209 | req->complete = dnload_request_complete; |
| 210 | |
| 211 | return len; |
| 212 | } |
| 213 | |
| 214 | /*-------------------------------------------------------------------------*/ |
| 215 | /* DFU state machine */ |
| 216 | static int state_app_idle(struct f_dfu *f_dfu, |
| 217 | const struct usb_ctrlrequest *ctrl, |
| 218 | struct usb_gadget *gadget, |
| 219 | struct usb_request *req) |
| 220 | { |
| 221 | int value = 0; |
| 222 | |
| 223 | switch (ctrl->bRequest) { |
| 224 | case USB_REQ_DFU_GETSTATUS: |
| 225 | handle_getstatus(req); |
| 226 | value = RET_STAT_LEN; |
| 227 | break; |
| 228 | case USB_REQ_DFU_GETSTATE: |
| 229 | handle_getstate(req); |
| 230 | break; |
| 231 | case USB_REQ_DFU_DETACH: |
| 232 | f_dfu->dfu_state = DFU_STATE_appDETACH; |
| 233 | to_dfu_mode(f_dfu); |
| 234 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 235 | value = RET_ZLP; |
| 236 | break; |
| 237 | default: |
| 238 | value = RET_STALL; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return value; |
| 243 | } |
| 244 | |
| 245 | static int state_app_detach(struct f_dfu *f_dfu, |
| 246 | const struct usb_ctrlrequest *ctrl, |
| 247 | struct usb_gadget *gadget, |
| 248 | struct usb_request *req) |
| 249 | { |
| 250 | int value = 0; |
| 251 | |
| 252 | switch (ctrl->bRequest) { |
| 253 | case USB_REQ_DFU_GETSTATUS: |
| 254 | handle_getstatus(req); |
| 255 | value = RET_STAT_LEN; |
| 256 | break; |
| 257 | case USB_REQ_DFU_GETSTATE: |
| 258 | handle_getstate(req); |
| 259 | break; |
| 260 | default: |
| 261 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 262 | value = RET_STALL; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | return value; |
| 267 | } |
| 268 | |
| 269 | static int state_dfu_idle(struct f_dfu *f_dfu, |
| 270 | const struct usb_ctrlrequest *ctrl, |
| 271 | struct usb_gadget *gadget, |
| 272 | struct usb_request *req) |
| 273 | { |
| 274 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 275 | u16 len = le16_to_cpu(ctrl->wLength); |
| 276 | int value = 0; |
| 277 | |
| 278 | switch (ctrl->bRequest) { |
| 279 | case USB_REQ_DFU_DNLOAD: |
| 280 | if (len == 0) { |
| 281 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 282 | value = RET_STALL; |
| 283 | break; |
| 284 | } |
| 285 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC; |
| 286 | f_dfu->blk_seq_num = w_value; |
| 287 | value = handle_dnload(gadget, len); |
| 288 | break; |
| 289 | case USB_REQ_DFU_UPLOAD: |
| 290 | f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE; |
| 291 | f_dfu->blk_seq_num = 0; |
| 292 | value = handle_upload(req, len); |
| 293 | break; |
| 294 | case USB_REQ_DFU_ABORT: |
| 295 | /* no zlp? */ |
| 296 | value = RET_ZLP; |
| 297 | break; |
| 298 | case USB_REQ_DFU_GETSTATUS: |
| 299 | handle_getstatus(req); |
| 300 | value = RET_STAT_LEN; |
| 301 | break; |
| 302 | case USB_REQ_DFU_GETSTATE: |
| 303 | handle_getstate(req); |
| 304 | break; |
| 305 | case USB_REQ_DFU_DETACH: |
| 306 | /* |
| 307 | * Proprietary extension: 'detach' from idle mode and |
| 308 | * get back to runtime mode in case of USB Reset. As |
| 309 | * much as I dislike this, we just can't use every USB |
| 310 | * bus reset to switch back to runtime mode, since at |
| 311 | * least the Linux USB stack likes to send a number of |
| 312 | * resets in a row :( |
| 313 | */ |
| 314 | f_dfu->dfu_state = |
| 315 | DFU_STATE_dfuMANIFEST_WAIT_RST; |
| 316 | to_runtime_mode(f_dfu); |
| 317 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 318 | break; |
| 319 | default: |
| 320 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 321 | value = RET_STALL; |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | return value; |
| 326 | } |
| 327 | |
| 328 | static int state_dfu_dnload_sync(struct f_dfu *f_dfu, |
| 329 | const struct usb_ctrlrequest *ctrl, |
| 330 | struct usb_gadget *gadget, |
| 331 | struct usb_request *req) |
| 332 | { |
| 333 | int value = 0; |
| 334 | |
| 335 | switch (ctrl->bRequest) { |
| 336 | case USB_REQ_DFU_GETSTATUS: |
| 337 | handle_getstatus(req); |
| 338 | value = RET_STAT_LEN; |
| 339 | break; |
| 340 | case USB_REQ_DFU_GETSTATE: |
| 341 | handle_getstate(req); |
| 342 | break; |
| 343 | default: |
| 344 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 345 | value = RET_STALL; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | return value; |
| 350 | } |
| 351 | |
| 352 | static int state_dfu_dnbusy(struct f_dfu *f_dfu, |
| 353 | const struct usb_ctrlrequest *ctrl, |
| 354 | struct usb_gadget *gadget, |
| 355 | struct usb_request *req) |
| 356 | { |
| 357 | int value = 0; |
| 358 | |
| 359 | switch (ctrl->bRequest) { |
| 360 | case USB_REQ_DFU_GETSTATUS: |
| 361 | handle_getstatus(req); |
| 362 | value = RET_STAT_LEN; |
| 363 | break; |
| 364 | default: |
| 365 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 366 | value = RET_STALL; |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | return value; |
| 371 | } |
| 372 | |
| 373 | static int state_dfu_dnload_idle(struct f_dfu *f_dfu, |
| 374 | const struct usb_ctrlrequest *ctrl, |
| 375 | struct usb_gadget *gadget, |
| 376 | struct usb_request *req) |
| 377 | { |
| 378 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 379 | u16 len = le16_to_cpu(ctrl->wLength); |
| 380 | int value = 0; |
| 381 | |
| 382 | switch (ctrl->bRequest) { |
| 383 | case USB_REQ_DFU_DNLOAD: |
| 384 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC; |
| 385 | f_dfu->blk_seq_num = w_value; |
| 386 | value = handle_dnload(gadget, len); |
| 387 | break; |
| 388 | case USB_REQ_DFU_ABORT: |
| 389 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 390 | value = RET_ZLP; |
| 391 | break; |
| 392 | case USB_REQ_DFU_GETSTATUS: |
| 393 | handle_getstatus(req); |
| 394 | value = RET_STAT_LEN; |
| 395 | break; |
| 396 | case USB_REQ_DFU_GETSTATE: |
| 397 | handle_getstate(req); |
| 398 | break; |
| 399 | default: |
| 400 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 401 | value = RET_STALL; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | return value; |
| 406 | } |
| 407 | |
| 408 | static int state_dfu_manifest_sync(struct f_dfu *f_dfu, |
| 409 | const struct usb_ctrlrequest *ctrl, |
| 410 | struct usb_gadget *gadget, |
| 411 | struct usb_request *req) |
| 412 | { |
| 413 | int value = 0; |
| 414 | |
| 415 | switch (ctrl->bRequest) { |
| 416 | case USB_REQ_DFU_GETSTATUS: |
| 417 | /* We're MainfestationTolerant */ |
| 418 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 419 | handle_getstatus(req); |
| 420 | f_dfu->blk_seq_num = 0; |
| 421 | value = RET_STAT_LEN; |
| 422 | break; |
| 423 | case USB_REQ_DFU_GETSTATE: |
| 424 | handle_getstate(req); |
| 425 | break; |
| 426 | default: |
| 427 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 428 | value = RET_STALL; |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | return value; |
| 433 | } |
| 434 | |
| 435 | static int state_dfu_upload_idle(struct f_dfu *f_dfu, |
| 436 | const struct usb_ctrlrequest *ctrl, |
| 437 | struct usb_gadget *gadget, |
| 438 | struct usb_request *req) |
| 439 | { |
| 440 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 441 | u16 len = le16_to_cpu(ctrl->wLength); |
| 442 | int value = 0; |
| 443 | |
| 444 | switch (ctrl->bRequest) { |
| 445 | case USB_REQ_DFU_UPLOAD: |
| 446 | /* state transition if less data then requested */ |
| 447 | f_dfu->blk_seq_num = w_value; |
| 448 | value = handle_upload(req, len); |
| 449 | if (value >= 0 && value < len) |
| 450 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 451 | break; |
| 452 | case USB_REQ_DFU_ABORT: |
| 453 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 454 | /* no zlp? */ |
| 455 | value = RET_ZLP; |
| 456 | break; |
| 457 | case USB_REQ_DFU_GETSTATUS: |
| 458 | handle_getstatus(req); |
| 459 | value = RET_STAT_LEN; |
| 460 | break; |
| 461 | case USB_REQ_DFU_GETSTATE: |
| 462 | handle_getstate(req); |
| 463 | break; |
| 464 | default: |
| 465 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 466 | value = RET_STALL; |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | return value; |
| 471 | } |
| 472 | |
| 473 | static int state_dfu_error(struct f_dfu *f_dfu, |
| 474 | const struct usb_ctrlrequest *ctrl, |
| 475 | struct usb_gadget *gadget, |
| 476 | struct usb_request *req) |
| 477 | { |
| 478 | int value = 0; |
| 479 | |
| 480 | switch (ctrl->bRequest) { |
| 481 | case USB_REQ_DFU_GETSTATUS: |
| 482 | handle_getstatus(req); |
| 483 | value = RET_STAT_LEN; |
| 484 | break; |
| 485 | case USB_REQ_DFU_GETSTATE: |
| 486 | handle_getstate(req); |
| 487 | break; |
| 488 | case USB_REQ_DFU_CLRSTATUS: |
| 489 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 490 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 491 | /* no zlp? */ |
| 492 | value = RET_ZLP; |
| 493 | break; |
| 494 | default: |
| 495 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 496 | value = RET_STALL; |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | return value; |
| 501 | } |
| 502 | |
| 503 | static dfu_state_fn dfu_state[] = { |
| 504 | state_app_idle, /* DFU_STATE_appIDLE */ |
| 505 | state_app_detach, /* DFU_STATE_appDETACH */ |
| 506 | state_dfu_idle, /* DFU_STATE_dfuIDLE */ |
| 507 | state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */ |
| 508 | state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */ |
| 509 | state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */ |
| 510 | state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */ |
| 511 | NULL, /* DFU_STATE_dfuMANIFEST */ |
| 512 | NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */ |
| 513 | state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */ |
| 514 | state_dfu_error /* DFU_STATE_dfuERROR */ |
| 515 | }; |
| 516 | |
| 517 | static int |
| 518 | dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 519 | { |
| 520 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 521 | struct usb_request *req = f->config->cdev->req; |
| 522 | struct f_dfu *f_dfu = f->config->cdev->req->context; |
| 523 | u16 len = le16_to_cpu(ctrl->wLength); |
| 524 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 525 | int value = 0; |
| 526 | u8 req_type = ctrl->bRequestType & USB_TYPE_MASK; |
| 527 | |
| 528 | debug("w_value: 0x%x len: 0x%x\n", w_value, len); |
| 529 | debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n", |
| 530 | req_type, ctrl->bRequest, f_dfu->dfu_state); |
| 531 | |
| 532 | if (req_type == USB_TYPE_STANDARD) { |
| 533 | if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR && |
| 534 | (w_value >> 8) == DFU_DT_FUNC) { |
| 535 | value = min(len, (u16) sizeof(dfu_func)); |
| 536 | memcpy(req->buf, &dfu_func, value); |
| 537 | } |
| 538 | } else /* DFU specific request */ |
| 539 | value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req); |
| 540 | |
| 541 | if (value >= 0) { |
| 542 | req->length = value; |
| 543 | req->zero = value < len; |
| 544 | value = usb_ep_queue(gadget->ep0, req, 0); |
| 545 | if (value < 0) { |
| 546 | debug("ep_queue --> %d\n", value); |
| 547 | req->status = 0; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return value; |
| 552 | } |
| 553 | |
| 554 | /*-------------------------------------------------------------------------*/ |
| 555 | |
| 556 | static int |
| 557 | dfu_prepare_strings(struct f_dfu *f_dfu, int n) |
| 558 | { |
| 559 | struct dfu_entity *de = NULL; |
| 560 | int i = 0; |
| 561 | |
| 562 | f_dfu->strings = calloc(sizeof(struct usb_string), n + 1); |
| 563 | if (!f_dfu->strings) |
| 564 | goto enomem; |
| 565 | |
| 566 | for (i = 0; i < n; ++i) { |
| 567 | de = dfu_get_entity(i); |
| 568 | f_dfu->strings[i].s = de->name; |
| 569 | } |
| 570 | |
| 571 | f_dfu->strings[i].id = 0; |
| 572 | f_dfu->strings[i].s = NULL; |
| 573 | |
| 574 | return 0; |
| 575 | |
| 576 | enomem: |
| 577 | while (i) |
| 578 | f_dfu->strings[--i].s = NULL; |
| 579 | |
| 580 | free(f_dfu->strings); |
| 581 | |
| 582 | return -ENOMEM; |
| 583 | } |
| 584 | |
| 585 | static int dfu_prepare_function(struct f_dfu *f_dfu, int n) |
| 586 | { |
| 587 | struct usb_interface_descriptor *d; |
| 588 | int i = 0; |
| 589 | |
| 590 | f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n); |
| 591 | if (!f_dfu->function) |
| 592 | goto enomem; |
| 593 | |
| 594 | for (i = 0; i < n; ++i) { |
| 595 | d = calloc(sizeof(*d), 1); |
| 596 | if (!d) |
| 597 | goto enomem; |
| 598 | |
| 599 | d->bLength = sizeof(*d); |
| 600 | d->bDescriptorType = USB_DT_INTERFACE; |
| 601 | d->bAlternateSetting = i; |
| 602 | d->bNumEndpoints = 0; |
| 603 | d->bInterfaceClass = USB_CLASS_APP_SPEC; |
| 604 | d->bInterfaceSubClass = 1; |
| 605 | d->bInterfaceProtocol = 2; |
| 606 | |
| 607 | f_dfu->function[i] = (struct usb_descriptor_header *)d; |
| 608 | } |
| 609 | f_dfu->function[i] = NULL; |
| 610 | |
| 611 | return 0; |
| 612 | |
| 613 | enomem: |
| 614 | while (i) { |
| 615 | free(f_dfu->function[--i]); |
| 616 | f_dfu->function[i] = NULL; |
| 617 | } |
| 618 | free(f_dfu->function); |
| 619 | |
| 620 | return -ENOMEM; |
| 621 | } |
| 622 | |
| 623 | static int dfu_bind(struct usb_configuration *c, struct usb_function *f) |
| 624 | { |
| 625 | struct usb_composite_dev *cdev = c->cdev; |
| 626 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 627 | int alt_num = dfu_get_alt_number(); |
| 628 | int rv, id, i; |
| 629 | |
| 630 | id = usb_interface_id(c, f); |
| 631 | if (id < 0) |
| 632 | return id; |
| 633 | dfu_intf_runtime.bInterfaceNumber = id; |
| 634 | |
| 635 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 636 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 637 | |
| 638 | rv = dfu_prepare_function(f_dfu, alt_num); |
| 639 | if (rv) |
| 640 | goto error; |
| 641 | |
| 642 | rv = dfu_prepare_strings(f_dfu, alt_num); |
| 643 | if (rv) |
| 644 | goto error; |
| 645 | for (i = 0; i < alt_num; i++) { |
| 646 | id = usb_string_id(cdev); |
| 647 | if (id < 0) |
| 648 | return id; |
| 649 | f_dfu->strings[i].id = id; |
| 650 | ((struct usb_interface_descriptor *)f_dfu->function[i]) |
| 651 | ->iInterface = id; |
| 652 | } |
| 653 | |
| 654 | stringtab_dfu.strings = f_dfu->strings; |
| 655 | |
| 656 | cdev->req->context = f_dfu; |
| 657 | |
| 658 | error: |
| 659 | return rv; |
| 660 | } |
| 661 | |
| 662 | static void dfu_unbind(struct usb_configuration *c, struct usb_function *f) |
| 663 | { |
| 664 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 665 | int alt_num = dfu_get_alt_number(); |
| 666 | int i; |
| 667 | |
| 668 | if (f_dfu->strings) { |
| 669 | i = alt_num; |
| 670 | while (i) |
| 671 | f_dfu->strings[--i].s = NULL; |
| 672 | |
| 673 | free(f_dfu->strings); |
| 674 | } |
| 675 | |
| 676 | if (f_dfu->function) { |
| 677 | i = alt_num; |
| 678 | while (i) { |
| 679 | free(f_dfu->function[--i]); |
| 680 | f_dfu->function[i] = NULL; |
| 681 | } |
| 682 | free(f_dfu->function); |
| 683 | } |
| 684 | |
| 685 | free(f_dfu); |
| 686 | } |
| 687 | |
| 688 | static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 689 | { |
| 690 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 691 | |
| 692 | debug("%s: intf:%d alt:%d\n", __func__, intf, alt); |
| 693 | |
| 694 | f_dfu->altsetting = alt; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* TODO: is this really what we need here? */ |
| 700 | static void dfu_disable(struct usb_function *f) |
| 701 | { |
| 702 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 703 | if (f_dfu->config == 0) |
| 704 | return; |
| 705 | |
| 706 | debug("%s: reset config\n", __func__); |
| 707 | |
| 708 | f_dfu->config = 0; |
| 709 | } |
| 710 | |
| 711 | static int dfu_bind_config(struct usb_configuration *c) |
| 712 | { |
| 713 | struct f_dfu *f_dfu; |
| 714 | int status; |
| 715 | |
| 716 | f_dfu = calloc(sizeof(*f_dfu), 1); |
| 717 | if (!f_dfu) |
| 718 | return -ENOMEM; |
| 719 | f_dfu->usb_function.name = "dfu"; |
| 720 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
| 721 | f_dfu->usb_function.bind = dfu_bind; |
| 722 | f_dfu->usb_function.unbind = dfu_unbind; |
| 723 | f_dfu->usb_function.set_alt = dfu_set_alt; |
| 724 | f_dfu->usb_function.disable = dfu_disable; |
| 725 | f_dfu->usb_function.strings = dfu_generic_strings, |
| 726 | f_dfu->usb_function.setup = dfu_handle, |
| 727 | |
| 728 | status = usb_add_function(c, &f_dfu->usb_function); |
| 729 | if (status) |
| 730 | free(f_dfu); |
| 731 | |
| 732 | return status; |
| 733 | } |
| 734 | |
| 735 | int dfu_add(struct usb_configuration *c) |
| 736 | { |
| 737 | int id; |
| 738 | |
| 739 | id = usb_string_id(c->cdev); |
| 740 | if (id < 0) |
| 741 | return id; |
| 742 | strings_dfu_generic[0].id = id; |
| 743 | dfu_intf_runtime.iInterface = id; |
| 744 | |
| 745 | debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__, |
| 746 | c->cdev, c->cdev->gadget, c->cdev->gadget->ep0); |
| 747 | |
| 748 | return dfu_bind_config(c); |
| 749 | } |