| 171 | | |
|---|
| 172 | | if (bytes_to_write == 0) |
|---|
| 173 | | return nbytes; |
|---|
| 174 | | |
|---|
| 175 | | assert(d_output_short == 0); |
|---|
| 176 | | } |
|---|
| 177 | | |
|---|
| 178 | | d_curr = (d_curr+1)%d_nblocks; |
|---|
| 179 | | buf = &d_buffer[d_curr*d_block_size]; |
|---|
| 180 | | |
|---|
| 181 | | if (d_outstanding_write != d_nblocks) { |
|---|
| 182 | | d_outstanding_write++; |
|---|
| 183 | | } else { |
|---|
| 184 | | retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT); |
|---|
| 185 | | if (retval < 0) { |
|---|
| 186 | | fprintf(stderr, "%s: usb_reap_async: %s\n", |
|---|
| 187 | | __FUNCTION__, usb_strerror()); |
|---|
| 188 | | return retval; |
|---|
| 189 | | } |
|---|
| 190 | | } |
|---|
| 191 | | |
|---|
| 192 | | memcpy(buf, (void *) &(((char*)buffer)[a]), bytes_to_write); |
|---|
| 193 | | |
|---|
| 194 | | d_output_short = d_block_size - bytes_to_write; |
|---|
| 195 | | if (d_output_short == 0) |
|---|
| 196 | | usb_submit_async(d_context[d_curr], buf, d_block_size); |
|---|
| | 171 | } |
|---|
| | 172 | |
|---|
| | 173 | while (bytes_to_write > 0) { |
|---|
| | 174 | d_curr = (d_curr+1)%d_nblocks; |
|---|
| | 175 | buf = &d_buffer[d_curr*d_block_size]; |
|---|
| | 176 | |
|---|
| | 177 | if (d_outstanding_write != d_nblocks) { |
|---|
| | 178 | d_outstanding_write++; |
|---|
| | 179 | } else { |
|---|
| | 180 | retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT); |
|---|
| | 181 | if (retval < 0) { |
|---|
| | 182 | fprintf(stderr, "%s: usb_reap_async: %s\n", |
|---|
| | 183 | __FUNCTION__, usb_strerror()); |
|---|
| | 184 | return retval; |
|---|
| | 185 | } |
|---|
| | 186 | } |
|---|
| | 187 | |
|---|
| | 188 | int ncopy = std::min(bytes_to_write, d_block_size); |
|---|
| | 189 | memcpy(buf, (void *) &(((char*)buffer)[a]), ncopy); |
|---|
| | 190 | bytes_to_write -= ncopy; |
|---|
| | 191 | a += ncopy; |
|---|
| | 192 | |
|---|
| | 193 | d_output_short = d_block_size - ncopy; |
|---|
| | 194 | if (d_output_short == 0) |
|---|
| | 195 | usb_submit_async(d_context[d_curr], buf, d_block_size); |
|---|
| | 196 | } |
|---|
| 227 | | |
|---|
| 228 | | if (bytes_to_read == 0) |
|---|
| 229 | | return nbytes; |
|---|
| 230 | | |
|---|
| 231 | | assert(d_input_leftover == 0); |
|---|
| 232 | | } |
|---|
| 233 | | |
|---|
| 234 | | |
|---|
| 235 | | d_curr = (d_curr+1)%d_nblocks; |
|---|
| 236 | | buf = &d_buffer[d_curr*d_block_size]; |
|---|
| 237 | | |
|---|
| 238 | | retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT); |
|---|
| 239 | | if (retval < 0) |
|---|
| 240 | | fprintf(stderr, "%s: usb_reap_async: %s\n", |
|---|
| 241 | | __FUNCTION__, usb_strerror()); |
|---|
| 242 | | |
|---|
| 243 | | memcpy((void *) &(((char*)buffer)[a]), buf, bytes_to_read); |
|---|
| 244 | | |
|---|
| 245 | | d_input_leftover = d_block_size - bytes_to_read; |
|---|
| 246 | | if (d_input_leftover == 0) |
|---|
| 247 | | usb_submit_async(d_context[d_curr], buf, d_block_size); |
|---|
| | 227 | } |
|---|
| | 228 | |
|---|
| | 229 | while (bytes_to_read > 0) { |
|---|
| | 230 | |
|---|
| | 231 | d_curr = (d_curr+1)%d_nblocks; |
|---|
| | 232 | buf = &d_buffer[d_curr*d_block_size]; |
|---|
| | 233 | |
|---|
| | 234 | retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT); |
|---|
| | 235 | if (retval < 0) |
|---|
| | 236 | fprintf(stderr, "%s: usb_reap_async: %s\n", |
|---|
| | 237 | __FUNCTION__, usb_strerror()); |
|---|
| | 238 | |
|---|
| | 239 | int ncopy = std::min(bytes_to_read, d_block_size); |
|---|
| | 240 | memcpy((void *) &(((char*)buffer)[a]), buf, ncopy); |
|---|
| | 241 | bytes_to_read -= ncopy; |
|---|
| | 242 | a += ncopy; |
|---|
| | 243 | |
|---|
| | 244 | d_input_leftover = d_block_size - ncopy; |
|---|
| | 245 | if (d_input_leftover == 0) |
|---|
| | 246 | usb_submit_async(d_context[d_curr], buf, d_block_size); |
|---|
| | 247 | } |
|---|