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