2010-09-06 00:59:42 +03:00
|
|
|
/*
|
2010-11-11 13:41:25 +02:00
|
|
|
* lib/atusb.c - ATUSB access functions library (USB version)
|
2010-09-06 00:59:42 +03:00
|
|
|
*
|
2011-02-11 04:11:34 +02:00
|
|
|
* Written 2010-2011 by Werner Almesberger
|
|
|
|
* Copyright 2010-2011 Werner Almesberger
|
2010-09-06 00:59:42 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <usb.h>
|
|
|
|
|
2010-11-11 13:26:01 +02:00
|
|
|
#include "atusb/ep0.h"
|
|
|
|
#include "atusb/usb-ids.h"
|
2010-09-06 00:59:42 +03:00
|
|
|
|
2011-04-10 13:43:42 +03:00
|
|
|
#include "usbopen.h"
|
2010-09-06 00:59:42 +03:00
|
|
|
#include "driver.h"
|
|
|
|
|
|
|
|
|
2010-11-11 13:41:25 +02:00
|
|
|
#define FROM_DEV ATUSB_FROM_DEV(0)
|
|
|
|
#define TO_DEV ATUSB_TO_DEV(0)
|
2010-09-06 00:59:42 +03:00
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc {
|
|
|
|
usb_dev_handle *dev;
|
|
|
|
int error;
|
|
|
|
};
|
2010-09-06 00:59:42 +03:00
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
/* ----- error handling ---------------------------------------------------- */
|
2010-09-06 00:59:42 +03:00
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static int atusb_error(void *handle)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
|
|
|
|
|
|
|
return dsc->error;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static int atusb_clear_error(void *handle)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int ret;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
ret = dsc->error;
|
|
|
|
dsc->error = 0;
|
2010-09-06 00:59:42 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- open/close -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-04-10 03:00:48 +03:00
|
|
|
static void *atusb_open(const char *arg)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
|
|
|
usb_dev_handle *dev;
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc;
|
2010-09-06 00:59:42 +03:00
|
|
|
|
2011-04-10 16:55:49 +03:00
|
|
|
usb_unrestrict();
|
tools/lib: atusb can now be selected by bus/device or by bus-port path
Examples:
usb:1/6 select logical device 6 on bus 1
usb:1-3.1.4 starting at bus 1, choose ports 3, 1, and 4
- usbopen.c (initialize, open_usb): moved libusb initialization to
separate function, to allow for sharing
- usbopen.h (restrict_usb_path), usbopen.c (restricted_path, open_usb,
restrict_usb_dev, restrict_usb_by_dev, read_num, restrict_usb_by_port,
restrict_usb_path): added mechanism to restrict USB path
- atusb.c (atusb_open): if an argument is given, call restrict_usb_path
with it
2011-04-10 15:51:30 +03:00
|
|
|
if (arg)
|
|
|
|
restrict_usb_path(arg);
|
2010-09-06 00:59:42 +03:00
|
|
|
dev = open_usb(USB_VENDOR, USB_PRODUCT);
|
2011-04-12 15:22:40 +03:00
|
|
|
if (!dev) {
|
2010-09-06 00:59:42 +03:00
|
|
|
fprintf(stderr, ":-(\n");
|
2011-04-12 15:22:40 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc = malloc(sizeof(*dsc));
|
|
|
|
if (!dsc) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
2011-04-12 15:22:40 +03:00
|
|
|
|
|
|
|
dsc->dev = dev;
|
|
|
|
dsc->error = 0;
|
|
|
|
|
|
|
|
return dsc;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_close(void *handle)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
|
|
|
/* to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- device mode ------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_reset(void *handle)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
res =
|
2011-04-12 15:22:40 +03:00
|
|
|
usb_control_msg(dsc->dev, TO_DEV, ATUSB_RESET, 0, 0, NULL, 0, 1000);
|
2010-09-06 00:59:42 +03:00
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_RESET: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_reset_rf(void *handle)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_RF_RESET, 0, 0, NULL,
|
|
|
|
0, 1000);
|
2010-09-06 00:59:42 +03:00
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_RF_RESET: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_test_mode(void *handle)
|
2010-11-11 07:29:07 +02:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-11-11 07:29:07 +02:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-11-11 07:29:07 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
res =
|
2011-04-12 15:22:40 +03:00
|
|
|
usb_control_msg(dsc->dev, TO_DEV, ATUSB_TEST, 0, 0, NULL, 0, 1000);
|
2010-11-11 07:29:07 +02:00
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_TEST: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-11-11 07:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-06 00:59:42 +03:00
|
|
|
/* ----- register access --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_reg_write(void *handle, uint8_t reg, uint8_t value)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_REG_WRITE, value, reg,
|
2010-09-06 00:59:42 +03:00
|
|
|
NULL, 0, 1000);
|
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_REG_WRITE: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static uint8_t atusb_reg_read(void *handle, uint8_t reg)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
uint8_t value = 0;
|
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return 0;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_REG_READ, 0, reg,
|
2010-09-06 00:59:42 +03:00
|
|
|
(void *) &value, 1, 1000);
|
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_REG_READ: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- frame buffer access ----------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static void atusb_buf_write(void *handle, const void *buf, int size)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_BUF_WRITE, 0, 0,
|
2010-09-06 00:59:42 +03:00
|
|
|
(void *) buf, size, 1000);
|
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_BUF_WRITE: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static int atusb_buf_read(void *handle, void *buf, int size)
|
2010-09-06 00:59:42 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-06 00:59:42 +03:00
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-06 00:59:42 +03:00
|
|
|
return -1;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_BUF_READ, 0, 0,
|
2010-09-06 00:59:42 +03:00
|
|
|
buf, size, 1000);
|
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_BUF_READ: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2010-09-06 00:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-10 02:22:45 +03:00
|
|
|
/* ----- RF interrupt ------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static int atusb_interrupt(void *handle)
|
2010-09-10 02:22:45 +03:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2010-09-10 02:22:45 +03:00
|
|
|
uint8_t buf;
|
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2010-09-10 02:22:45 +03:00
|
|
|
return -1;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_POLL_INT, 0, 0,
|
2010-09-10 02:22:45 +03:00
|
|
|
(void *) &buf, 1, 1000);
|
|
|
|
if (res < 0) {
|
2010-11-11 13:41:25 +02:00
|
|
|
fprintf(stderr, "ATUSB_POLL_INT: %d\n", res);
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2011-04-11 01:54:42 +03:00
|
|
|
return -1;
|
2010-09-10 02:22:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-11 04:11:34 +02:00
|
|
|
/* ----- CLKM handling ----------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ATmega32U2-based boards don't allow disabling CLKM, so we keep it at 8 MHz.
|
|
|
|
* We could accommodate a choice between 8 MHz and 16 MHz, but that's for
|
|
|
|
* later.
|
|
|
|
*/
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
static int atusb_set_clkm(void *handle, int mhz)
|
2011-02-11 04:11:34 +02:00
|
|
|
{
|
2011-04-12 15:22:40 +03:00
|
|
|
struct atusb_dsc *dsc = handle;
|
2011-02-11 04:11:34 +02:00
|
|
|
uint8_t ids[3];
|
|
|
|
int res;
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
if (dsc->error)
|
2011-02-11 04:11:34 +02:00
|
|
|
return 0;
|
2011-04-12 15:22:40 +03:00
|
|
|
res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_ID, 0, 0,
|
2011-02-11 04:11:34 +02:00
|
|
|
(void *) ids, 3, 1000);
|
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
|
2011-04-12 15:22:40 +03:00
|
|
|
dsc->error = 1;
|
2011-02-11 04:11:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
switch (ids[2]) {
|
|
|
|
case HW_TYPE_100813:
|
|
|
|
case HW_TYPE_101216:
|
|
|
|
break;
|
|
|
|
case HW_TYPE_110131:
|
|
|
|
if (mhz == 0 || mhz == 8)
|
|
|
|
return 1;
|
|
|
|
fprintf(stderr, "this board only supports CLKM = 8 MHz\n");
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
|
|
|
"atusb_set_clkm: unknown hardware type 0x%02x\n", ids[2]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return atrf_set_clkm_generic(atusb_reg_write, dsc, mhz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 15:22:40 +03:00
|
|
|
/* ----- Driver-specific hacks --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
void *atusb_dev_handle(void *handle)
|
|
|
|
{
|
|
|
|
struct atusb_dsc *dsc = handle;
|
|
|
|
|
|
|
|
return dsc->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-06 00:59:42 +03:00
|
|
|
/* ----- driver interface -------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-11-11 14:04:36 +02:00
|
|
|
struct atrf_driver atusb_driver = {
|
2011-04-10 03:18:31 +03:00
|
|
|
.name = "usb",
|
2010-09-06 00:59:42 +03:00
|
|
|
.open = atusb_open,
|
|
|
|
.close = atusb_close,
|
Merged uSD driver into unified tool build, completed conversion of tools.
- atusd/tools/lib/atusd.c: moved to tools/lib/
- atusd/tools/lib/atusd.h: removed, we can now use tools/include/atspi.h
- tools/lib/atusd.c: added copyright header
- tools/lib/atusd.c: updated for driver API
- tools/lib/Makefile: added atusd.o
- tools/Makefile.common: moved common elements from all other makefiles in
tools/*/ here
- tools/Makefile.common: added target-specific compiler and flags
- tools/atspi-id/Makefile, tools/atspi-reset/Makefile,
tools/atspi-rssi/Makefile, tools/atspi-trim/Makefile,
tools/atspi-txrx/Makefile, tools/lib/Makefile: used Makefile.common
- tools/lib/Makefile: differentiate USB and uSD build
- tools/atspi-id/atspi-id.c, tools/atspi-reset/atspi-reset.c,
tools/atspi-rssi/atspi-rssi.c, tools/atspi-trim/atspi-trim.c,
tools/atspi-txr/atspi-txr.c: updated for driver-agnostic API
- tools/atspi-id/atspi-id.c, tools/atspi-reset/atspi-reset.c,
tools/atspi-rssi/atspi-rssi.c, tools/atspi-trim/atspi-trim.c,
tools/atspi-txr/atspi-txr.c: corrected AF86RF230 typo in title
- tools/include/atspi.h, tools/lib/atspi.c, tools/lib/driver.c,
tools/lib/atusb.c: brought back support for atspi_error and
atspi_clear_error
- tools/atspi-id/atspi-id.c (atspi_get_protocol): renamed to get_protocol,
to make it clear that it's not from libatspi
- tools/atspi-id/atspi-id.c (atspi_get_build): renamed to get_build, to
make it clear that it's not from libatspi
- tools/include/atspi.h, tools/lib/atspi.c (atspi_usb_handle): new function
to obtain a driver's USB device handle (or NULL if the driver doesn't use
USB)
2010-09-06 02:32:58 +03:00
|
|
|
.error = atusb_error,
|
|
|
|
.clear_error = atusb_clear_error,
|
2010-09-06 00:59:42 +03:00
|
|
|
.reset = atusb_reset,
|
|
|
|
.reset_rf = atusb_reset_rf,
|
2010-11-11 07:29:07 +02:00
|
|
|
.test_mode = atusb_test_mode,
|
2011-04-10 03:00:48 +03:00
|
|
|
.slp_tr = NULL, /* @@@ not yet */
|
2011-02-11 04:11:34 +02:00
|
|
|
.set_clkm = atusb_set_clkm,
|
2010-09-06 00:59:42 +03:00
|
|
|
.reg_write = atusb_reg_write,
|
|
|
|
.reg_read = atusb_reg_read,
|
|
|
|
.buf_write = atusb_buf_write,
|
|
|
|
.buf_read = atusb_buf_read,
|
2010-11-11 15:34:09 +02:00
|
|
|
.interrupt = atusb_interrupt,
|
2010-09-06 00:59:42 +03:00
|
|
|
};
|