tools/lib/atusb-common.c |
14 | 14 | #include <stdint.h> |
15 | 15 | #include <stdlib.h> |
16 | 16 | #include <stdio.h> |
| 17 | #include <string.h> |
17 | 18 | #include <usb.h> |
18 | 19 | #include <errno.h> |
19 | 20 | |
... | ... | |
276 | 277 | } |
277 | 278 | |
278 | 279 | |
| 280 | /* ----- HardMAC ----------------------------------------------------------- */ |
| 281 | |
| 282 | |
| 283 | void atusb_rx_mode(void *handle, int on) |
| 284 | { |
| 285 | struct atusb_dsc *dsc = handle; |
| 286 | int res; |
| 287 | |
| 288 | if (dsc->error) |
| 289 | return; |
| 290 | |
| 291 | res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_RX_MODE, |
| 292 | on, 0, NULL, 0, 1000); |
| 293 | if (res < 0) { |
| 294 | fprintf(stderr, "ATUSB_RX_MODE: %d\n", res); |
| 295 | dsc->error = 1; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | int atusb_rx(void *handle, void *buf, int size, uint8_t *lqi) |
| 301 | { |
| 302 | struct atusb_dsc *dsc = handle; |
| 303 | uint8_t len; |
| 304 | int res; |
| 305 | uint8_t tmp[MAX_PSDU+2]; /* PHR, LQI */ |
| 306 | |
| 307 | /* |
| 308 | * Seems that either the USB stack of libusb doesn't like it if we do a |
| 309 | * read of size one followed by the full read. Therefore, we just do |
| 310 | * a maximum-sized read and hope that we don't split packets. |
| 311 | */ |
| 312 | res = usb_bulk_read(dsc->dev, 1, (char *) tmp, sizeof(tmp), 0); |
| 313 | if (res < 0) { |
| 314 | fprintf(stderr, "usb_bulk_read: %d\n", res); |
| 315 | dsc->error = 1; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | len = tmp[0]; |
| 320 | if (len & 0x80) { |
| 321 | fprintf(stderr, "atusb_rx: invalid length 0x%02x\n", len); |
| 322 | return 0; |
| 323 | } |
| 324 | if (len > size) { |
| 325 | fprintf(stderr, "atusb_rx: len %u > size %d\n", len, size); |
| 326 | return 0; |
| 327 | } |
| 328 | if (len > res+2) { |
| 329 | fprintf(stderr, "atusb_rx: len %u > res %d+2\n", len, res); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | memcpy(buf, tmp+1, len); |
| 334 | if (lqi) |
| 335 | *lqi = tmp[len+1]; |
| 336 | |
| 337 | return len; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | void atusb_tx(void *handle, const void *buf, int size) |
| 342 | { |
| 343 | struct atusb_dsc *dsc = handle; |
| 344 | int res; |
| 345 | |
| 346 | if (dsc->error) |
| 347 | return; |
| 348 | |
| 349 | res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_TX, |
| 350 | 0, 0, (void *) buf, size, 1000); |
| 351 | if (res < 0) { |
| 352 | fprintf(stderr, "ATUSB_TX: %d\n", res); |
| 353 | dsc->error = 1; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
279 | 358 | /* ----- Driver-specific hacks --------------------------------------------- */ |
280 | 359 | |
281 | 360 | |
tools/lib/atusb-common.h |
34 | 34 | void atusb_test_mode(void *handle); |
35 | 35 | void atusb_slp_tr(void *handle, int on, int pulse); |
36 | 36 | int atusb_interrupt_wait(void *handle, int timeout_ms); |
| 37 | void atusb_rx_mode(void *handle, int on); |
| 38 | int atusb_rx(void *handle, void *buf, int size, uint8_t *lqi); |
| 39 | void atusb_tx(void *handle, const void *buf, int size); |
37 | 40 | |
38 | 41 | int atusb_set_clkm(void *handle, int mhz); |
39 | 42 | |