diff options
Diffstat (limited to 'drivers/usb/atm')
-rw-r--r-- | drivers/usb/atm/Kconfig | 50 | ||||
-rw-r--r-- | drivers/usb/atm/Makefile | 7 | ||||
-rw-r--r-- | drivers/usb/atm/cxacru.c | 878 | ||||
-rw-r--r-- | drivers/usb/atm/speedtch.c | 1112 | ||||
-rw-r--r-- | drivers/usb/atm/usb_atm.c | 1188 | ||||
-rw-r--r-- | drivers/usb/atm/usb_atm.h | 176 | ||||
-rw-r--r-- | drivers/usb/atm/usbatm.c | 1230 | ||||
-rw-r--r-- | drivers/usb/atm/usbatm.h | 184 | ||||
-rw-r--r-- | drivers/usb/atm/xusbatm.c | 196 |
9 files changed, 3077 insertions, 1944 deletions
diff --git a/drivers/usb/atm/Kconfig b/drivers/usb/atm/Kconfig index 0d9f5379b8cf..f429862e0974 100644 --- a/drivers/usb/atm/Kconfig +++ b/drivers/usb/atm/Kconfig @@ -1,30 +1,60 @@ # -# USB ATM driver configuration +# USB/ATM DSL configuration # -comment "USB ATM/DSL drivers" + +menu "USB DSL modem support" depends on USB config USB_ATM - tristate "Generic USB ATM/DSL core I/O support" + tristate "USB DSL modem support" depends on USB && ATM select CRC32 default n help - This provides a library which is used for packet I/O by USB DSL - modems, such as the SpeedTouch driver below. + Say Y here if you want to connect a USB Digital Subscriber Line (DSL) + modem to your computer's USB port. You will then need to choose your + modem from the list below. To compile this driver as a module, choose M here: the - module will be called usb_atm. + module will be called usbatm. config USB_SPEEDTOUCH - tristate "Alcatel Speedtouch USB support" - depends on USB && ATM - select USB_ATM + tristate "Speedtouch USB support" + depends on USB_ATM + select FW_LOADER help - Say Y here if you have an Alcatel SpeedTouch USB or SpeedTouch 330 + Say Y here if you have an SpeedTouch USB or SpeedTouch 330 modem. In order to use your modem you will need to install the two parts of the firmware, extracted by the user space tools; see <http://www.linux-usb.org/SpeedTouch/> for details. To compile this driver as a module, choose M here: the module will be called speedtch. + +config USB_CXACRU + tristate "Conexant AccessRunner USB support" + depends on USB_ATM + select FW_LOADER + help + Say Y here if you have an ADSL USB modem based on the Conexant + AccessRunner chipset. In order to use your modem you will need to + install the firmware, extracted by the user space tools; see + <http://accessrunner.sourceforge.net/> for details. + + To compile this driver as a module, choose M here: the + module will be called cxacru. + +config USB_XUSBATM + tristate "Other USB DSL modem support" + depends on USB_ATM + help + Say Y here if you have a DSL USB modem not explicitly supported by + another USB DSL drivers. In order to use your modem you will need to + pass the vendor ID, product ID, and endpoint numbers for transmission + and reception as module parameters. You may need to initialize the + the modem using a user space utility (a firmware loader for example). + + To compile this driver as a module, choose M here: the + module will be called xusbatm. + +endmenu diff --git a/drivers/usb/atm/Makefile b/drivers/usb/atm/Makefile index 9213b8b97587..751f297be2ef 100644 --- a/drivers/usb/atm/Makefile +++ b/drivers/usb/atm/Makefile @@ -1,7 +1,8 @@ # -# Makefile for the rest of the USB drivers -# (the ones that don't fit into any other categories) +# Makefile for USB ATM/xDSL drivers # -obj-$(CONFIG_USB_ATM) += usb_atm.o +obj-$(CONFIG_USB_CXACRU) += cxacru.o obj-$(CONFIG_USB_SPEEDTOUCH) += speedtch.o +obj-$(CONFIG_USB_ATM) += usbatm.o +obj-$(CONFIG_USB_XUSBATM) += xusbatm.o diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c new file mode 100644 index 000000000000..8e184e2641cb --- /dev/null +++ b/drivers/usb/atm/cxacru.c @@ -0,0 +1,878 @@ +/****************************************************************************** + * cxacru.c - driver for USB ADSL modems based on + * Conexant AccessRunner chipset + * + * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan + * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru) + * + * 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ******************************************************************************/ + +/* + * Credit is due for Josep Comas, who created the original patch to speedtch.c + * to support the different padding used by the AccessRunner (now generalized + * into usbatm), and the userspace firmware loading utility. + */ + +#include <linux/module.h> +#include <linux/moduleparam.h> +#include <linux/kernel.h> +#include <linux/timer.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/init.h> +#include <linux/device.h> /* FIXME: linux/firmware.h should include it itself */ +#include <linux/firmware.h> + +#include "usbatm.h" + +#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands" +#define DRIVER_VERSION "0.2" +#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver" + +static const char cxacru_driver_name[] = "cxacru"; + +#define CXACRU_EP_CMD 0x01 /* Bulk/interrupt in/out */ +#define CXACRU_EP_DATA 0x02 /* Bulk in/out */ + +#define CMD_PACKET_SIZE 64 /* Should be maxpacket(ep)? */ + +/* Addresses */ +#define PLLFCLK_ADDR 0x00350068 +#define PLLBCLK_ADDR 0x0035006c +#define SDRAMEN_ADDR 0x00350010 +#define FW_ADDR 0x00801000 +#define BR_ADDR 0x00180600 +#define SIG_ADDR 0x00180500 +#define BR_STACK_ADDR 0x00187f10 + +/* Values */ +#define SDRAM_ENA 0x1 + +#define CMD_TIMEOUT 2000 /* msecs */ +#define POLL_INTERVAL 5000 /* msecs */ + +/* commands for interaction with the modem through the control channel before + * firmware is loaded */ +enum cxacru_fw_request { + FW_CMD_ERR, + FW_GET_VER, + FW_READ_MEM, + FW_WRITE_MEM, + FW_RMW_MEM, + FW_CHECKSUM_MEM, + FW_GOTO_MEM, +}; + +/* commands for interaction with the modem through the control channel once + * firmware is loaded */ +enum cxacru_cm_request { + CM_REQUEST_UNDEFINED = 0x80, + CM_REQUEST_TEST, + CM_REQUEST_CHIP_GET_MAC_ADDRESS, + CM_REQUEST_CHIP_GET_DP_VERSIONS, + CM_REQUEST_CHIP_ADSL_LINE_START, + CM_REQUEST_CHIP_ADSL_LINE_STOP, + CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS, + CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED, + CM_REQUEST_CARD_INFO_GET, + CM_REQUEST_CARD_DATA_GET, + CM_REQUEST_CARD_DATA_SET, + CM_REQUEST_COMMAND_HW_IO, + CM_REQUEST_INTERFACE_HW_IO, + CM_REQUEST_CARD_SERIAL_DATA_PATH_GET, + CM_REQUEST_CARD_SERIAL_DATA_PATH_SET, + CM_REQUEST_CARD_CONTROLLER_VERSION_GET, + CM_REQUEST_CARD_GET_STATUS, + CM_REQUEST_CARD_GET_MAC_ADDRESS, + CM_REQUEST_CARD_GET_DATA_LINK_STATUS, + CM_REQUEST_MAX, +}; + +/* reply codes to the commands above */ +enum cxacru_cm_status { + CM_STATUS_UNDEFINED, + CM_STATUS_SUCCESS, + CM_STATUS_ERROR, + CM_STATUS_UNSUPPORTED, + CM_STATUS_UNIMPLEMENTED, + CM_STATUS_PARAMETER_ERROR, + CM_STATUS_DBG_LOOPBACK, + CM_STATUS_MAX, +}; + +/* indices into CARD_INFO_GET return array */ +enum cxacru_info_idx { + CXINF_DOWNSTREAM_RATE, + CXINF_UPSTREAM_RATE, + CXINF_LINK_STATUS, + CXINF_LINE_STATUS, + CXINF_MAC_ADDRESS_HIGH, + CXINF_MAC_ADDRESS_LOW, + CXINF_UPSTREAM_SNR_MARGIN, + CXINF_DOWNSTREAM_SNR_MARGIN, + CXINF_UPSTREAM_ATTENUATION, + CXINF_DOWNSTREAM_ATTENUATION, + CXINF_TRANSMITTER_POWER, + CXINF_UPSTREAM_BITS_PER_FRAME, + CXINF_DOWNSTREAM_BITS_PER_FRAME, + CXINF_STARTUP_ATTEMPTS, + CXINF_UPSTREAM_CRC_ERRORS, + CXINF_DOWNSTREAM_CRC_ERRORS, + CXINF_UPSTREAM_FEC_ERRORS, + CXINF_DOWNSTREAM_FEC_ERRORS, + CXINF_UPSTREAM_HEC_ERRORS, + CXINF_DOWNSTREAM_HEC_ERRORS, + CXINF_LINE_STARTABLE, + CXINF_MODULATION, + CXINF_ADSL_HEADEND, + CXINF_ADSL_HEADEND_ENVIRONMENT, + CXINF_CONTROLLER_VERSION, + /* dunno what the missing two mean */ + CXINF_MAX = 0x1c, +}; + +struct cxacru_modem_type { + u32 pll_f_clk; + u32 pll_b_clk; + int boot_rom_patch; +}; + +struct cxacru_data { + struct usbatm_data *usbatm; + + const struct cxacru_modem_type *modem_type; + + int line_status; + struct work_struct poll_work; + + /* contol handles */ + struct semaphore cm_serialize; + u8 *rcv_buf; + u8 *snd_buf; + struct urb *rcv_urb; + struct urb *snd_urb; + struct completion rcv_done; + struct completion snd_done; +}; + +/* the following three functions are stolen from drivers/usb/core/message.c */ +static void cxacru_blocking_completion(struct urb *urb, struct pt_regs *regs) +{ + complete((struct completion *)urb->context); +} + +static void cxacru_timeout_kill(unsigned long data) +{ + usb_unlink_urb((struct urb *) data); +} + +static int cxacru_start_wait_urb(struct urb *urb, struct completion *done, + int* actual_length) +{ + struct timer_list timer; + int status; + + init_timer(&timer); + timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT); + timer.data = (unsigned long) urb; + timer.function = cxacru_timeout_kill; + add_timer(&timer); + wait_for_completion(done); + status = urb->status; + if (status == -ECONNRESET) + status = -ETIMEDOUT; + del_timer_sync(&timer); + + if (actual_length) + *actual_length = urb->actual_length; + return status; +} + +static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm, + u8 *wdata, int wsize, u8 *rdata, int rsize) +{ + int ret, actlen; + int offb, offd; + const int stride = CMD_PACKET_SIZE - 4; + u8 *wbuf = instance->snd_buf; + u8 *rbuf = instance->rcv_buf; + int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE; + int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE; + + if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) { + dbg("too big transfer requested"); + ret = -ENOMEM; + goto fail; + } + + down(&instance->cm_serialize); + + /* submit reading urb before the writing one */ + init_completion(&instance->rcv_done); + ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL); + if (ret < 0) { + dbg("submitting read urb for cm %#x failed", cm); + ret = ret; + goto fail; + } + + memset(wbuf, 0, wbuflen); + /* handle wsize == 0 */ + wbuf[0] = cm; + for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) { + wbuf[offb] = cm; + memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd)); + } + + instance->snd_urb->transfer_buffer_length = wbuflen; + init_completion(&instance->snd_done); + ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL); + if (ret < 0) { + dbg("submitting write urb for cm %#x failed", cm); + ret = ret; + goto fail; + } + + ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL); + if (ret < 0) { + dbg("sending cm %#x failed", cm); + ret = ret; + goto fail; + } + + ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen); + if (ret < 0) { + dbg("receiving cm %#x failed", cm); + ret = ret; + goto fail; + } + if (actlen % CMD_PACKET_SIZE || !actlen) { + dbg("response is not a positive multiple of %d: %#x", + CMD_PACKET_SIZE, actlen); + ret = -EIO; + goto fail; + } + + /* check the return status and copy the data to the output buffer, if needed */ + for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) { + if (rbuf[offb] != cm) { + dbg("wrong cm %#x in response", rbuf[offb]); + ret = -EIO; + goto fail; + } + if (rbuf[offb + 1] != CM_STATUS_SUCCESS) { + dbg("response failed: %#x", rbuf[offb + 1]); + ret = -EIO; + goto fail; + } + if (offd >= rsize) + break; + memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd)); + offd += stride; + } + + ret = offd; + dbg("cm %#x", cm); +fail: + up(&instance->cm_serialize); + return ret; +} + +static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm, + u32 *data, int size) +{ + int ret, len; + u32 *buf; + int offb, offd; + const int stride = CMD_PACKET_SIZE / (4 * 2) - 1; + int buflen = ((size - 1) / stride + 1 + size * 2) * 4; + + buf = kmalloc(buflen, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen); + if (ret < 0) + goto cleanup; + + /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */ + len = ret / 4; + for (offb = 0; offb < len; ) { + int l = le32_to_cpu(buf[offb++]); + if (l > stride || l > (len - offb) / 2) { + dbg("wrong data length %#x in response", l); + ret = -EIO; + goto cleanup; + } + while (l--) { + offd = le32_to_cpu(buf[offb++]); + if (offd >= size) { + dbg("wrong index %#x in response", offd); + ret = -EIO; + goto cleanup; + } + data[offd] = le32_to_cpu(buf[offb++]); + } + } + + ret = 0; + +cleanup: + kfree(buf); + return ret; +} + +static int cxacru_card_status(struct cxacru_data *instance) +{ + int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0); + if (ret < 0) { /* firmware not loaded */ + dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret); + return ret; + } + return 0; +} + +static void cxacru_poll_status(struct cxacru_data *instance); + +static int cxacru_atm_start(struct usbatm_data *usbatm_instance, + struct atm_dev *atm_dev) +{ + struct cxacru_data *instance = usbatm_instance->driver_data; + struct device *dev = &usbatm_instance->usb_intf->dev; + /* + struct atm_dev *atm_dev = usbatm_instance->atm_dev; + */ + int ret; + + dbg("cxacru_atm_start"); + + /* Read MAC address */ + ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0, + atm_dev->esi, sizeof(atm_dev->esi)); + if (ret < 0) { + dev_err(dev, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret); + return ret; + } + + /* start ADSL */ + ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0); + if (ret < 0) { + dev_err(dev, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret); + return ret; + } + + /* Start status polling */ + cxacru_poll_status(instance); + return 0; +} + +static void cxacru_poll_status(struct cxacru_data *instance) +{ + u32 buf[CXINF_MAX] = {}; + struct device *dev = &instance->usbatm->usb_intf->dev; + struct atm_dev *atm_dev = instance->usbatm->atm_dev; + int ret; + + ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX); + if (ret < 0) { + dev_warn(dev, "poll status: error %d\n", ret); + goto reschedule; + } + + if (instance->line_status == buf[CXINF_LINE_STATUS]) + goto reschedule; + + instance->line_status = buf[CXINF_LINE_STATUS]; + switch (instance->line_status) { + case 0: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: down\n"); + break; + + case 1: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: attemtping to activate\n"); + break; + + case 2: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: training\n"); + break; + + case 3: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: channel analysis\n"); + break; + + case 4: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: exchange\n"); + break; + + case 5: + atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424; + atm_dev->signal = ATM_PHY_SIG_FOUND; + + dev_info(dev, "ADSL line: up (%d kb/s down | %d kb/s up)\n", + buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]); + break; + + case 6: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: waiting\n"); + break; + + case 7: + atm_dev->signal = ATM_PHY_SIG_LOST; + dev_info(dev, "ADSL line: initializing\n"); + break; + + default: + atm_dev->signal = ATM_PHY_SIG_UNKNOWN; + dev_info(dev, "Unknown line state %02x\n", instance->line_status); + break; + } +reschedule: + schedule_delayed_work(&instance->poll_work, msecs_to_jiffies(POLL_INTERVAL)); +} + +static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw, + u8 code1, u8 code2, u32 addr, u8 *data, int size) +{ + int ret; + u8 *buf; + int offd, offb; + const int stride = CMD_PACKET_SIZE - 8; + + buf = (u8 *) __get_free_page(GFP_KERNEL); + if (!buf) + return -ENOMEM; + + offb = offd = 0; + do { + int l = min_t(int, stride, size - offd); + buf[offb++] = fw; + buf[offb++] = l; + buf[offb++] = code1; + buf[offb++] = code2; + *((u32 *) (buf + offb)) = cpu_to_le32(addr); + offb += 4; + addr += l; + if(l) + memcpy(buf + offb, data + offd, l); + if (l < stride) + memset(buf + offb + l, 0, stride - l); + offb += stride; + offd += stride; + if ((offb >= PAGE_SIZE) || (offd >= size)) { + ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD), + buf, offb, NULL, CMD_TIMEOUT); + if (ret < 0) { + dbg("sending fw %#x failed", fw); + goto cleanup; + } + offb = 0; + } + } while(offd < size); + dbg("sent fw %#x", fw); + + ret = 0; + +cleanup: + free_page((unsigned long) buf); + return ret; +} + +static void cxacru_upload_firmware(struct cxacru_data *instance, + const struct firmware *fw, + const struct firmware *bp, + const struct firmware *cf) +{ + int ret; + int off; + struct usb_device *usb_dev = instance->usbatm->usb_dev; + struct device *dev = &instance->usbatm->usb_intf->dev; + u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct }; + u32 val; + + dbg("cxacru_upload_firmware"); + + /* FirmwarePllFClkValue */ + val = cpu_to_le32(instance->modem_type->pll_f_clk); + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4); + if (ret) { + dev_err(dev, "FirmwarePllFClkValue failed: %d\n", ret); + return; + } + + /* FirmwarePllBClkValue */ + val = cpu_to_le32(instance->modem_type->pll_b_clk); + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4); + if (ret) { + dev_err(dev, "FirmwarePllBClkValue failed: %d\n", ret); + return; + } + + /* Enable SDRAM */ + val = cpu_to_le32(SDRAM_ENA); + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4); + if (ret) { + dev_err(dev, "Enable SDRAM failed: %d\n", ret); + return; + } + + /* Firmware */ + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size); + if (ret) { + dev_err(dev, "Firmware upload failed: %d\n", ret); + return; + } + + /* Boot ROM patch */ + if (instance->modem_type->boot_rom_patch) { + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size); + if (ret) { + dev_err(dev, "Boot ROM patching failed: %d\n", ret); + return; + } + } + + /* Signature */ + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4); + if (ret) { + dev_err(dev, "Signature storing failed: %d\n", ret); + return; + } + + if (instance->modem_type->boot_rom_patch) { + val = cpu_to_le32(BR_ADDR); + ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4); + } + else { + ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0); + } + if (ret) { + dev_err(dev, "Passing control to firmware failed: %d\n", ret); + return; + } + + /* Delay to allow firmware to start up. */ + msleep_interruptible(1000); + + usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD)); + usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD)); + usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA)); + usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA)); + + ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0); + if (ret < 0) { + dev_err(dev, "modem failed to initialize: %d\n", ret); + return; + } + + /* Load config data (le32), doing one packet at a time */ + if (cf) + for (off = 0; off < cf->size / 4; ) { + u32 buf[CMD_PACKET_SIZE / 4 - 1]; + int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1); + buf[0] = cpu_to_le32(len); + for (i = 0; i < len; i++, off++) { + buf[i * 2 + 1] = cpu_to_le32(off); + memcpy(buf + i * 2 + 2, cf->data + off * 4, 4); + } + ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET, + (u8 *) buf, len, NULL, 0); + if (ret < 0) { + dev_err(dev, "load config data failed: %d\n", ret); + return; + } + } + + msleep_interruptible(4000); +} + +static int cxacru_find_firmware(struct cxacru_data *instance, + char* phase, const struct firmware **fw_p) +{ + struct device *dev = &instance->usbatm->usb_intf->dev; + char buf[16]; + + sprintf(buf, "cxacru-%s.bin", phase); + dbg("cxacru_find_firmware: looking for %s", buf); + + if (request_firmware(fw_p, buf, dev)) { + dev_dbg(dev, "no stage %s firmware found\n", phase); + return -ENOENT; + } + + dev_info(dev, "found firmware %s\n", buf); + + return 0; +} + +static int cxacru_heavy_init(struct usbatm_data *usbatm_instance, + struct usb_interface *usb_intf) +{ + struct device *dev = &usbatm_instance->usb_intf->dev; + const struct firmware *fw, *bp, *cf; + struct cxacru_data *instance = usbatm_instance->driver_data; + + int ret = cxacru_find_firmware(instance, "fw", &fw); + if (ret) { + dev_warn(dev, "firmware (cxacru-fw.bin) unavailable (hotplug misconfiguration?)\n"); + return ret; + } + + if (instance->modem_type->boot_rom_patch) { + ret = cxacru_find_firmware(instance, "bp", &bp); + if (ret) { + dev_warn(dev, "boot ROM patch (cxacru-bp.bin) unavailable (hotplug misconfiguration?)\n"); + release_firmware(fw); + return ret; + } + } + + if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */ + cf = NULL; + + cxacru_upload_firmware(instance, fw, bp, cf); + + if (cf) + release_firmware(cf); + if (instance->modem_type->boot_rom_patch) + release_firmware(bp); + release_firmware(fw); + + ret = cxacru_card_status(instance); + if (ret) + dbg("modem initialisation failed"); + else + dbg("done setting up the modem"); + + return ret; +} + +static int cxacru_bind(struct usbatm_data *usbatm_instance, + struct usb_interface *intf, const struct usb_device_id *id, + int *need_heavy_init) +{ + struct cxacru_data *instance; + struct usb_device *usb_dev = interface_to_usbdev(intf); + int ret; + + /* instance init */ + instance = kmalloc(sizeof(*instance), GFP_KERNEL); + if (!instance) { + dbg("cxacru_bind: no memory for instance data"); + return -ENOMEM; + } + + memset(instance, 0, sizeof(*instance)); + + instance->usbatm = usbatm_instance; + instance->modem_type = (struct cxacru_modem_type *) id->driver_info; + + instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL); + if (!instance->rcv_buf) { + dbg("cxacru_bind: no memory for rcv_buf"); + ret = -ENOMEM; + goto fail; + } + instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL); + if (!instance->snd_buf) { + dbg("cxacru_bind: no memory for snd_buf"); + ret = -ENOMEM; + goto fail; + } + instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL); + if (!instance->rcv_urb) { + dbg("cxacru_bind: no memory for rcv_urb"); + ret = -ENOMEM; + goto fail; + } + instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL); + if (!instance->snd_urb) { + dbg("cxacru_bind: no memory for snd_urb"); + ret = -ENOMEM; + goto fail; + } + + usb_fill_int_urb(instance->rcv_urb, + usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD), + instance->rcv_buf, PAGE_SIZE, + cxacru_blocking_completion, &instance->rcv_done, 1); + instance->rcv_urb->transfer_flags |= URB_ASYNC_UNLINK; + + usb_fill_int_urb(instance->snd_urb, + usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD), + instance->snd_buf, PAGE_SIZE, + cxacru_blocking_completion, &instance->snd_done, 4); + instance->snd_urb->transfer_flags |= URB_ASYNC_UNLINK; + + init_MUTEX(&instance->cm_serialize); + + INIT_WORK(&instance->poll_work, (void *)cxacru_poll_status, instance); + + usbatm_instance->driver_data = instance; + + *need_heavy_init = cxacru_card_status(instance); + + return 0; + + fail: + free_page((unsigned long) instance->snd_buf); + free_page((unsigned long) instance->rcv_buf); + usb_free_urb(instance->snd_urb); + usb_free_urb(instance->rcv_urb); + kfree(instance); + + return ret; +} + +static void cxacru_unbind(struct usbatm_data *usbatm_instance, + struct usb_interface *intf) +{ + struct cxacru_data *instance = usbatm_instance->driver_data; + + dbg("cxacru_unbind entered"); + + if (!instance) { + dbg("cxacru_unbind: NULL instance!"); + return; + } + + while (!cancel_delayed_work(&instance->poll_work)) + flush_scheduled_work(); + + usb_kill_urb(instance->snd_urb); + usb_kill_urb(instance->rcv_urb); + usb_free_urb(instance->snd_urb); + usb_free_urb(instance->rcv_urb); + + free_page((unsigned long) instance->snd_buf); + free_page((unsigned long) instance->rcv_buf); + kfree(instance); + + usbatm_instance->driver_data = NULL; +} + +static const struct cxacru_modem_type cxacru_cafe = { + .pll_f_clk = 0x02d874df, + .pll_b_clk = 0x0196a51a, + .boot_rom_patch = 1, +}; + +static const struct cxacru_modem_type cxacru_cb00 = { + .pll_f_clk = 0x5, + .pll_b_clk = 0x3, + .boot_rom_patch = 0, +}; + +static const struct usb_device_id cxacru_usb_ids[] = { + { /* V = Conexant P = ADSL modem (Euphrates project) */ + USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe + }, + { /* V = Conexant P = ADSL modem (Hasbani project) */ + USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Conexant P = ADSL modem */ + USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Conexant P = ADSL modem */ + USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Olitec P = ADSL modem version 2 */ + USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe + }, + { /* V = Olitec P = ADSL modem version 3 */ + USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */ + USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe + }, + { /* V = Zoom P = 5510 */ + USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Draytek P = Vigor 318 */ + USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */ + USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */ + USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Aethra P = Starmodem UM1020 */ + USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */ + USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */ + USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00 + }, + { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */ + USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00 + }, + {} +}; + +MODULE_DEVICE_TABLE(usb, cxacru_usb_ids); + +static struct usbatm_driver cxacru_driver = { + .owner = THIS_MODULE, + .driver_name = cxacru_driver_name, + .bind = cxacru_bind, + .heavy_init = cxacru_heavy_init, + .unbind = cxacru_unbind, + .atm_start = cxacru_atm_start, + .in = CXACRU_EP_DATA, + .out = CXACRU_EP_DATA, + .rx_padding = 3, + .tx_padding = 11, +}; + +static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) +{ + return usbatm_usb_probe(intf, id, &cxacru_driver); +} + +static struct usb_driver cxacru_usb_driver = { + .owner = THIS_MODULE, + .name = cxacru_driver_name, + .probe = cxacru_usb_probe, + .disconnect = usbatm_usb_disconnect, + .id_table = cxacru_usb_ids +}; + +static int __init cxacru_init(void) +{ + return usb_register(&cxacru_usb_driver); +} + +static void __exit cxacru_cleanup(void) +{ + usb_deregister(&cxacru_usb_driver); +} + +module_init(cxacru_init); +module_exit(cxacru_cleanup); + +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_LICENSE("GPL"); +MODULE_VERSION(DRIVER_VERSION); diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c index 2a1697bfd695..d0cbbb7f0385 100644 --- a/drivers/usb/atm/speedtch.c +++ b/drivers/usb/atm/speedtch.c @@ -5,6 +5,8 @@ * Copyright (C) 2003, Duncan Sands * Copyright (C) 2004, David Woodhouse * + * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault + * * 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) @@ -21,467 +23,191 @@ * ******************************************************************************/ -#include <linux/module.h> -#include <linux/moduleparam.h> +#include <asm/page.h> +#include <linux/device.h> +#include <linux/errno.h> +#include <linux/firmware.h> #include <linux/gfp.h> +#include <linux/init.h> #include <linux/kernel.h> -#include <linux/sched.h> -#include <linux/timer.h> -#include <linux/errno.h> -#include <linux/proc_fs.h> +#include <linux/module.h> +#include <linux/moduleparam.h> #include <linux/slab.h> -#include <linux/wait.h> -#include <linux/list.h> -#include <asm/processor.h> -#include <asm/uaccess.h> -#include <linux/smp_lock.h> -#include <linux/interrupt.h> -#include <linux/atm.h> -#include <linux/atmdev.h> -#include <linux/crc32.h> -#include <linux/init.h> -#include <linux/firmware.h> - -#include "usb_atm.h" +#include <linux/stat.h> +#include <linux/timer.h> +#include <linux/workqueue.h> -#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE) -# define USE_FW_LOADER -#endif +#include "usbatm.h" #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" -#define DRIVER_VERSION "1.8" +#define DRIVER_VERSION "1.9" #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION static const char speedtch_driver_name[] = "speedtch"; -#define SPEEDTOUCH_VENDORID 0x06b9 -#define SPEEDTOUCH_PRODUCTID 0x4061 +#define CTRL_TIMEOUT 2000 /* milliseconds */ +#define DATA_TIMEOUT 2000 /* milliseconds */ -/* Timeout in jiffies */ -#define CTRL_TIMEOUT 2000 -#define DATA_TIMEOUT 2000 +#define OFFSET_7 0 /* size 1 */ +#define OFFSET_b 1 /* size 8 */ +#define OFFSET_d 9 /* size 4 */ +#define OFFSET_e 13 /* size 1 */ +#define OFFSET_f 14 /* size 1 */ +#define TOTAL 15 -#define OFFSET_7 0 /* size 1 */ -#define OFFSET_b 1 /* size 8 */ -#define OFFSET_d 9 /* size 4 */ -#define OFFSET_e 13 /* size 1 */ -#define OFFSET_f 14 /* size 1 */ -#define TOTAL 15 +#define SIZE_7 1 +#define SIZE_b 8 +#define SIZE_d 4 +#define SIZE_e 1 +#define SIZE_f 1 -#define SIZE_7 1 -#define SIZE_b 8 -#define SIZE_d 4 -#define SIZE_e 1 -#define SIZE_f 1 +#define MIN_POLL_DELAY 5000 /* milliseconds */ +#define MAX_POLL_DELAY 60000 /* milliseconds */ -static int dl_512_first = 0; -static int sw_buffering = 0; +#define RESUBMIT_DELAY 1000 /* milliseconds */ -module_param(dl_512_first, bool, 0444); -MODULE_PARM_DESC(dl_512_first, "Read 512 bytes before sending firmware"); +#define DEFAULT_ALTSETTING 1 +#define DEFAULT_DL_512_FIRST 0 +#define DEFAULT_SW_BUFFERING 0 -module_param(sw_buffering, uint, 0444); -MODULE_PARM_DESC(sw_buffering, "Enable software buffering"); +static int altsetting = DEFAULT_ALTSETTING; +static int dl_512_first = DEFAULT_DL_512_FIRST; +static int sw_buffering = DEFAULT_SW_BUFFERING; -#define UDSL_IOCTL_LINE_UP 1 -#define UDSL_IOCTL_LINE_DOWN 2 +module_param(altsetting, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(altsetting, + "Alternative setting for data interface (default: " + __MODULE_STRING(DEFAULT_ALTSETTING) ")"); -#define SPEEDTCH_ENDPOINT_INT 0x81 -#define SPEEDTCH_ENDPOINT_DATA 0x07 -#define SPEEDTCH_ENDPOINT_FIRMWARE 0x05 +module_param(dl_512_first, bool, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(dl_512_first, + "Read 512 bytes before sending firmware (default: " + __MODULE_STRING(DEFAULT_DL_512_FIRST) ")"); -#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) ) +module_param(sw_buffering, bool, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(sw_buffering, + "Enable software buffering (default: " + __MODULE_STRING(DEFAULT_SW_BUFFERING) ")"); -static struct usb_device_id speedtch_usb_ids[] = { - {USB_DEVICE(SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID)}, - {} -}; +#define ENDPOINT_INT 0x81 +#define ENDPOINT_DATA 0x07 +#define ENDPOINT_FIRMWARE 0x05 -MODULE_DEVICE_TABLE(usb, speedtch_usb_ids); +#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) ) struct speedtch_instance_data { - struct udsl_instance_data u; + struct usbatm_data *usbatm; + + struct work_struct status_checker; + + unsigned char last_status; + + int poll_delay; /* milliseconds */ - /* Status */ + struct timer_list resubmit_timer; struct urb *int_urb; unsigned char int_data[16]; - struct work_struct poll_work; - struct timer_list poll_timer; -}; -/* USB */ - -static int speedtch_usb_probe(struct usb_interface *intf, - const struct usb_device_id *id); -static void speedtch_usb_disconnect(struct usb_interface *intf); -static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code, - void *user_data); -static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs); -static void speedtch_poll_status(struct speedtch_instance_data *instance); -static struct usb_driver speedtch_usb_driver = { - .owner = THIS_MODULE, - .name = speedtch_driver_name, - .probe = speedtch_usb_probe, - .disconnect = speedtch_usb_disconnect, - .ioctl = speedtch_usb_ioctl, - .id_table = speedtch_usb_ids, + unsigned char scratch_buffer[TOTAL]; }; /*************** ** firmware ** ***************/ -static void speedtch_got_firmware(struct speedtch_instance_data *instance, - int got_it) -{ - int err; - struct usb_interface *intf; - - down(&instance->u.serialize); /* vs self, speedtch_firmware_start */ - if (instance->u.status == UDSL_LOADED_FIRMWARE) - goto out; - if (!got_it) { - instance->u.status = UDSL_NO_FIRMWARE; - goto out; - } - if ((err = usb_set_interface(instance->u.usb_dev, 1, 1)) < 0) { - dbg("speedtch_got_firmware: usb_set_interface returned %d!", err); - instance->u.status = UDSL_NO_FIRMWARE; - goto out; - } - - /* Set up interrupt endpoint */ - intf = usb_ifnum_to_if(instance->u.usb_dev, 0); - if (intf && !usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) { - - instance->int_urb = usb_alloc_urb(0, GFP_KERNEL); - if (instance->int_urb) { - - usb_fill_int_urb(instance->int_urb, instance->u.usb_dev, - usb_rcvintpipe(instance->u.usb_dev, SPEEDTCH_ENDPOINT_INT), - instance->int_data, - sizeof(instance->int_data), - speedtch_handle_int, instance, 50); - err = usb_submit_urb(instance->int_urb, GFP_KERNEL); - if (err) { - /* Doesn't matter; we'll poll anyway */ - dbg("speedtch_got_firmware: Submission of interrupt URB failed %d", err); - usb_free_urb(instance->int_urb); - instance->int_urb = NULL; - usb_driver_release_interface(&speedtch_usb_driver, intf); - } - } - } - /* Start status polling */ - mod_timer(&instance->poll_timer, jiffies + (1 * HZ)); - - instance->u.status = UDSL_LOADED_FIRMWARE; - tasklet_schedule(&instance->u.receive_tasklet); - out: - up(&instance->u.serialize); - wake_up_interruptible(&instance->u.firmware_waiters); -} - -static int speedtch_set_swbuff(struct speedtch_instance_data *instance, - int state) +static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state) { - struct usb_device *dev = instance->u.usb_dev; + struct usbatm_data *usbatm = instance->usbatm; + struct usb_device *usb_dev = usbatm->usb_dev; int ret; - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), - 0x32, 0x40, state ? 0x01 : 0x00, - 0x00, NULL, 0, 100); - if (ret < 0) { - printk("Warning: %sabling SW buffering: usb_control_msg returned %d\n", - state ? "En" : "Dis", ret); - return ret; - } - - dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis"); - return 0; + ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), + 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT); + if (ret < 0) + usb_warn(usbatm, + "%sabling SW buffering: usb_control_msg returned %d\n", + state ? "En" : "Dis", ret); + else + dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis"); } static void speedtch_test_sequence(struct speedtch_instance_data *instance) { - struct usb_device *dev = instance->u.usb_dev; - unsigned char buf[10]; + struct usbatm_data *usbatm = instance->usbatm; + struct usb_device *usb_dev = usbatm->usb_dev; + unsigned char *buf = instance->scratch_buffer; int ret; /* URB 147 */ buf[0] = 0x1c; buf[1] = 0x50; - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), - 0x01, 0x40, 0x0b, 0x00, buf, 2, 100); + ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), + 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT); if (ret < 0) - printk(KERN_WARNING "%s failed on URB147: %d\n", __func__, ret); + usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret); /* URB 148 */ buf[0] = 0x32; buf[1] = 0x00; - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), - 0x01, 0x40, 0x02, 0x00, buf, 2, 100); + ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), + 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT); if (ret < 0) - printk(KERN_WARNING "%s failed on URB148: %d\n", __func__, ret); + usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret); /* URB 149 */ buf[0] = 0x01; buf[1] = 0x00; buf[2] = 0x01; - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), - 0x01, 0x40, 0x03, 0x00, buf, 3, 100); + ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), + 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT); if (ret < 0) - printk(KERN_WARNING "%s failed on URB149: %d\n", __func__, ret); + usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret); /* URB 150 */ buf[0] = 0x01; buf[1] = 0x00; buf[2] = 0x01; - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), - 0x01, 0x40, 0x04, 0x00, buf, 3, 100); + ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), + 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT); if (ret < 0) - printk(KERN_WARNING "%s failed on URB150: %d\n", __func__, ret); -} - -static int speedtch_start_synchro(struct speedtch_instance_data *instance) -{ - struct usb_device *dev = instance->u.usb_dev; - unsigned char buf[2]; - int ret; - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x12, 0xc0, 0x04, 0x00, - buf, sizeof(buf), CTRL_TIMEOUT); - if (ret < 0) { - printk(KERN_WARNING "SpeedTouch: Failed to start ADSL synchronisation: %d\n", ret); - return ret; - } - - dbg("speedtch_start_synchro: modem prodded. %d Bytes returned: %02x %02x", ret, buf[0], buf[1]); - return 0; -} - -static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs) -{ - struct speedtch_instance_data *instance = urb->context; - unsigned int count = urb->actual_length; - int ret; - - /* The magic interrupt for "up state" */ - const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 }; - /* The magic interrupt for "down state" */ - const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 }; - - switch (urb->status) { - case 0: - /* success */ - break; - case -ECONNRESET: - case -ENOENT: - case -ESHUTDOWN: - /* this urb is terminated; clean up */ - dbg("%s - urb shutting down with status: %d", __func__, urb->status); - return; - default: - dbg("%s - nonzero urb status received: %d", __func__, urb->status); - goto exit; - } - - if (count < 6) { - dbg("%s - int packet too short", __func__); - goto exit; - } - - if (!memcmp(up_int, instance->int_data, 6)) { - del_timer(&instance->poll_timer); - printk(KERN_NOTICE "DSL line goes up\n"); - } else if (!memcmp(down_int, instance->int_data, 6)) { - printk(KERN_NOTICE "DSL line goes down\n"); - } else { - int i; - - printk(KERN_DEBUG "Unknown interrupt packet of %d bytes:", count); - for (i = 0; i < count; i++) - printk(" %02x", instance->int_data[i]); - printk("\n"); - } - schedule_work(&instance->poll_work); - - exit: - rmb(); - if (!instance->int_urb) - return; - - ret = usb_submit_urb(urb, GFP_ATOMIC); - if (ret) - err("%s - usb_submit_urb failed with result %d", __func__, ret); -} - -static int speedtch_get_status(struct speedtch_instance_data *instance, - unsigned char *buf) -{ - struct usb_device *dev = instance->u.usb_dev; - int ret; - - memset(buf, 0, TOTAL); - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7, - CTRL_TIMEOUT); - if (ret < 0) { - dbg("MSG 7 failed"); - return ret; - } - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b, - CTRL_TIMEOUT); - if (ret < 0) { - dbg("MSG B failed"); - return ret; - } - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d, - CTRL_TIMEOUT); - if (ret < 0) { - dbg("MSG D failed"); - return ret; - } - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e, - CTRL_TIMEOUT); - if (ret < 0) { - dbg("MSG E failed"); - return ret; - } - - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f, - CTRL_TIMEOUT); - if (ret < 0) { - dbg("MSG F failed"); - return ret; - } - - return 0; + usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret); } -static void speedtch_poll_status(struct speedtch_instance_data *instance) -{ - unsigned char buf[TOTAL]; - int ret; - - ret = speedtch_get_status(instance, buf); - if (ret) { - printk(KERN_WARNING - "SpeedTouch: Error %d fetching device status\n", ret); - return; - } - - dbg("Line state %02x", buf[OFFSET_7]); - - switch (buf[OFFSET_7]) { - case 0: - if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) { - instance->u.atm_dev->signal = ATM_PHY_SIG_LOST; - printk(KERN_NOTICE "ADSL line is down\n"); - /* It'll never resync again unless we ask it to... */ - speedtch_start_synchro(instance); - } - break; - - case 0x08: - if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) { - instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN; - printk(KERN_NOTICE "ADSL line is blocked?\n"); - } - break; - - case 0x10: - if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) { - instance->u.atm_dev->signal = ATM_PHY_SIG_LOST; - printk(KERN_NOTICE "ADSL line is synchronising\n"); - } - break; - - case 0x20: - if (instance->u.atm_dev->signal != ATM_PHY_SIG_FOUND) { - int down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8) - | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24); - int up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8) - | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24); - - if (!(down_speed & 0x0000ffff) && - !(up_speed & 0x0000ffff)) { - down_speed >>= 16; - up_speed >>= 16; - } - instance->u.atm_dev->link_rate = down_speed * 1000 / 424; - instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND; - - printk(KERN_NOTICE - "ADSL line is up (%d Kib/s down | %d Kib/s up)\n", - down_speed, up_speed); - } - break; - - default: - if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) { - instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN; - printk(KERN_NOTICE "Unknown line state %02x\n", buf[OFFSET_7]); - } - break; - } -} - -static void speedtch_timer_poll(unsigned long data) -{ - struct speedtch_instance_data *instance = (void *)data; - - schedule_work(&instance->poll_work); - mod_timer(&instance->poll_timer, jiffies + (5 * HZ)); -} - -#ifdef USE_FW_LOADER -static void speedtch_upload_firmware(struct speedtch_instance_data *instance, +static int speedtch_upload_firmware(struct speedtch_instance_data *instance, const struct firmware *fw1, const struct firmware *fw2) { unsigned char *buffer; - struct usb_device *usb_dev = instance->u.usb_dev; + struct usbatm_data *usbatm = instance->usbatm; struct usb_interface *intf; - int actual_length, ret; + struct usb_device *usb_dev = usbatm->usb_dev; + int actual_length; + int ret = 0; int offset; - dbg("speedtch_upload_firmware"); - - if (!(intf = usb_ifnum_to_if(usb_dev, 2))) { - dbg("speedtch_upload_firmware: interface not found!"); - goto fail; - } + usb_dbg(usbatm, "%s entered\n", __func__); if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) { - dbg("speedtch_upload_firmware: no memory for buffer!"); - goto fail; + ret = -ENOMEM; + usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__); + goto out; } - /* A user-space firmware loader may already have claimed interface #2 */ - if ((ret = - usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) < 0) { - dbg("speedtch_upload_firmware: interface in use (%d)!", ret); - goto fail_free; + if (!(intf = usb_ifnum_to_if(usb_dev, 2))) { + ret = -ENODEV; + usb_dbg(usbatm, "%s: interface not found!\n", __func__); + goto out_free; } /* URB 7 */ if (dl_512_first) { /* some modems need a read before writing the firmware */ - ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), + ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE), buffer, 0x200, &actual_length, 2000); if (ret < 0 && ret != -ETIMEDOUT) - dbg("speedtch_upload_firmware: read BLOCK0 from modem failed (%d)!", ret); + usb_dbg(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret); else - dbg("speedtch_upload_firmware: BLOCK0 downloaded (%d bytes)", ret); + usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret); } /* URB 8 : both leds are static green */ @@ -489,60 +215,60 @@ static void speedtch_upload_firmware(struct speedtch_instance_data *instance, int thislen = min_t(int, PAGE_SIZE, fw1->size - offset); memcpy(buffer, fw1->data + offset, thislen); - ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), + ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE), buffer, thislen, &actual_length, DATA_TIMEOUT); if (ret < 0) { - dbg("speedtch_upload_firmware: write BLOCK1 to modem failed (%d)!", ret); - goto fail_release; + usb_dbg(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret); + goto out_free; } - dbg("speedtch_upload_firmware: BLOCK1 uploaded (%zu bytes)", fw1->size); + usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size); } /* USB led blinking green, ADSL led off */ /* URB 11 */ - ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), + ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE), buffer, 0x200, &actual_length, DATA_TIMEOUT); if (ret < 0) { - dbg("speedtch_upload_firmware: read BLOCK2 from modem failed (%d)!", ret); - goto fail_release; + usb_dbg(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret); + goto out_free; } - dbg("speedtch_upload_firmware: BLOCK2 downloaded (%d bytes)", actual_length); + usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length); /* URBs 12 to 139 - USB led blinking green, ADSL led off */ for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) { int thislen = min_t(int, PAGE_SIZE, fw2->size - offset); memcpy(buffer, fw2->data + offset, thislen); - ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), + ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE), buffer, thislen, &actual_length, DATA_TIMEOUT); if (ret < 0) { - dbg("speedtch_upload_firmware: write BLOCK3 to modem failed (%d)!", ret); - goto fail_release; + usb_dbg(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret); + goto out_free; } } - dbg("speedtch_upload_firmware: BLOCK3 uploaded (%zu bytes)", fw2->size); + usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size); /* USB led static green, ADSL led static red */ /* URB 142 */ - ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), + ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE), buffer, 0x200, &actual_length, DATA_TIMEOUT); if (ret < 0) { - dbg("speedtch_upload_firmware: read BLOCK4 from modem failed (%d)!", ret); - goto fail_release; + usb_dbg(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret); + goto out_free; } /* success */ - dbg("speedtch_upload_firmware: BLOCK4 downloaded (%d bytes)", actual_length); + usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length); /* Delay to allow firmware to start up. We can do this here because we're in our own kernel thread anyway. */ - msleep(1000); + msleep_interruptible(1000); /* Enable software buffering, if requested */ if (sw_buffering) @@ -551,291 +277,543 @@ static void speedtch_upload_firmware(struct speedtch_instance_data *instance, /* Magic spell; don't ask us what this does */ speedtch_test_sequence(instance); - /* Start modem synchronisation */ - if (speedtch_start_synchro(instance)) - dbg("speedtch_start_synchro: failed"); - - speedtch_got_firmware(instance, 1); - - free_page((unsigned long)buffer); - return; + ret = 0; - fail_release: - /* Only release interface #2 if uploading failed; we don't release it - we succeeded. This prevents the userspace tools from trying to load - the firmware themselves */ - usb_driver_release_interface(&speedtch_usb_driver, intf); - fail_free: +out_free: free_page((unsigned long)buffer); - fail: - speedtch_got_firmware(instance, 0); +out: + return ret; } -static int speedtch_find_firmware(struct speedtch_instance_data - *instance, int phase, +static int speedtch_find_firmware(struct usb_interface *intf, int phase, const struct firmware **fw_p) { - char buf[24]; - const u16 bcdDevice = le16_to_cpu(instance->u.usb_dev->descriptor.bcdDevice); + struct device *dev = &intf->dev; + const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice); const u8 major_revision = bcdDevice >> 8; const u8 minor_revision = bcdDevice & 0xff; + char buf[24]; sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision); - dbg("speedtch_find_firmware: looking for %s", buf); + dev_dbg(dev, "%s: looking for %s\n", __func__, buf); - if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) { + if (request_firmware(fw_p, buf, dev)) { sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision); - dbg("speedtch_find_firmware: looking for %s", buf); + dev_dbg(dev, "%s: looking for %s\n", __func__, buf); - if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) { + if (request_firmware(fw_p, buf, dev)) { sprintf(buf, "speedtch-%d.bin", phase); - dbg("speedtch_find_firmware: looking for %s", buf); + dev_dbg(dev, "%s: looking for %s\n", __func__, buf); - if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) { - dev_warn(&instance->u.usb_dev->dev, "no stage %d firmware found!", phase); + if (request_firmware(fw_p, buf, dev)) { + dev_warn(dev, "no stage %d firmware found!\n", phase); return -ENOENT; } } } - dev_info(&instance->u.usb_dev->dev, "found stage %d firmware %s\n", phase, buf); + dev_info(dev, "found stage %d firmware %s\n", phase, buf); return 0; } -static int speedtch_load_firmware(void *arg) +static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf) { const struct firmware *fw1, *fw2; - struct speedtch_instance_data *instance = arg; - - BUG_ON(!instance); + struct speedtch_instance_data *instance = usbatm->driver_data; + int ret; - daemonize("firmware/speedtch"); + if ((ret = speedtch_find_firmware(intf, 1, &fw1)) < 0) + return ret; - if (!speedtch_find_firmware(instance, 1, &fw1)) { - if (!speedtch_find_firmware(instance, 2, &fw2)) { - speedtch_upload_firmware(instance, fw1, fw2); - release_firmware(fw2); - } + if ((ret = speedtch_find_firmware(intf, 2, &fw2)) < 0) { release_firmware(fw1); + return ret; } - /* In case we failed, set state back to NO_FIRMWARE so that - another later attempt may work. Otherwise, we never actually - manage to recover if, for example, the firmware is on /usr and - we look for it too early. */ - speedtch_got_firmware(instance, 0); + ret = speedtch_upload_firmware(instance, fw1, fw2); - module_put(THIS_MODULE); - udsl_put_instance(&instance->u); - return 0; + release_firmware(fw2); + release_firmware(fw1); + + return ret; } -#endif /* USE_FW_LOADER */ -static void speedtch_firmware_start(struct speedtch_instance_data *instance) + +/********** +** ATM ** +**********/ + +static int speedtch_read_status(struct speedtch_instance_data *instance) { -#ifdef USE_FW_LOADER + struct usbatm_data *usbatm = instance->usbatm; + struct usb_device *usb_dev = usbatm->usb_dev; + unsigned char *buf = instance->scratch_buffer; int ret; -#endif - dbg("speedtch_firmware_start"); + memset(buf, 0, TOTAL); - down(&instance->u.serialize); /* vs self, speedtch_got_firmware */ + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7, + CTRL_TIMEOUT); + if (ret < 0) { + atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__); + return ret; + } - if (instance->u.status >= UDSL_LOADING_FIRMWARE) { - up(&instance->u.serialize); - return; + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b, + CTRL_TIMEOUT); + if (ret < 0) { + atm_dbg(usbatm, "%s: MSG B failed\n", __func__); + return ret; } - instance->u.status = UDSL_LOADING_FIRMWARE; - up(&instance->u.serialize); + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d, + CTRL_TIMEOUT); + if (ret < 0) { + atm_dbg(usbatm, "%s: MSG D failed\n", __func__); + return ret; + } -#ifdef USE_FW_LOADER - udsl_get_instance(&instance->u); - try_module_get(THIS_MODULE); + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e, + CTRL_TIMEOUT); + if (ret < 0) { + atm_dbg(usbatm, "%s: MSG E failed\n", __func__); + return ret; + } - ret = kernel_thread(speedtch_load_firmware, instance, - CLONE_FS | CLONE_FILES); + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f, + CTRL_TIMEOUT); + if (ret < 0) { + atm_dbg(usbatm, "%s: MSG F failed\n", __func__); + return ret; + } - if (ret >= 0) - return; /* OK */ + return 0; +} - dbg("speedtch_firmware_start: kernel_thread failed (%d)!", ret); +static int speedtch_start_synchro(struct speedtch_instance_data *instance) +{ + struct usbatm_data *usbatm = instance->usbatm; + struct usb_device *usb_dev = usbatm->usb_dev; + unsigned char *buf = instance->scratch_buffer; + int ret; - module_put(THIS_MODULE); - udsl_put_instance(&instance->u); - /* Just pretend it never happened... hope modem_run happens */ -#endif /* USE_FW_LOADER */ + atm_dbg(usbatm, "%s entered\n", __func__); - speedtch_got_firmware(instance, 0); -} + memset(buf, 0, 2); -static int speedtch_firmware_wait(struct udsl_instance_data *instance) -{ - speedtch_firmware_start((void *)instance); + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x12, 0xc0, 0x04, 0x00, + buf, 2, CTRL_TIMEOUT); - if (wait_event_interruptible(instance->firmware_waiters, instance->status != UDSL_LOADING_FIRMWARE) < 0) - return -ERESTARTSYS; + if (ret < 0) + atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret); + else + atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n", + __func__, ret, buf[0], buf[1]); - return (instance->status == UDSL_LOADED_FIRMWARE) ? 0 : -EAGAIN; + return ret; } -/********** -** USB ** -**********/ - -static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code, - void *user_data) +static void speedtch_check_status(struct speedtch_instance_data *instance) { - struct speedtch_instance_data *instance = usb_get_intfdata(intf); + struct usbatm_data *usbatm = instance->usbatm; + struct atm_dev *atm_dev = usbatm->atm_dev; + unsigned char *buf = instance->scratch_buffer; + int down_speed, up_speed, ret; + unsigned char status; - dbg("speedtch_usb_ioctl entered"); + atm_dbg(usbatm, "%s entered\n", __func__); - if (!instance) { - dbg("speedtch_usb_ioctl: NULL instance!"); - return -ENODEV; + ret = speedtch_read_status(instance); + if (ret < 0) { + atm_warn(usbatm, "error %d fetching device status\n", ret); + instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY); + return; } - switch (code) { - case UDSL_IOCTL_LINE_UP: - instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND; - speedtch_got_firmware(instance, 1); - return (instance->u.status == UDSL_LOADED_FIRMWARE) ? 0 : -EIO; - case UDSL_IOCTL_LINE_DOWN: - instance->u.atm_dev->signal = ATM_PHY_SIG_LOST; - return 0; - default: - return -ENOTTY; + instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY); + + status = buf[OFFSET_7]; + + atm_dbg(usbatm, "%s: line state %02x\n", __func__, status); + + if ((status != instance->last_status) || !status) { + switch (status) { + case 0: + atm_dev->signal = ATM_PHY_SIG_LOST; + if (instance->last_status) + atm_info(usbatm, "ADSL line is down\n"); + /* It may never resync again unless we ask it to... */ + ret = speedtch_start_synchro(instance); + break; + + case 0x08: + atm_dev->signal = ATM_PHY_SIG_UNKNOWN; + atm_info(usbatm, "ADSL line is blocked?\n"); + break; + + case 0x10: + atm_dev->signal = ATM_PHY_SIG_LOST; + atm_info(usbatm, "ADSL line is synchronising\n"); + break; + + case 0x20: + down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8) + | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24); + up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8) + | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24); + + if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) { + down_speed >>= 16; + up_speed >>= 16; + } + + atm_dev->link_rate = down_speed * 1000 / 424; + atm_dev->signal = ATM_PHY_SIG_FOUND; + + atm_info(usbatm, + "ADSL line is up (%d kb/s down | %d kb/s up)\n", + down_speed, up_speed); + break; + + default: + atm_dev->signal = ATM_PHY_SIG_UNKNOWN; + atm_info(usbatm, "Unknown line state %02x\n", status); + break; + } + + instance->last_status = status; } } -static int speedtch_usb_probe(struct usb_interface *intf, - const struct usb_device_id *id) +static void speedtch_status_poll(unsigned long data) { - struct usb_device *dev = interface_to_usbdev(intf); - int ifnum = intf->altsetting->desc.bInterfaceNumber; - struct speedtch_instance_data *instance; - unsigned char mac_str[13]; - int ret, i; - char buf7[SIZE_7]; + struct speedtch_instance_data *instance = (void *)data; - dbg("speedtch_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d", - le16_to_cpu(dev->descriptor.idVendor), - le16_to_cpu(dev->descriptor.idProduct), ifnum); + schedule_work(&instance->status_checker); - if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) || - (ifnum != 1)) - return -ENODEV; + /* The following check is racy, but the race is harmless */ + if (instance->poll_delay < MAX_POLL_DELAY) + mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay)); + else + atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n"); +} - dbg("speedtch_usb_probe: device accepted"); +static void speedtch_resubmit_int(unsigned long data) +{ + struct speedtch_instance_data *instance = (void *)data; + struct urb *int_urb = instance->int_urb; + int ret; - /* instance init */ - instance = kmalloc(sizeof(*instance), GFP_KERNEL); - if (!instance) { - dbg("speedtch_usb_probe: no memory for instance data!"); - return -ENOMEM; + atm_dbg(instance->usbatm, "%s entered\n", __func__); + + if (int_urb) { + ret = usb_submit_urb(int_urb, GFP_ATOMIC); + if (!ret) + schedule_work(&instance->status_checker); + else { + atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret); + mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY)); + } } +} - memset(instance, 0, sizeof(struct speedtch_instance_data)); +static void speedtch_handle_int(struct urb *int_urb, struct pt_regs *regs) +{ + struct speedtch_instance_data *instance = int_urb->context; + struct usbatm_data *usbatm = instance->usbatm; + unsigned int count = int_urb->actual_length; + int ret = int_urb->status; - if ((ret = usb_set_interface(dev, 0, 0)) < 0) - goto fail; + /* The magic interrupt for "up state" */ + const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 }; + /* The magic interrupt for "down state" */ + const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 }; - if ((ret = usb_set_interface(dev, 2, 0)) < 0) + atm_dbg(usbatm, "%s entered\n", __func__); + + if (ret < 0) { + atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, ret); goto fail; + } - instance->u.data_endpoint = SPEEDTCH_ENDPOINT_DATA; - instance->u.firmware_wait = speedtch_firmware_wait; - instance->u.driver_name = speedtch_driver_name; + if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) { + del_timer(&instance->status_checker.timer); + atm_info(usbatm, "DSL line goes up\n"); + } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) { + atm_info(usbatm, "DSL line goes down\n"); + } else { + int i; - ret = udsl_instance_setup(dev, &instance->u); - if (ret) + atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count); + for (i = 0; i < count; i++) + printk(" %02x", instance->int_data[i]); + printk("\n"); goto fail; + } + + if ((int_urb = instance->int_urb)) { + ret = usb_submit_urb(int_urb, GFP_ATOMIC); + schedule_work(&instance->status_checker); + if (ret < 0) { + atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret); + goto fail; + } + } + + return; + +fail: + if ((int_urb = instance->int_urb)) + mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY)); +} - init_timer(&instance->poll_timer); - instance->poll_timer.function = speedtch_timer_poll; - instance->poll_timer.data = (unsigned long)instance; +static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev) +{ + struct usb_device *usb_dev = usbatm->usb_dev; + struct speedtch_instance_data *instance = usbatm->driver_data; + int i, ret; + unsigned char mac_str[13]; - INIT_WORK(&instance->poll_work, (void *)speedtch_poll_status, instance); + atm_dbg(usbatm, "%s entered\n", __func__); - /* set MAC address, it is stored in the serial number */ - memset(instance->u.atm_dev->esi, 0, sizeof(instance->u.atm_dev->esi)); - if (usb_string(dev, dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) { + if ((ret = usb_set_interface(usb_dev, 1, altsetting)) < 0) { + atm_dbg(usbatm, "%s: usb_set_interface returned %d!\n", __func__, ret); + return ret; + } + + /* Set MAC address, it is stored in the serial number */ + memset(atm_dev->esi, 0, sizeof(atm_dev->esi)); + if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) { for (i = 0; i < 6; i++) - instance->u.atm_dev->esi[i] = - (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1])); + atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1])); } - /* First check whether the modem already seems to be alive */ - ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), - 0x12, 0xc0, 0x07, 0x00, buf7, SIZE_7, 500); + /* Start modem synchronisation */ + ret = speedtch_start_synchro(instance); - if (ret == SIZE_7) { - dbg("firmware appears to be already loaded"); - speedtch_got_firmware(instance, 1); - speedtch_poll_status(instance); - } else { - speedtch_firmware_start(instance); + /* Set up interrupt endpoint */ + if (instance->int_urb) { + ret = usb_submit_urb(instance->int_urb, GFP_KERNEL); + if (ret < 0) { + /* Doesn't matter; we'll poll anyway */ + atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret); + usb_free_urb(instance->int_urb); + instance->int_urb = NULL; + } } - usb_set_intfdata(intf, instance); + /* Start status polling */ + mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000)); return 0; +} - fail: - kfree(instance); +static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev) +{ + struct speedtch_instance_data *instance = usbatm->driver_data; + struct urb *int_urb = instance->int_urb; + + atm_dbg(usbatm, "%s entered\n", __func__); + + del_timer_sync(&instance->status_checker.timer); + + /* + * Since resubmit_timer and int_urb can schedule themselves and + * each other, shutting them down correctly takes some care + */ + instance->int_urb = NULL; /* signal shutdown */ + mb(); + usb_kill_urb(int_urb); + del_timer_sync(&instance->resubmit_timer); + /* + * At this point, speedtch_handle_int and speedtch_resubmit_int + * can run or be running, but instance->int_urb == NULL means that + * they will not reschedule + */ + usb_kill_urb(int_urb); + del_timer_sync(&instance->resubmit_timer); + usb_free_urb(int_urb); - return -ENOMEM; + flush_scheduled_work(); } -static void speedtch_usb_disconnect(struct usb_interface *intf) + +/********** +** USB ** +**********/ + +static struct usb_device_id speedtch_usb_ids[] = { + {USB_DEVICE(0x06b9, 0x4061)}, + {} +}; + +MODULE_DEVICE_TABLE(usb, speedtch_usb_ids); + +static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *); + +static struct usb_driver speedtch_usb_driver = { + .owner = THIS_MODULE, + .name = speedtch_driver_name, + .probe = speedtch_usb_probe, + .disconnect = usbatm_usb_disconnect, + .id_table = speedtch_usb_ids +}; + +static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) { + struct usb_interface *cur_intf; + int i; + + for(i = 0; i < num_interfaces; i++) + if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) { + usb_set_intfdata(cur_intf, NULL); + usb_driver_release_interface(&speedtch_usb_driver, cur_intf); + } +} + +static int speedtch_bind(struct usbatm_data *usbatm, + struct usb_interface *intf, + const struct usb_device_id *id, + int *need_heavy_init) { - struct speedtch_instance_data *instance = usb_get_intfdata(intf); + struct usb_device *usb_dev = interface_to_usbdev(intf); + struct usb_interface *cur_intf; + struct speedtch_instance_data *instance; + int ifnum = intf->altsetting->desc.bInterfaceNumber; + int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces; + int i, ret; - dbg("speedtch_usb_disconnect entered"); + usb_dbg(usbatm, "%s entered\n", __func__); - if (!instance) { - dbg("speedtch_usb_disconnect: NULL instance!"); - return; + if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) { + usb_dbg(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass); + return -ENODEV; } -/*QQ need to handle disconnects on interface #2 while uploading firmware */ -/*QQ and what about interface #1? */ + /* claim all interfaces */ - if (instance->int_urb) { - struct urb *int_urb = instance->int_urb; - instance->int_urb = NULL; - wmb(); - usb_unlink_urb(int_urb); - usb_free_urb(int_urb); + for (i=0; i < num_interfaces; i++) { + cur_intf = usb_ifnum_to_if(usb_dev, i); + + if ((i != ifnum) && cur_intf) { + ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm); + + if (ret < 0) { + usb_dbg(usbatm, "%s: failed to claim interface %d (%d)\n", __func__, i, ret); + speedtch_release_interfaces(usb_dev, i); + return ret; + } + } } - instance->int_data[0] = 1; - del_timer_sync(&instance->poll_timer); - wmb(); - flush_scheduled_work(); + instance = kmalloc(sizeof(*instance), GFP_KERNEL); + + if (!instance) { + usb_dbg(usbatm, "%s: no memory for instance data!\n", __func__); + ret = -ENOMEM; + goto fail_release; + } + + memset(instance, 0, sizeof(struct speedtch_instance_data)); + + instance->usbatm = usbatm; + + INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance); + + instance->status_checker.timer.function = speedtch_status_poll; + instance->status_checker.timer.data = (unsigned long)instance; + instance->last_status = 0xff; + instance->poll_delay = MIN_POLL_DELAY; + + init_timer(&instance->resubmit_timer); + instance->resubmit_timer.function = speedtch_resubmit_int; + instance->resubmit_timer.data = (unsigned long)instance; - udsl_instance_disconnect(&instance->u); + instance->int_urb = usb_alloc_urb(0, GFP_KERNEL); - /* clean up */ - usb_set_intfdata(intf, NULL); - udsl_put_instance(&instance->u); + if (instance->int_urb) + usb_fill_int_urb(instance->int_urb, usb_dev, + usb_rcvintpipe(usb_dev, ENDPOINT_INT), + instance->int_data, sizeof(instance->int_data), + speedtch_handle_int, instance, 50); + else + usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__); + + /* check whether the modem already seems to be alive */ + ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), + 0x12, 0xc0, 0x07, 0x00, + instance->scratch_buffer + OFFSET_7, SIZE_7, 500); + + *need_heavy_init = (ret != SIZE_7); + + usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, need_heavy_init ? "not" : "already"); + + if (*need_heavy_init) + if ((ret = usb_reset_device(usb_dev)) < 0) + goto fail_free; + + usbatm->driver_data = instance; + + return 0; + +fail_free: + usb_free_urb(instance->int_urb); + kfree(instance); +fail_release: + speedtch_release_interfaces(usb_dev, num_interfaces); + return ret; +} + +static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf) +{ + struct usb_device *usb_dev = interface_to_usbdev(intf); + struct speedtch_instance_data *instance = usbatm->driver_data; + + usb_dbg(usbatm, "%s entered\n", __func__); + + speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces); + usb_free_urb(instance->int_urb); + kfree(instance); } + /*********** ** init ** ***********/ +static struct usbatm_driver speedtch_usbatm_driver = { + .owner = THIS_MODULE, + .driver_name = speedtch_driver_name, + .bind = speedtch_bind, + .heavy_init = speedtch_heavy_init, + .unbind = speedtch_unbind, + .atm_start = speedtch_atm_start, + .atm_stop = speedtch_atm_stop, + .in = ENDPOINT_DATA, + .out = ENDPOINT_DATA +}; + +static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) +{ + return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver); +} + static int __init speedtch_usb_init(void) { - dbg("speedtch_usb_init: driver version " DRIVER_VERSION); + dbg("%s: driver version %s", __func__, DRIVER_VERSION); return usb_register(&speedtch_usb_driver); } static void __exit speedtch_usb_cleanup(void) { - dbg("speedtch_usb_cleanup entered"); + dbg("%s", __func__); usb_deregister(&speedtch_usb_driver); } diff --git a/drivers/usb/atm/usb_atm.c b/drivers/usb/atm/usb_atm.c deleted file mode 100644 index a4cd4476d49a..000000000000 --- a/drivers/usb/atm/usb_atm.c +++ /dev/null @@ -1,1188 +0,0 @@ -/****************************************************************************** - * usb_atm.c - Generic USB xDSL driver core - * - * Copyright (C) 2001, Alcatel - * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas - * Copyright (C) 2004, David Woodhouse - * - * 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. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - ******************************************************************************/ - -/* - * Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@free.fr) - * - * 1.7+: - See the check-in logs - * - * 1.6: - No longer opens a connection if the firmware is not loaded - * - Added support for the speedtouch 330 - * - Removed the limit on the number of devices - * - Module now autoloads on device plugin - * - Merged relevant parts of sarlib - * - Replaced the kernel thread with a tasklet - * - New packet transmission code - * - Changed proc file contents - * - Fixed all known SMP races - * - Many fixes and cleanups - * - Various fixes by Oliver Neukum (oliver@neukum.name) - * - * 1.5A: - Version for inclusion in 2.5 series kernel - * - Modifications by Richard Purdie (rpurdie@rpsys.net) - * - made compatible with kernel 2.5.6 onwards by changing - * udsl_usb_send_data_context->urb to a pointer and adding code - * to alloc and free it - * - remove_wait_queue() added to udsl_atm_processqueue_thread() - * - * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL. - * (reported by stephen.robinson@zen.co.uk) - * - * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave() - * - unlink all active send urbs of a vcc that is being closed. - * - * 1.3.1: - added the version number - * - * 1.3: - Added multiple send urb support - * - fixed memory leak and vcc->tx_inuse starvation bug - * when not enough memory left in vcc. - * - * 1.2: - Fixed race condition in udsl_usb_send_data() - * 1.1: - Turned off packet debugging - * - */ - -#include <linux/module.h> -#include <linux/moduleparam.h> -#include <linux/kernel.h> -#include <linux/sched.h> -#include <linux/timer.h> -#include <linux/errno.h> -#include <linux/proc_fs.h> -#include <linux/slab.h> -#include <linux/wait.h> -#include <linux/list.h> -#include <asm/uaccess.h> -#include <linux/smp_lock.h> -#include <linux/interrupt.h> -#include <linux/atm.h> -#include <linux/atmdev.h> -#include <linux/crc32.h> -#include <linux/init.h> -#include <linux/firmware.h> - -#include "usb_atm.h" - -#ifdef VERBOSE_DEBUG -static int udsl_print_packet(const unsigned char *data, int len); -#define PACKETDEBUG(arg...) udsl_print_packet (arg) -#define vdbg(arg...) dbg (arg) -#else -#define PACKETDEBUG(arg...) -#define vdbg(arg...) -#endif - -#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" -#define DRIVER_VERSION "1.8" -#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION - -static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS; -static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS; -static unsigned int num_rcv_bufs = UDSL_DEFAULT_RCV_BUFS; -static unsigned int num_snd_bufs = UDSL_DEFAULT_SND_BUFS; -static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE; -static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE; - -module_param(num_rcv_urbs, uint, 0444); -MODULE_PARM_DESC(num_rcv_urbs, - "Number of urbs used for reception (range: 0-" - __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: " - __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")"); - -module_param(num_snd_urbs, uint, 0444); -MODULE_PARM_DESC(num_snd_urbs, - "Number of urbs used for transmission (range: 0-" - __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: " - __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")"); - -module_param(num_rcv_bufs, uint, 0444); -MODULE_PARM_DESC(num_rcv_bufs, - "Number of buffers used for reception (range: 0-" - __MODULE_STRING(UDSL_MAX_RCV_BUFS) ", default: " - __MODULE_STRING(UDSL_DEFAULT_RCV_BUFS) ")"); - -module_param(num_snd_bufs, uint, 0444); -MODULE_PARM_DESC(num_snd_bufs, - "Number of buffers used for transmission (range: 0-" - __MODULE_STRING(UDSL_MAX_SND_BUFS) ", default: " - __MODULE_STRING(UDSL_DEFAULT_SND_BUFS) ")"); - -module_param(rcv_buf_size, uint, 0444); -MODULE_PARM_DESC(rcv_buf_size, - "Size of the buffers used for reception (range: 0-" - __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: " - __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")"); - -module_param(snd_buf_size, uint, 0444); -MODULE_PARM_DESC(snd_buf_size, - "Size of the buffers used for transmission (range: 0-" - __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: " - __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")"); - -/* ATM */ - -static void udsl_atm_dev_close(struct atm_dev *dev); -static int udsl_atm_open(struct atm_vcc *vcc); -static void udsl_atm_close(struct atm_vcc *vcc); -static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg); -static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb); -static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page); - -static struct atmdev_ops udsl_atm_devops = { - .dev_close = udsl_atm_dev_close, - .open = udsl_atm_open, - .close = udsl_atm_close, - .ioctl = udsl_atm_ioctl, - .send = udsl_atm_send, - .proc_read = udsl_atm_proc_read, - .owner = THIS_MODULE, -}; - -/*********** -** misc ** -***********/ - -static inline void udsl_pop(struct atm_vcc *vcc, struct sk_buff *skb) -{ - if (vcc->pop) - vcc->pop(vcc, skb); - else - dev_kfree_skb(skb); -} - -/************* -** decode ** -*************/ - -static inline struct udsl_vcc_data *udsl_find_vcc(struct udsl_instance_data *instance, - short vpi, int vci) -{ - struct udsl_vcc_data *vcc; - - list_for_each_entry(vcc, &instance->vcc_list, list) - if ((vcc->vci == vci) && (vcc->vpi == vpi)) - return vcc; - return NULL; -} - -static void udsl_extract_cells(struct udsl_instance_data *instance, - unsigned char *source, unsigned int howmany) -{ - struct udsl_vcc_data *cached_vcc = NULL; - struct atm_vcc *vcc; - struct sk_buff *sarb; - struct udsl_vcc_data *vcc_data; - int cached_vci = 0; - unsigned int i; - int pti; - int vci; - short cached_vpi = 0; - short vpi; - - for (i = 0; i < howmany; - i++, source += ATM_CELL_SIZE + instance->rcv_padding) { - vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4); - vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4); - pti = (source[3] & 0x2) != 0; - - vdbg("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti); - - if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi)) - vcc_data = cached_vcc; - else if ((vcc_data = udsl_find_vcc(instance, vpi, vci))) { - cached_vcc = vcc_data; - cached_vpi = vpi; - cached_vci = vci; - } else { - dbg("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci); - continue; - } - - vcc = vcc_data->vcc; - sarb = vcc_data->sarb; - - if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) { - dbg("udsl_extract_cells: buffer overrun (sarb->len %u, vcc: 0x%p)!", sarb->len, vcc); - /* discard cells already received */ - skb_trim(sarb, 0); - } - - memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD); - __skb_put(sarb, ATM_CELL_PAYLOAD); - - if (pti) { - struct sk_buff *skb; - unsigned int length; - unsigned int pdu_length; - - length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5]; - - /* guard against overflow */ - if (length > ATM_MAX_AAL5_PDU) { - dbg("udsl_extract_cells: bogus length %u (vcc: 0x%p)!", length, vcc); - atomic_inc(&vcc->stats->rx_err); - goto out; - } - - pdu_length = UDSL_NUM_CELLS(length) * ATM_CELL_PAYLOAD; - - if (sarb->len < pdu_length) { - dbg("udsl_extract_cells: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!", pdu_length, sarb->len, vcc); - atomic_inc(&vcc->stats->rx_err); - goto out; - } - - if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) { - dbg("udsl_extract_cells: packet failed crc check (vcc: 0x%p)!", vcc); - atomic_inc(&vcc->stats->rx_err); - goto out; - } - - vdbg("udsl_extract_cells: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", length, pdu_length, vcc); - - if (!(skb = dev_alloc_skb(length))) { - dbg("udsl_extract_cells: no memory for skb (length: %u)!", length); - atomic_inc(&vcc->stats->rx_drop); - goto out; - } - - vdbg("udsl_extract_cells: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", skb, skb->truesize); - - if (!atm_charge(vcc, skb->truesize)) { - dbg("udsl_extract_cells: failed atm_charge (skb->truesize: %u)!", skb->truesize); - dev_kfree_skb(skb); - goto out; /* atm_charge increments rx_drop */ - } - - memcpy(skb->data, sarb->tail - pdu_length, length); - __skb_put(skb, length); - - vdbg("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize); - - PACKETDEBUG(skb->data, skb->len); - - vcc->push(vcc, skb); - - atomic_inc(&vcc->stats->rx); - out: - skb_trim(sarb, 0); - } - } -} - -/************* -** encode ** -*************/ - -static inline void udsl_fill_cell_header(unsigned char *target, struct atm_vcc *vcc) -{ - target[0] = vcc->vpi >> 4; - target[1] = (vcc->vpi << 4) | (vcc->vci >> 12); - target[2] = vcc->vci >> 4; - target[3] = vcc->vci << 4; - target[4] = 0xec; -} - -static const unsigned char zeros[ATM_CELL_PAYLOAD]; - -static void udsl_groom_skb(struct atm_vcc *vcc, struct sk_buff *skb) -{ - struct udsl_control *ctrl = UDSL_SKB(skb); - unsigned int zero_padding; - u32 crc; - - ctrl->atm_data.vcc = vcc; - - ctrl->num_cells = UDSL_NUM_CELLS(skb->len); - ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD; - - zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER; - - if (ctrl->num_entire + 1 < ctrl->num_cells) - ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER); - else - ctrl->pdu_padding = zero_padding; - - ctrl->aal5_trailer[0] = 0; /* UU = 0 */ - ctrl->aal5_trailer[1] = 0; /* CPI = 0 */ - ctrl->aal5_trailer[2] = skb->len >> 8; - ctrl->aal5_trailer[3] = skb->len; - - crc = crc32_be(~0, skb->data, skb->len); - crc = crc32_be(crc, zeros, zero_padding); - crc = crc32_be(crc, ctrl->aal5_trailer, 4); - crc = ~crc; - - ctrl->aal5_trailer[4] = crc >> 24; - ctrl->aal5_trailer[5] = crc >> 16; - ctrl->aal5_trailer[6] = crc >> 8; - ctrl->aal5_trailer[7] = crc; -} - -static unsigned int udsl_write_cells(struct udsl_instance_data *instance, - unsigned int howmany, struct sk_buff *skb, - unsigned char **target_p) -{ - struct udsl_control *ctrl = UDSL_SKB(skb); - unsigned char *target = *target_p; - unsigned int nc, ne, i; - - vdbg("udsl_write_cells: howmany=%u, skb->len=%d, num_cells=%u, num_entire=%u, pdu_padding=%u", howmany, skb->len, ctrl->num_cells, ctrl->num_entire, ctrl->pdu_padding); - - nc = ctrl->num_cells; - ne = min(howmany, ctrl->num_entire); - - for (i = 0; i < ne; i++) { - udsl_fill_cell_header(target, ctrl->atm_data.vcc); - target += ATM_CELL_HEADER; - memcpy(target, skb->data, ATM_CELL_PAYLOAD); - target += ATM_CELL_PAYLOAD; - if (instance->snd_padding) { - memset(target, 0, instance->snd_padding); - target += instance->snd_padding; - } - __skb_pull(skb, ATM_CELL_PAYLOAD); - } - - ctrl->num_entire -= ne; - - if (!(ctrl->num_cells -= ne) || !(howmany -= ne)) - goto out; - - udsl_fill_cell_header(target, ctrl->atm_data.vcc); - target += ATM_CELL_HEADER; - memcpy(target, skb->data, skb->len); - target += skb->len; - __skb_pull(skb, skb->len); - memset(target, 0, ctrl->pdu_padding); - target += ctrl->pdu_padding; - - if (--ctrl->num_cells) { - if (!--howmany) { - ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER; - goto out; - } - - if (instance->snd_padding) { - memset(target, 0, instance->snd_padding); - target += instance->snd_padding; - } - udsl_fill_cell_header(target, ctrl->atm_data.vcc); - target += ATM_CELL_HEADER; - memset(target, 0, ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER); - target += ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER; - - --ctrl->num_cells; - UDSL_ASSERT(!ctrl->num_cells); - } - - memcpy(target, ctrl->aal5_trailer, ATM_AAL5_TRAILER); - target += ATM_AAL5_TRAILER; - /* set pti bit in last cell */ - *(target + 3 - ATM_CELL_SIZE) |= 0x2; - if (instance->snd_padding) { - memset(target, 0, instance->snd_padding); - target += instance->snd_padding; - } - out: - *target_p = target; - return nc - ctrl->num_cells; -} - -/************** -** receive ** -**************/ - -static void udsl_complete_receive(struct urb *urb, struct pt_regs *regs) -{ - struct udsl_receive_buffer *buf; - struct udsl_instance_data *instance; - struct udsl_receiver *rcv; - unsigned long flags; - - if (!urb || !(rcv = urb->context)) { - dbg("udsl_complete_receive: bad urb!"); - return; - } - - instance = rcv->instance; - buf = rcv->buffer; - - buf->filled_cells = urb->actual_length / (ATM_CELL_SIZE + instance->rcv_padding); - - vdbg("udsl_complete_receive: urb 0x%p, status %d, actual_length %d, filled_cells %u, rcv 0x%p, buf 0x%p", urb, urb->status, urb->actual_length, buf->filled_cells, rcv, buf); - - UDSL_ASSERT(buf->filled_cells <= rcv_buf_size); - - /* may not be in_interrupt() */ - spin_lock_irqsave(&instance->receive_lock, flags); - list_add(&rcv->list, &instance->spare_receivers); - list_add_tail(&buf->list, &instance->filled_receive_buffers); - if (likely(!urb->status)) - tasklet_schedule(&instance->receive_tasklet); - spin_unlock_irqrestore(&instance->receive_lock, flags); -} - -static void udsl_process_receive(unsigned long data) -{ - struct udsl_receive_buffer *buf; - struct udsl_instance_data *instance = (struct udsl_instance_data *)data; - struct udsl_receiver *rcv; - int err; - - made_progress: - while (!list_empty(&instance->spare_receive_buffers)) { - spin_lock_irq(&instance->receive_lock); - if (list_empty(&instance->spare_receivers)) { - spin_unlock_irq(&instance->receive_lock); - break; - } - rcv = list_entry(instance->spare_receivers.next, - struct udsl_receiver, list); - list_del(&rcv->list); - spin_unlock_irq(&instance->receive_lock); - - buf = list_entry(instance->spare_receive_buffers.next, - struct udsl_receive_buffer, list); - list_del(&buf->list); - - rcv->buffer = buf; - - usb_fill_bulk_urb(rcv->urb, instance->usb_dev, - usb_rcvbulkpipe(instance->usb_dev, instance->data_endpoint), - buf->base, - rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding), - udsl_complete_receive, rcv); - - vdbg("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p", - rcv->urb, rcv, buf); - - if ((err = usb_submit_urb(rcv->urb, GFP_ATOMIC)) < 0) { - dbg("udsl_process_receive: urb submission failed (%d)!", err); - list_add(&buf->list, &instance->spare_receive_buffers); - spin_lock_irq(&instance->receive_lock); - list_add(&rcv->list, &instance->spare_receivers); - spin_unlock_irq(&instance->receive_lock); - break; - } - } - - spin_lock_irq(&instance->receive_lock); - if (list_empty(&instance->filled_receive_buffers)) { - spin_unlock_irq(&instance->receive_lock); - return; /* done - no more buffers */ - } - buf = list_entry(instance->filled_receive_buffers.next, - struct udsl_receive_buffer, list); - list_del(&buf->list); - spin_unlock_irq(&instance->receive_lock); - - vdbg("udsl_process_receive: processing buf 0x%p", buf); - udsl_extract_cells(instance, buf->base, buf->filled_cells); - list_add(&buf->list, &instance->spare_receive_buffers); - goto made_progress; -} - -/*********** -** send ** -***********/ - -static void udsl_complete_send(struct urb *urb, struct pt_regs *regs) -{ - struct udsl_instance_data *instance; - struct udsl_sender *snd; - unsigned long flags; - - if (!urb || !(snd = urb->context) || !(instance = snd->instance)) { - dbg("udsl_complete_send: bad urb!"); - return; - } - - vdbg("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb, - urb->status, snd, snd->buffer); - - /* may not be in_interrupt() */ - spin_lock_irqsave(&instance->send_lock, flags); - list_add(&snd->list, &instance->spare_senders); - list_add(&snd->buffer->list, &instance->spare_send_buffers); - tasklet_schedule(&instance->send_tasklet); - spin_unlock_irqrestore(&instance->send_lock, flags); -} - -static void udsl_process_send(unsigned long data) -{ - struct udsl_send_buffer *buf; - struct udsl_instance_data *instance = (struct udsl_instance_data *)data; - struct sk_buff *skb; - struct udsl_sender *snd; - int err; - unsigned int num_written; - - made_progress: - spin_lock_irq(&instance->send_lock); - while (!list_empty(&instance->spare_senders)) { - if (!list_empty(&instance->filled_send_buffers)) { - buf = list_entry(instance->filled_send_buffers.next, - struct udsl_send_buffer, list); - list_del(&buf->list); - } else if ((buf = instance->current_buffer)) { - instance->current_buffer = NULL; - } else /* all buffers empty */ - break; - - snd = list_entry(instance->spare_senders.next, - struct udsl_sender, list); - list_del(&snd->list); - spin_unlock_irq(&instance->send_lock); - - snd->buffer = buf; - usb_fill_bulk_urb(snd->urb, instance->usb_dev, - usb_sndbulkpipe(instance->usb_dev, instance->data_endpoint), - buf->base, - (snd_buf_size - buf->free_cells) * (ATM_CELL_SIZE + instance->snd_padding), - udsl_complete_send, snd); - - vdbg("udsl_process_send: submitting urb 0x%p (%d cells), snd 0x%p, buf 0x%p", - snd->urb, snd_buf_size - buf->free_cells, snd, buf); - - if ((err = usb_submit_urb(snd->urb, GFP_ATOMIC)) < 0) { - dbg("udsl_process_send: urb submission failed (%d)!", err); - spin_lock_irq(&instance->send_lock); - list_add(&snd->list, &instance->spare_senders); - spin_unlock_irq(&instance->send_lock); - list_add(&buf->list, &instance->filled_send_buffers); - return; /* bail out */ - } - - spin_lock_irq(&instance->send_lock); - } /* while */ - spin_unlock_irq(&instance->send_lock); - - if (!instance->current_skb) - instance->current_skb = skb_dequeue(&instance->sndqueue); - if (!instance->current_skb) - return; /* done - no more skbs */ - - skb = instance->current_skb; - - if (!(buf = instance->current_buffer)) { - spin_lock_irq(&instance->send_lock); - if (list_empty(&instance->spare_send_buffers)) { - instance->current_buffer = NULL; - spin_unlock_irq(&instance->send_lock); - return; /* done - no more buffers */ - } - buf = list_entry(instance->spare_send_buffers.next, - struct udsl_send_buffer, list); - list_del(&buf->list); - spin_unlock_irq(&instance->send_lock); - - buf->free_start = buf->base; - buf->free_cells = snd_buf_size; - - instance->current_buffer = buf; - } - - num_written = udsl_write_cells(instance, buf->free_cells, skb, &buf->free_start); - - vdbg("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p", - num_written, skb, buf); - - if (!(buf->free_cells -= num_written)) { - list_add_tail(&buf->list, &instance->filled_send_buffers); - instance->current_buffer = NULL; - } - - vdbg("udsl_process_send: buffer contains %d cells, %d left", - snd_buf_size - buf->free_cells, buf->free_cells); - - if (!UDSL_SKB(skb)->num_cells) { - struct atm_vcc *vcc = UDSL_SKB(skb)->atm_data.vcc; - - udsl_pop(vcc, skb); - instance->current_skb = NULL; - - atomic_inc(&vcc->stats->tx); - } - - goto made_progress; -} - -static void udsl_cancel_send(struct udsl_instance_data *instance, - struct atm_vcc *vcc) -{ - struct sk_buff *skb, *n; - - dbg("udsl_cancel_send entered"); - spin_lock_irq(&instance->sndqueue.lock); - for (skb = instance->sndqueue.next, n = skb->next; - skb != (struct sk_buff *)&instance->sndqueue; - skb = n, n = skb->next) - if (UDSL_SKB(skb)->atm_data.vcc == vcc) { - dbg("udsl_cancel_send: popping skb 0x%p", skb); - __skb_unlink(skb, &instance->sndqueue); - udsl_pop(vcc, skb); - } - spin_unlock_irq(&instance->sndqueue.lock); - - tasklet_disable(&instance->send_tasklet); - if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm_data.vcc == vcc)) { - dbg("udsl_cancel_send: popping current skb (0x%p)", skb); - instance->current_skb = NULL; - udsl_pop(vcc, skb); - } - tasklet_enable(&instance->send_tasklet); - dbg("udsl_cancel_send done"); -} - -static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb) -{ - struct udsl_instance_data *instance = vcc->dev->dev_data; - int err; - - vdbg("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len); - - if (!instance) { - dbg("udsl_atm_send: NULL data!"); - err = -ENODEV; - goto fail; - } - - if (vcc->qos.aal != ATM_AAL5) { - dbg("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal); - err = -EINVAL; - goto fail; - } - - if (skb->len > ATM_MAX_AAL5_PDU) { - dbg("udsl_atm_send: packet too long (%d vs %d)!", skb->len, - ATM_MAX_AAL5_PDU); - err = -EINVAL; - goto fail; - } - - PACKETDEBUG(skb->data, skb->len); - - udsl_groom_skb(vcc, skb); - skb_queue_tail(&instance->sndqueue, skb); - tasklet_schedule(&instance->send_tasklet); - - return 0; - - fail: - udsl_pop(vcc, skb); - return err; -} - -/******************** -** bean counting ** -********************/ - -static void udsl_destroy_instance(struct kref *kref) -{ - struct udsl_instance_data *instance = - container_of(kref, struct udsl_instance_data, refcount); - - tasklet_kill(&instance->receive_tasklet); - tasklet_kill(&instance->send_tasklet); - usb_put_dev(instance->usb_dev); - kfree(instance); -} - -void udsl_get_instance(struct udsl_instance_data *instance) -{ - kref_get(&instance->refcount); -} - -void udsl_put_instance(struct udsl_instance_data *instance) -{ - kref_put(&instance->refcount, udsl_destroy_instance); -} - -/********** -** ATM ** -**********/ - -static void udsl_atm_dev_close(struct atm_dev *dev) -{ - struct udsl_instance_data *instance = dev->dev_data; - - dev->dev_data = NULL; - udsl_put_instance(instance); -} - -static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page) -{ - struct udsl_instance_data *instance = atm_dev->dev_data; - int left = *pos; - - if (!instance) { - dbg("udsl_atm_proc_read: NULL instance!"); - return -ENODEV; - } - - if (!left--) - return sprintf(page, "%s\n", instance->description); - - if (!left--) - return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", - atm_dev->esi[0], atm_dev->esi[1], - atm_dev->esi[2], atm_dev->esi[3], - atm_dev->esi[4], atm_dev->esi[5]); - - if (!left--) - return sprintf(page, - "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n", - atomic_read(&atm_dev->stats.aal5.tx), - atomic_read(&atm_dev->stats.aal5.tx_err), - atomic_read(&atm_dev->stats.aal5.rx), - atomic_read(&atm_dev->stats.aal5.rx_err), - atomic_read(&atm_dev->stats.aal5.rx_drop)); - - if (!left--) { - switch (atm_dev->signal) { - case ATM_PHY_SIG_FOUND: - sprintf(page, "Line up"); - break; - case ATM_PHY_SIG_LOST: - sprintf(page, "Line down"); - break; - default: - sprintf(page, "Line state unknown"); - break; - } - - if (instance->usb_dev->state == USB_STATE_NOTATTACHED) - strcat(page, ", disconnected\n"); - else { - if (instance->status == UDSL_LOADED_FIRMWARE) - strcat(page, ", firmware loaded\n"); - else if (instance->status == UDSL_LOADING_FIRMWARE) - strcat(page, ", firmware loading\n"); - else - strcat(page, ", no firmware\n"); - } - - return strlen(page); - } - - return 0; -} - -static int udsl_atm_open(struct atm_vcc *vcc) -{ - struct udsl_instance_data *instance = vcc->dev->dev_data; - struct udsl_vcc_data *new; - unsigned int max_pdu; - int vci = vcc->vci; - short vpi = vcc->vpi; - int err; - - dbg("udsl_atm_open: vpi %hd, vci %d", vpi, vci); - - if (!instance) { - dbg("udsl_atm_open: NULL data!"); - return -ENODEV; - } - - /* only support AAL5 */ - if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) - || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) { - dbg("udsl_atm_open: unsupported ATM type %d!", vcc->qos.aal); - return -EINVAL; - } - - if (instance->firmware_wait && - (err = instance->firmware_wait(instance)) < 0) { - dbg("udsl_atm_open: firmware not loaded (%d)!", err); - return err; - } - - down(&instance->serialize); /* vs self, udsl_atm_close */ - - if (udsl_find_vcc(instance, vpi, vci)) { - dbg("udsl_atm_open: %hd/%d already in use!", vpi, vci); - up(&instance->serialize); - return -EADDRINUSE; - } - - if (!(new = kmalloc(sizeof(struct udsl_vcc_data), GFP_KERNEL))) { - dbg("udsl_atm_open: no memory for vcc_data!"); - up(&instance->serialize); - return -ENOMEM; - } - - memset(new, 0, sizeof(struct udsl_vcc_data)); - new->vcc = vcc; - new->vpi = vpi; - new->vci = vci; - - /* udsl_extract_cells requires at least one cell */ - max_pdu = max(1, UDSL_NUM_CELLS(vcc->qos.rxtp.max_sdu)) * ATM_CELL_PAYLOAD; - if (!(new->sarb = alloc_skb(max_pdu, GFP_KERNEL))) { - dbg("udsl_atm_open: no memory for SAR buffer!"); - kfree(new); - up(&instance->serialize); - return -ENOMEM; - } - - vcc->dev_data = new; - - tasklet_disable(&instance->receive_tasklet); - list_add(&new->list, &instance->vcc_list); - tasklet_enable(&instance->receive_tasklet); - - set_bit(ATM_VF_ADDR, &vcc->flags); - set_bit(ATM_VF_PARTIAL, &vcc->flags); - set_bit(ATM_VF_READY, &vcc->flags); - - up(&instance->serialize); - - tasklet_schedule(&instance->receive_tasklet); - - dbg("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, max_pdu); - - return 0; -} - -static void udsl_atm_close(struct atm_vcc *vcc) -{ - struct udsl_instance_data *instance = vcc->dev->dev_data; - struct udsl_vcc_data *vcc_data = vcc->dev_data; - - dbg("udsl_atm_close called"); - - if (!instance || !vcc_data) { - dbg("udsl_atm_close: NULL data!"); - return; - } - - dbg("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d", - vcc_data, vcc_data->vpi, vcc_data->vci); - - udsl_cancel_send(instance, vcc); - - down(&instance->serialize); /* vs self, udsl_atm_open */ - - tasklet_disable(&instance->receive_tasklet); - list_del(&vcc_data->list); - tasklet_enable(&instance->receive_tasklet); - - kfree_skb(vcc_data->sarb); - vcc_data->sarb = NULL; - - kfree(vcc_data); - vcc->dev_data = NULL; - - vcc->vpi = ATM_VPI_UNSPEC; - vcc->vci = ATM_VCI_UNSPEC; - clear_bit(ATM_VF_READY, &vcc->flags); - clear_bit(ATM_VF_PARTIAL, &vcc->flags); - clear_bit(ATM_VF_ADDR, &vcc->flags); - - up(&instance->serialize); - - dbg("udsl_atm_close successful"); -} - -static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, - void __user * arg) -{ - switch (cmd) { - case ATM_QUERYLOOP: - return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0; - default: - return -ENOIOCTLCMD; - } -} - -/********** -** USB ** -**********/ - -int udsl_instance_setup(struct usb_device *dev, - struct udsl_instance_data *instance) -{ - char *buf; - int i, length; - - kref_init(&instance->refcount); /* one for USB */ - udsl_get_instance(instance); /* one for ATM */ - - init_MUTEX(&instance->serialize); - - instance->usb_dev = dev; - - INIT_LIST_HEAD(&instance->vcc_list); - - instance->status = UDSL_NO_FIRMWARE; - init_waitqueue_head(&instance->firmware_waiters); - - spin_lock_init(&instance->receive_lock); - INIT_LIST_HEAD(&instance->spare_receivers); - INIT_LIST_HEAD(&instance->filled_receive_buffers); - - tasklet_init(&instance->receive_tasklet, udsl_process_receive, (unsigned long)instance); - INIT_LIST_HEAD(&instance->spare_receive_buffers); - - skb_queue_head_init(&instance->sndqueue); - - spin_lock_init(&instance->send_lock); - INIT_LIST_HEAD(&instance->spare_senders); - INIT_LIST_HEAD(&instance->spare_send_buffers); - - tasklet_init(&instance->send_tasklet, udsl_process_send, - (unsigned long)instance); - INIT_LIST_HEAD(&instance->filled_send_buffers); - - /* receive init */ - for (i = 0; i < num_rcv_urbs; i++) { - struct udsl_receiver *rcv = &(instance->receivers[i]); - - if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { - dbg("udsl_usb_probe: no memory for receive urb %d!", i); - goto fail; - } - - rcv->instance = instance; - - list_add(&rcv->list, &instance->spare_receivers); - } - - for (i = 0; i < num_rcv_bufs; i++) { - struct udsl_receive_buffer *buf = - &(instance->receive_buffers[i]); - - buf->base = kmalloc(rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding), - GFP_KERNEL); - if (!buf->base) { - dbg("udsl_usb_probe: no memory for receive buffer %d!", i); - goto fail; - } - - list_add(&buf->list, &instance->spare_receive_buffers); - } - - /* send init */ - for (i = 0; i < num_snd_urbs; i++) { - struct udsl_sender *snd = &(instance->senders[i]); - - if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) { - dbg("udsl_usb_probe: no memory for send urb %d!", i); - goto fail; - } - - snd->instance = instance; - - list_add(&snd->list, &instance->spare_senders); - } - - for (i = 0; i < num_snd_bufs; i++) { - struct udsl_send_buffer *buf = &(instance->send_buffers[i]); - - buf->base = kmalloc(snd_buf_size * (ATM_CELL_SIZE + instance->snd_padding), - GFP_KERNEL); - if (!buf->base) { - dbg("udsl_usb_probe: no memory for send buffer %d!", i); - goto fail; - } - - list_add(&buf->list, &instance->spare_send_buffers); - } - - /* ATM init */ - instance->atm_dev = atm_dev_register(instance->driver_name, - &udsl_atm_devops, -1, NULL); - if (!instance->atm_dev) { - dbg("udsl_usb_probe: failed to register ATM device!"); - goto fail; - } - - instance->atm_dev->ci_range.vpi_bits = ATM_CI_MAX; - instance->atm_dev->ci_range.vci_bits = ATM_CI_MAX; - instance->atm_dev->signal = ATM_PHY_SIG_UNKNOWN; - - /* temp init ATM device, set to 128kbit */ - instance->atm_dev->link_rate = 128 * 1000 / 424; - - /* device description */ - buf = instance->description; - length = sizeof(instance->description); - - if ((i = usb_string(dev, dev->descriptor.iProduct, buf, length)) < 0) - goto finish; - - buf += i; - length -= i; - - i = scnprintf(buf, length, " ("); - buf += i; - length -= i; - - if (length <= 0 || (i = usb_make_path(dev, buf, length)) < 0) - goto finish; - - buf += i; - length -= i; - - snprintf(buf, length, ")"); - - finish: - /* ready for ATM callbacks */ - wmb(); - instance->atm_dev->dev_data = instance; - - usb_get_dev(dev); - - return 0; - - fail: - for (i = 0; i < num_snd_bufs; i++) - kfree(instance->send_buffers[i].base); - - for (i = 0; i < num_snd_urbs; i++) - usb_free_urb(instance->senders[i].urb); - - for (i = 0; i < num_rcv_bufs; i++) - kfree(instance->receive_buffers[i].base); - - for (i = 0; i < num_rcv_urbs; i++) - usb_free_urb(instance->receivers[i].urb); - - return -ENOMEM; -} - -void udsl_instance_disconnect(struct udsl_instance_data *instance) -{ - int i; - - dbg("udsl_instance_disconnect entered"); - - if (!instance) { - dbg("udsl_instance_disconnect: NULL instance!"); - return; - } - - /* receive finalize */ - tasklet_disable(&instance->receive_tasklet); - - for (i = 0; i < num_rcv_urbs; i++) - usb_kill_urb(instance->receivers[i].urb); - - /* no need to take the spinlock */ - INIT_LIST_HEAD(&instance->filled_receive_buffers); - INIT_LIST_HEAD(&instance->spare_receive_buffers); - - tasklet_enable(&instance->receive_tasklet); - - for (i = 0; i < num_rcv_urbs; i++) - usb_free_urb(instance->receivers[i].urb); - - for (i = 0; i < num_rcv_bufs; i++) - kfree(instance->receive_buffers[i].base); - - /* send finalize */ - tasklet_disable(&instance->send_tasklet); - - for (i = 0; i < num_snd_urbs; i++) - usb_kill_urb(instance->senders[i].urb); - - /* no need to take the spinlock */ - INIT_LIST_HEAD(&instance->spare_senders); - INIT_LIST_HEAD(&instance->spare_send_buffers); - instance->current_buffer = NULL; - - tasklet_enable(&instance->send_tasklet); - - for (i = 0; i < num_snd_urbs; i++) - usb_free_urb(instance->senders[i].urb); - - for (i = 0; i < num_snd_bufs; i++) - kfree(instance->send_buffers[i].base); - - /* ATM finalize */ - shutdown_atm_dev(instance->atm_dev); -} - -EXPORT_SYMBOL_GPL(udsl_get_instance); -EXPORT_SYMBOL_GPL(udsl_put_instance); -EXPORT_SYMBOL_GPL(udsl_instance_setup); -EXPORT_SYMBOL_GPL(udsl_instance_disconnect); - -/*********** -** init ** -***********/ - -static int __init udsl_usb_init(void) -{ - dbg("udsl_usb_init: driver version " DRIVER_VERSION); - - if (sizeof(struct udsl_control) > sizeof(((struct sk_buff *) 0)->cb)) { - printk(KERN_ERR __FILE__ ": unusable with this kernel!\n"); - return -EIO; - } - - if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) - || (num_snd_urbs > UDSL_MAX_SND_URBS) - || (num_rcv_bufs > UDSL_MAX_RCV_BUFS) - || (num_snd_bufs > UDSL_MAX_SND_BUFS) - || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) - || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE)) - return -EINVAL; - - return 0; -} - -static void __exit udsl_usb_exit(void) -{ -} - -module_init(udsl_usb_init); -module_exit(udsl_usb_exit); - -MODULE_AUTHOR(DRIVER_AUTHOR); -MODULE_DESCRIPTION(DRIVER_DESC); -MODULE_LICENSE("GPL"); -MODULE_VERSION(DRIVER_VERSION); - -/************ -** debug ** -************/ - -#ifdef VERBOSE_DEBUG -static int udsl_print_packet(const unsigned char *data, int len) -{ - unsigned char buffer[256]; - int i = 0, j = 0; - - for (i = 0; i < len;) { - buffer[0] = '\0'; - sprintf(buffer, "%.3d :", i); - for (j = 0; (j < 16) && (i < len); j++, i++) { - sprintf(buffer, "%s %2.2x", buffer, data[i]); - } - dbg("%s", buffer); - } - return i; -} -#endif diff --git a/drivers/usb/atm/usb_atm.h b/drivers/usb/atm/usb_atm.h deleted file mode 100644 index cf8c53283530..000000000000 --- a/drivers/usb/atm/usb_atm.h +++ /dev/null @@ -1,176 +0,0 @@ -/****************************************************************************** - * usb_atm.h - Generic USB xDSL driver core - * - * Copyright (C) 2001, Alcatel - * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas - * Copyright (C) 2004, David Woodhouse - * - * 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. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - ******************************************************************************/ - -#include <linux/config.h> -#include <linux/list.h> -#include <linux/kref.h> -#include <linux/atm.h> -#include <linux/atmdev.h> -#include <asm/semaphore.h> - -/* -#define DEBUG -#define VERBOSE_DEBUG -*/ - -#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG) -# define DEBUG -#endif - -#include <linux/usb.h> - -#ifdef DEBUG -#define UDSL_ASSERT(x) BUG_ON(!(x)) -#else -#define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '" #x "' at line %d", __LINE__); } while(0) -#endif - -#define UDSL_MAX_RCV_URBS 4 -#define UDSL_MAX_SND_URBS 4 -#define UDSL_MAX_RCV_BUFS 8 -#define UDSL_MAX_SND_BUFS 8 -#define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */ -#define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */ -#define UDSL_DEFAULT_RCV_URBS 2 -#define UDSL_DEFAULT_SND_URBS 2 -#define UDSL_DEFAULT_RCV_BUFS 4 -#define UDSL_DEFAULT_SND_BUFS 4 -#define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */ -#define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */ - -#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD) -#define UDSL_NUM_CELLS(x) (((x) + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD) - -/* receive */ - -struct udsl_receive_buffer { - struct list_head list; - unsigned char *base; - unsigned int filled_cells; -}; - -struct udsl_receiver { - struct list_head list; - struct udsl_receive_buffer *buffer; - struct urb *urb; - struct udsl_instance_data *instance; -}; - -struct udsl_vcc_data { - /* vpi/vci lookup */ - struct list_head list; - short vpi; - int vci; - struct atm_vcc *vcc; - - /* raw cell reassembly */ - struct sk_buff *sarb; -}; - -/* send */ - -struct udsl_send_buffer { - struct list_head list; - unsigned char *base; - unsigned char *free_start; - unsigned int free_cells; -}; - -struct udsl_sender { - struct list_head list; - struct udsl_send_buffer *buffer; - struct urb *urb; - struct udsl_instance_data *instance; -}; - -struct udsl_control { - struct atm_skb_data atm_data; - unsigned int num_cells; - unsigned int num_entire; - unsigned int pdu_padding; - unsigned char aal5_trailer[ATM_AAL5_TRAILER]; -}; - -#define UDSL_SKB(x) ((struct udsl_control *)(x)->cb) - -/* main driver data */ - -enum udsl_status { - UDSL_NO_FIRMWARE, - UDSL_LOADING_FIRMWARE, - UDSL_LOADED_FIRMWARE -}; - -struct udsl_instance_data { - struct kref refcount; - struct semaphore serialize; - - /* USB device part */ - struct usb_device *usb_dev; - char description[64]; - int data_endpoint; - int snd_padding; - int rcv_padding; - const char *driver_name; - - /* ATM device part */ - struct atm_dev *atm_dev; - struct list_head vcc_list; - - /* firmware */ - int (*firmware_wait) (struct udsl_instance_data *); - enum udsl_status status; - wait_queue_head_t firmware_waiters; - - /* receive */ - struct udsl_receiver receivers[UDSL_MAX_RCV_URBS]; - struct udsl_receive_buffer receive_buffers[UDSL_MAX_RCV_BUFS]; - - spinlock_t receive_lock; - struct list_head spare_receivers; - struct list_head filled_receive_buffers; - - struct tasklet_struct receive_tasklet; - struct list_head spare_receive_buffers; - - /* send */ - struct udsl_sender senders[UDSL_MAX_SND_URBS]; - struct udsl_send_buffer send_buffers[UDSL_MAX_SND_BUFS]; - - struct sk_buff_head sndqueue; - - spinlock_t send_lock; - struct list_head spare_senders; - struct list_head spare_send_buffers; - - struct tasklet_struct send_tasklet; - struct sk_buff *current_skb; /* being emptied */ - struct udsl_send_buffer *current_buffer; /* being filled */ - struct list_head filled_send_buffers; -}; - -extern int udsl_instance_setup(struct usb_device *dev, - struct udsl_instance_data *instance); -extern void udsl_instance_disconnect(struct udsl_instance_data *instance); -extern void udsl_get_instance(struct udsl_instance_data *instance); -extern void udsl_put_instance(struct udsl_instance_data *instance); diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c new file mode 100644 index 000000000000..bb1db1959854 --- /dev/null +++ b/drivers/usb/atm/usbatm.c @@ -0,0 +1,1230 @@ +/****************************************************************************** + * usbatm.c - Generic USB xDSL driver core + * + * Copyright (C) 2001, Alcatel + * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas + * Copyright (C) 2004, David Woodhouse, Roman Kagan + * + * 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ******************************************************************************/ + +/* + * Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse + * + * 1.7+: - See the check-in logs + * + * 1.6: - No longer opens a connection if the firmware is not loaded + * - Added support for the speedtouch 330 + * - Removed the limit on the number of devices + * - Module now autoloads on device plugin + * - Merged relevant parts of sarlib + * - Replaced the kernel thread with a tasklet + * - New packet transmission code + * - Changed proc file contents + * - Fixed all known SMP races + * - Many fixes and cleanups + * - Various fixes by Oliver Neukum (oliver@neukum.name) + * + * 1.5A: - Version for inclusion in 2.5 series kernel + * - Modifications by Richard Purdie (rpurdie@rpsys.net) + * - made compatible with kernel 2.5.6 onwards by changing + * usbatm_usb_send_data_context->urb to a pointer and adding code + * to alloc and free it + * - remove_wait_queue() added to usbatm_atm_processqueue_thread() + * + * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL. + * (reported by stephen.robinson@zen.co.uk) + * + * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave() + * - unlink all active send urbs of a vcc that is being closed. + * + * 1.3.1: - added the version number + * + * 1.3: - Added multiple send urb support + * - fixed memory leak and vcc->tx_inuse starvation bug + * when not enough memory left in vcc. + * + * 1.2: - Fixed race condition in usbatm_usb_send_data() + * 1.1: - Turned off packet debugging + * + */ + +#include "usbatm.h" + +#include <asm/uaccess.h> +#include <linux/crc32.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/moduleparam.h> +#include <linux/proc_fs.h> +#include <linux/sched.h> +#include <linux/signal.h> +#include <linux/slab.h> +#include <linux/smp_lock.h> +#include <linux/stat.h> +#include <linux/timer.h> +#include <linux/wait.h> + +#ifdef VERBOSE_DEBUG +static int usbatm_print_packet(const unsigned char *data, int len); +#define PACKETDEBUG(arg...) usbatm_print_packet (arg) +#define vdbg(arg...) dbg (arg) +#else +#define PACKETDEBUG(arg...) +#define vdbg(arg...) +#endif + +#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" +#define DRIVER_VERSION "1.9" +#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION + +static const char usbatm_driver_name[] = "usbatm"; + +#define UDSL_MAX_RCV_URBS 16 +#define UDSL_MAX_SND_URBS 16 +#define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */ +#define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */ +#define UDSL_DEFAULT_RCV_URBS 4 +#define UDSL_DEFAULT_SND_URBS 4 +#define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */ +#define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */ + +#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD) + +#define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */ + +static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS; +static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS; +static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE; +static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE; + +module_param(num_rcv_urbs, uint, S_IRUGO); +MODULE_PARM_DESC(num_rcv_urbs, + "Number of urbs used for reception (range: 0-" + __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: " + __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")"); + +module_param(num_snd_urbs, uint, S_IRUGO); +MODULE_PARM_DESC(num_snd_urbs, + "Number of urbs used for transmission (range: 0-" + __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: " + __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")"); + +module_param(rcv_buf_size, uint, S_IRUGO); +MODULE_PARM_DESC(rcv_buf_size, + "Size of the buffers used for reception in ATM cells (range: 1-" + __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: " + __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")"); + +module_param(snd_buf_size, uint, S_IRUGO); +MODULE_PARM_DESC(snd_buf_size, + "Size of the buffers used for transmission in ATM cells (range: 1-" + __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: " + __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")"); + + +/* receive */ + +struct usbatm_vcc_data { + /* vpi/vci lookup */ + struct list_head list; + short vpi; + int vci; + struct atm_vcc *vcc; + + /* raw cell reassembly */ + struct sk_buff *sarb; +}; + + +/* send */ + +struct usbatm_control { + struct atm_skb_data atm; + u32 len; + u32 crc; +}; + +#define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb) + + +/* ATM */ + +static void usbatm_atm_dev_close(struct atm_dev *dev); +static int usbatm_atm_open(struct atm_vcc *vcc); +static void usbatm_atm_close(struct atm_vcc *vcc); +static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg); +static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb); +static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page); + +static struct atmdev_ops usbatm_atm_devops = { + .dev_close = usbatm_atm_dev_close, + .open = usbatm_atm_open, + .close = usbatm_atm_close, + .ioctl = usbatm_atm_ioctl, + .send = usbatm_atm_send, + .proc_read = usbatm_atm_proc_read, + .owner = THIS_MODULE, +}; + + +/*********** +** misc ** +***********/ + +static inline unsigned int usbatm_pdu_length(unsigned int length) +{ + length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER; + return length - length % ATM_CELL_PAYLOAD; +} + +static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb) +{ + if (vcc->pop) + vcc->pop(vcc, skb); + else + dev_kfree_skb(skb); +} + + +/*********** +** urbs ** +************/ + +static inline struct urb *usbatm_pop_urb(struct usbatm_channel *channel) +{ + struct urb *urb; + + spin_lock_irq(&channel->lock); + if (list_empty(&channel->list)) { + spin_unlock_irq(&channel->lock); + return NULL; + } + + urb = list_entry(channel->list.next, struct urb, urb_list); + list_del(&urb->urb_list); + spin_unlock_irq(&channel->lock); + + return urb; +} + +static inline int usbatm_submit_urb(struct urb *urb) +{ + struct usbatm_channel *channel = urb->context; + int ret; + + vdbg("%s: submitting urb 0x%p, size %u", + __func__, urb, urb->transfer_buffer_length); + + ret = usb_submit_urb(urb, GFP_ATOMIC); + if (ret) { + atm_dbg(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n", + __func__, urb, ret); + + /* consider all errors transient and return the buffer back to the queue */ + urb->status = -EAGAIN; + spin_lock_irq(&channel->lock); + + /* must add to the front when sending; doesn't matter when receiving */ + list_add(&urb->urb_list, &channel->list); + + spin_unlock_irq(&channel->lock); + + /* make sure the channel doesn't stall */ + mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS)); + } + + return ret; +} + +static void usbatm_complete(struct urb *urb, struct pt_regs *regs) +{ + struct usbatm_channel *channel = urb->context; + unsigned long flags; + + vdbg("%s: urb 0x%p, status %d, actual_length %d", + __func__, urb, urb->status, urb->actual_length); + + /* usually in_interrupt(), but not always */ + spin_lock_irqsave(&channel->lock, flags); + + /* must add to the back when receiving; doesn't matter when sending */ + list_add_tail(&urb->urb_list, &channel->list); + + spin_unlock_irqrestore(&channel->lock, flags); + + if (unlikely(urb->status)) + /* throttle processing in case of an error */ + mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS)); + else + tasklet_schedule(&channel->tasklet); +} + + +/************* +** decode ** +*************/ + +static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance, + short vpi, int vci) +{ + struct usbatm_vcc_data *vcc; + + list_for_each_entry(vcc, &instance->vcc_list, list) + if ((vcc->vci == vci) && (vcc->vpi == vpi)) + return vcc; + return NULL; +} + +static void usbatm_extract_cells(struct usbatm_data *instance, + unsigned char *source, unsigned int avail_data) +{ + struct usbatm_vcc_data *cached_vcc = NULL; + struct atm_vcc *vcc; + struct sk_buff *sarb; + unsigned int stride = instance->rx_channel.stride; + int vci, cached_vci = 0; + short vpi, cached_vpi = 0; + u8 pti; + + for (; avail_data >= stride; avail_data -= stride, source += stride) { + vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4); + vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4); + pti = ((source[3] & 0xe) >> 1); + + vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti); + + if ((vci != cached_vci) || (vpi != cached_vpi)) { + cached_vpi = vpi; + cached_vci = vci; + + cached_vcc = usbatm_find_vcc(instance, vpi, vci); + + if (!cached_vcc) + atm_dbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci); + } + + if (!cached_vcc) + continue; + + vcc = cached_vcc->vcc; + + /* OAM F5 end-to-end */ + if (pti == ATM_PTI_E2EF5) { + atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n", __func__, vpi, vci); + atomic_inc(&vcc->stats->rx_err); + continue; + } + + sarb = cached_vcc->sarb; + + if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) { + atm_dbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n", + __func__, sarb->len, vcc); + /* discard cells already received */ + skb_trim(sarb, 0); + UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end); + } + + memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD); + __skb_put(sarb, ATM_CELL_PAYLOAD); + + if (pti & 1) { + struct sk_buff *skb; + unsigned int length; + unsigned int pdu_length; + + length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5]; + + /* guard against overflow */ + if (length > ATM_MAX_AAL5_PDU) { + atm_dbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n", + __func__, length, vcc); + atomic_inc(&vcc->stats->rx_err); + goto out; + } + + pdu_length = usbatm_pdu_length(length); + + if (sarb->len < pdu_length) { + atm_dbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n", + __func__, pdu_length, sarb->len, vcc); + atomic_inc(&vcc->stats->rx_err); + goto out; + } + + if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) { + atm_dbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n", + __func__, vcc); + atomic_inc(&vcc->stats->rx_err); + goto out; + } + + vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc); + + if (!(skb = dev_alloc_skb(length))) { + atm_dbg(instance, "%s: no memory for skb (length: %u)!\n", __func__, length); + atomic_inc(&vcc->stats->rx_drop); + goto out; + } + + vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize); + + if (!atm_charge(vcc, skb->truesize)) { + atm_dbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n", __func__, skb->truesize); + dev_kfree_skb(skb); + goto out; /* atm_charge increments rx_drop */ + } + + memcpy(skb->data, sarb->tail - pdu_length, length); + __skb_put(skb, length); + + vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u", + __func__, skb, skb->len, skb->truesize); + + PACKETDEBUG(skb->data, skb->len); + + vcc->push(vcc, skb); + + atomic_inc(&vcc->stats->rx); + out: + skb_trim(sarb, 0); + } + } +} + + +/************* +** encode ** +*************/ + +static unsigned int usbatm_write_cells(struct usbatm_data *instance, + struct sk_buff *skb, + u8 *target, unsigned int avail_space) +{ + struct usbatm_control *ctrl = UDSL_SKB(skb); + struct atm_vcc *vcc = ctrl->atm.vcc; + unsigned int num_written; + unsigned int stride = instance->tx_channel.stride; + + vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space); + UDSL_ASSERT(!(avail_space % stride)); + + for (num_written = 0; num_written < avail_space && ctrl->len; + num_written += stride, target += stride) { + unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD); + unsigned int left = ATM_CELL_PAYLOAD - data_len; + u8 *ptr = target; + + ptr[0] = vcc->vpi >> 4; + ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12); + ptr[2] = vcc->vci >> 4; + ptr[3] = vcc->vci << 4; + ptr[4] = 0xec; + ptr += ATM_CELL_HEADER; + + memcpy(ptr, skb->data, data_len); + ptr += data_len; + __skb_pull(skb, data_len); + + if(!left) + continue; + + memset(ptr, 0, left); + + if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */ + u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER; + /* trailer[0] = 0; UU = 0 */ + /* trailer[1] = 0; CPI = 0 */ + trailer[2] = ctrl->len >> 8; + trailer[3] = ctrl->len; + + ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4); + + trailer[4] = ctrl->crc >> 24; + trailer[5] = ctrl->crc >> 16; + trailer[6] = ctrl->crc >> 8; + trailer[7] = ctrl->crc; + + target[3] |= 0x2; /* adjust PTI */ + + ctrl->len = 0; /* tag this skb finished */ + } + else + ctrl->crc = crc32_be(ctrl->crc, ptr, left); + } + + return num_written; +} + + +/************** +** receive ** +**************/ + +static void usbatm_rx_process(unsigned long data) +{ + struct usbatm_data *instance = (struct usbatm_data *)data; + struct urb *urb; + + while ((urb = usbatm_pop_urb(&instance->rx_channel))) { + vdbg("%s: processing urb 0x%p", __func__, urb); + + if (usb_pipeisoc(urb->pipe)) { + int i; + for (i = 0; i < urb->number_of_packets; i++) + if (!urb->iso_frame_desc[i].status) + usbatm_extract_cells(instance, + (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset, + urb->iso_frame_desc[i].actual_length); + } + else + if (!urb->status) + usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length); + + if (usbatm_submit_urb(urb)) + return; + } +} + + +/*********** +** send ** +***********/ + +static void usbatm_tx_process(unsigned long data) +{ + struct usbatm_data *instance = (struct usbatm_data *)data; + struct sk_buff *skb = instance->current_skb; + struct urb *urb = NULL; + const unsigned int buf_size = instance->tx_channel.buf_size; + unsigned int num_written = 0; + u8 *buffer = NULL; + + if (!skb) + skb = skb_dequeue(&instance->sndqueue); + + while (skb) { + if (!urb) { + urb = usbatm_pop_urb(&instance->tx_channel); + if (!urb) + break; /* no more senders */ + buffer = urb->transfer_buffer; + num_written = (urb->status == -EAGAIN) ? + urb->transfer_buffer_length : 0; + } + + num_written += usbatm_write_cells(instance, skb, + buffer + num_written, + buf_size - num_written); + + vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p", + __func__, num_written, skb, urb); + + if (!UDSL_SKB(skb)->len) { + struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc; + + usbatm_pop(vcc, skb); + atomic_inc(&vcc->stats->tx); + + skb = skb_dequeue(&instance->sndqueue); + } + + if (num_written == buf_size || (!skb && num_written)) { + urb->transfer_buffer_length = num_written; + + if (usbatm_submit_urb(urb)) + break; + urb = NULL; + } + } + + instance->current_skb = skb; +} + +static void usbatm_cancel_send(struct usbatm_data *instance, + struct atm_vcc *vcc) +{ + struct sk_buff *skb, *n; + + atm_dbg(instance, "%s entered\n", __func__); + spin_lock_irq(&instance->sndqueue.lock); + for (skb = instance->sndqueue.next, n = skb->next; + skb != (struct sk_buff *)&instance->sndqueue; + skb = n, n = skb->next) + if (UDSL_SKB(skb)->atm.vcc == vcc) { + atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb); + __skb_unlink(skb, &instance->sndqueue); + usbatm_pop(vcc, skb); + } + spin_unlock_irq(&instance->sndqueue.lock); + + tasklet_disable(&instance->tx_channel.tasklet); + if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) { + atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb); + instance->current_skb = NULL; + usbatm_pop(vcc, skb); + } + tasklet_enable(&instance->tx_channel.tasklet); + atm_dbg(instance, "%s done\n", __func__); +} + +static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb) +{ + struct usbatm_data *instance = vcc->dev->dev_data; + struct usbatm_control *ctrl = UDSL_SKB(skb); + int err; + + vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len); + + if (!instance) { + dbg("%s: NULL data!", __func__); + err = -ENODEV; + goto fail; + } + + if (vcc->qos.aal != ATM_AAL5) { + atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); + err = -EINVAL; + goto fail; + } + + if (skb->len > ATM_MAX_AAL5_PDU) { + atm_dbg(instance, "%s: packet too long (%d vs %d)!\n", + __func__, skb->len, ATM_MAX_AAL5_PDU); + err = -EINVAL; + goto fail; + } + + PACKETDEBUG(skb->data, skb->len); + + /* initialize the control block */ + ctrl->atm.vcc = vcc; + ctrl->len = skb->len; + ctrl->crc = crc32_be(~0, skb->data, skb->len); + + skb_queue_tail(&instance->sndqueue, skb); + tasklet_schedule(&instance->tx_channel.tasklet); + + return 0; + + fail: + usbatm_pop(vcc, skb); + return err; +} + + +/******************** +** bean counting ** +********************/ + +static void usbatm_destroy_instance(struct kref *kref) +{ + struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount); + + dbg("%s", __func__); + + tasklet_kill(&instance->rx_channel.tasklet); + tasklet_kill(&instance->tx_channel.tasklet); + usb_put_dev(instance->usb_dev); + kfree(instance); +} + +void usbatm_get_instance(struct usbatm_data *instance) +{ + dbg("%s", __func__); + + kref_get(&instance->refcount); +} + +void usbatm_put_instance(struct usbatm_data *instance) +{ + dbg("%s", __func__); + + kref_put(&instance->refcount, usbatm_destroy_instance); +} + + +/********** +** ATM ** +**********/ + +static void usbatm_atm_dev_close(struct atm_dev *dev) +{ + struct usbatm_data *instance = dev->dev_data; + + dbg("%s", __func__); + + if (!instance) + return; + + dev->dev_data = NULL; + usbatm_put_instance(instance); /* taken in usbatm_atm_init */ +} + +static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page) +{ + struct usbatm_data *instance = atm_dev->dev_data; + int left = *pos; + + if (!instance) { + dbg("%s: NULL instance!", __func__); + return -ENODEV; + } + + if (!left--) + return sprintf(page, "%s\n", instance->description); + + if (!left--) + return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", + atm_dev->esi[0], atm_dev->esi[1], + atm_dev->esi[2], atm_dev->esi[3], + atm_dev->esi[4], atm_dev->esi[5]); + + if (!left--) + return sprintf(page, + "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n", + atomic_read(&atm_dev->stats.aal5.tx), + atomic_read(&atm_dev->stats.aal5.tx_err), + atomic_read(&atm_dev->stats.aal5.rx), + atomic_read(&atm_dev->stats.aal5.rx_err), + atomic_read(&atm_dev->stats.aal5.rx_drop)); + + if (!left--) + switch (atm_dev->signal) { + case ATM_PHY_SIG_FOUND: + return sprintf(page, "Line up\n"); + case ATM_PHY_SIG_LOST: + return sprintf(page, "Line down\n"); + default: + return sprintf(page, "Line state unknown\n"); + } + + return 0; +} + +static int usbatm_atm_open(struct atm_vcc *vcc) +{ + struct usbatm_data *instance = vcc->dev->dev_data; + struct usbatm_vcc_data *new = NULL; + int ret; + int vci = vcc->vci; + short vpi = vcc->vpi; + + if (!instance) { + dbg("%s: NULL data!", __func__); + return -ENODEV; + } + + atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci); + + /* only support AAL5 */ + if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) + || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) { + atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); + return -EINVAL; + } + + down(&instance->serialize); /* vs self, usbatm_atm_close */ + + if (usbatm_find_vcc(instance, vpi, vci)) { + atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci); + ret = -EADDRINUSE; + goto fail; + } + + if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) { + atm_dbg(instance, "%s: no memory for vcc_data!\n", __func__); + ret = -ENOMEM; + goto fail; + } + + memset(new, 0, sizeof(struct usbatm_vcc_data)); + new->vcc = vcc; + new->vpi = vpi; + new->vci = vci; + + new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL); + if (!new->sarb) { + atm_dbg(instance, "%s: no memory for SAR buffer!\n", __func__); + ret = -ENOMEM; + goto fail; + } + + vcc->dev_data = new; + + tasklet_disable(&instance->rx_channel.tasklet); + list_add(&new->list, &instance->vcc_list); + tasklet_enable(&instance->rx_channel.tasklet); + + set_bit(ATM_VF_ADDR, &vcc->flags); + set_bit(ATM_VF_PARTIAL, &vcc->flags); + set_bit(ATM_VF_READY, &vcc->flags); + + up(&instance->serialize); + + atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new); + + return 0; + +fail: + kfree(new); + up(&instance->serialize); + return ret; +} + +static void usbatm_atm_close(struct atm_vcc *vcc) +{ + struct usbatm_data *instance = vcc->dev->dev_data; + struct usbatm_vcc_data *vcc_data = vcc->dev_data; + + if (!instance || !vcc_data) { + dbg("%s: NULL data!", __func__); + return; + } + + atm_dbg(instance, "%s entered\n", __func__); + + atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n", + __func__, vcc_data, vcc_data->vpi, vcc_data->vci); + + usbatm_cancel_send(instance, vcc); + + down(&instance->serialize); /* vs self, usbatm_atm_open */ + + tasklet_disable(&instance->rx_channel.tasklet); + list_del(&vcc_data->list); + tasklet_enable(&instance->rx_channel.tasklet); + + kfree_skb(vcc_data->sarb); + vcc_data->sarb = NULL; + + kfree(vcc_data); + vcc->dev_data = NULL; + + vcc->vpi = ATM_VPI_UNSPEC; + vcc->vci = ATM_VCI_UNSPEC; + clear_bit(ATM_VF_READY, &vcc->flags); + clear_bit(ATM_VF_PARTIAL, &vcc->flags); + clear_bit(ATM_VF_ADDR, &vcc->flags); + + up(&instance->serialize); + + atm_dbg(instance, "%s successful\n", __func__); +} + +static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, + void __user * arg) +{ + switch (cmd) { + case ATM_QUERYLOOP: + return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0; + default: + return -ENOIOCTLCMD; + } +} + +static int usbatm_atm_init(struct usbatm_data *instance) +{ + struct atm_dev *atm_dev; + int ret, i; + + /* ATM init */ + atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL); + if (!atm_dev) { + usb_dbg(instance, "%s: failed to register ATM device!\n", __func__); + return -1; + } + + instance->atm_dev = atm_dev; + + atm_dev->ci_range.vpi_bits = ATM_CI_MAX; + atm_dev->ci_range.vci_bits = ATM_CI_MAX; + atm_dev->signal = ATM_PHY_SIG_UNKNOWN; + + /* temp init ATM device, set to 128kbit */ + atm_dev->link_rate = 128 * 1000 / 424; + + if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) { + atm_dbg(instance, "%s: atm_start failed: %d!\n", __func__, ret); + goto fail; + } + + /* ready for ATM callbacks */ + usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */ + mb(); + atm_dev->dev_data = instance; + + /* submit all rx URBs */ + for (i = 0; i < num_rcv_urbs; i++) + usbatm_submit_urb(instance->urbs[i]); + + return 0; + + fail: + instance->atm_dev = NULL; + shutdown_atm_dev(atm_dev); /* usbatm_atm_dev_close will eventually be called */ + return ret; +} + + +/********** +** USB ** +**********/ + +static int usbatm_do_heavy_init(void *arg) +{ + struct usbatm_data *instance = arg; + int ret; + + daemonize(instance->driver->driver_name); + allow_signal(SIGTERM); + + complete(&instance->thread_started); + + ret = instance->driver->heavy_init(instance, instance->usb_intf); + + if (!ret) + ret = usbatm_atm_init(instance); + + down(&instance->serialize); + instance->thread_pid = -1; + up(&instance->serialize); + + complete_and_exit(&instance->thread_exited, ret); +} + +static int usbatm_heavy_init(struct usbatm_data *instance) +{ + int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL); + + if (ret < 0) { + usb_dbg(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret); + return ret; + } + + down(&instance->serialize); + instance->thread_pid = ret; + up(&instance->serialize); + + wait_for_completion(&instance->thread_started); + + return 0; +} + +static void usbatm_tasklet_schedule(unsigned long data) +{ + tasklet_schedule((struct tasklet_struct *) data); +} + +static inline void usbatm_init_channel(struct usbatm_channel *channel) +{ + spin_lock_init(&channel->lock); + INIT_LIST_HEAD(&channel->list); + channel->delay.function = usbatm_tasklet_schedule; + channel->delay.data = (unsigned long) &channel->tasklet; + init_timer(&channel->delay); +} + +int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id, + struct usbatm_driver *driver) +{ + struct device *dev = &intf->dev; + struct usb_device *usb_dev = interface_to_usbdev(intf); + struct usbatm_data *instance; + char *buf; + int error = -ENOMEM; + int i, length; + int need_heavy; + + dev_dbg(dev, "%s: trying driver %s with vendor=0x%x, product=0x%x, ifnum %d\n", + __func__, driver->driver_name, + le16_to_cpu(usb_dev->descriptor.idVendor), + le16_to_cpu(usb_dev->descriptor.idProduct), + intf->altsetting->desc.bInterfaceNumber); + + /* instance init */ + instance = kcalloc(1, sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL); + if (!instance) { + dev_dbg(dev, "%s: no memory for instance data!\n", __func__); + return -ENOMEM; + } + + /* public fields */ + + instance->driver = driver; + snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name); + + instance->usb_dev = usb_dev; + instance->usb_intf = intf; + + buf = instance->description; + length = sizeof(instance->description); + + if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0) + goto bind; + + buf += i; + length -= i; + + i = scnprintf(buf, length, " ("); + buf += i; + length -= i; + + if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0) + goto bind; + + buf += i; + length -= i; + + snprintf(buf, length, ")"); + + bind: + need_heavy = 1; + if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) { + dev_dbg(dev, "%s: bind failed: %d!\n", __func__, error); + goto fail_free; + } + + /* private fields */ + + kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */ + init_MUTEX(&instance->serialize); + + instance->thread_pid = -1; + init_completion(&instance->thread_started); + init_completion(&instance->thread_exited); + + INIT_LIST_HEAD(&instance->vcc_list); + + usbatm_init_channel(&instance->rx_channel); + usbatm_init_channel(&instance->tx_channel); + tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance); + tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance); + instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in); + instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out); + instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding; + instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding; + instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride; + instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride; + instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance; + + skb_queue_head_init(&instance->sndqueue); + + for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { + struct urb *urb; + u8 *buffer; + unsigned int iso_packets = 0, iso_size = 0; + struct usbatm_channel *channel = i < num_rcv_urbs ? + &instance->rx_channel : &instance->tx_channel; + + if (usb_pipeisoc(channel->endpoint)) { + /* don't expect iso out endpoints */ + iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0); + iso_size -= iso_size % channel->stride; /* alignment */ + BUG_ON(!iso_size); + iso_packets = (channel->buf_size - 1) / iso_size + 1; + } + + urb = usb_alloc_urb(iso_packets, GFP_KERNEL); + if (!urb) { + dev_dbg(dev, "%s: no memory for urb %d!\n", __func__, i); + goto fail_unbind; + } + + instance->urbs[i] = urb; + + buffer = kmalloc(channel->buf_size, GFP_KERNEL); + if (!buffer) { + dev_dbg(dev, "%s: no memory for buffer %d!\n", __func__, i); + goto fail_unbind; + } + memset(buffer, 0, channel->buf_size); + + usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint, + buffer, channel->buf_size, usbatm_complete, channel); + if (iso_packets) { + int j; + urb->interval = 1; + urb->transfer_flags = URB_ISO_ASAP; + urb->number_of_packets = iso_packets; + for (j = 0; j < iso_packets; j++) { + urb->iso_frame_desc[j].offset = iso_size * j; + urb->iso_frame_desc[j].length = min_t(int, iso_size, + channel->buf_size - urb->iso_frame_desc[j].offset); + } + } + + /* put all tx URBs on the list of spares */ + if (i >= num_rcv_urbs) + list_add_tail(&urb->urb_list, &channel->list); + + vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p", + __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb); + } + + if (need_heavy && driver->heavy_init) { + error = usbatm_heavy_init(instance); + } else { + complete(&instance->thread_exited); /* pretend that heavy_init was run */ + error = usbatm_atm_init(instance); + } + + if (error < 0) + goto fail_unbind; + + usb_get_dev(usb_dev); + usb_set_intfdata(intf, instance); + + return 0; + + fail_unbind: + if (instance->driver->unbind) + instance->driver->unbind(instance, intf); + fail_free: + for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { + if (instance->urbs[i]) + kfree(instance->urbs[i]->transfer_buffer); + usb_free_urb(instance->urbs[i]); + } + + kfree (instance); + + return error; +} +EXPORT_SYMBOL_GPL(usbatm_usb_probe); + +void usbatm_usb_disconnect(struct usb_interface *intf) +{ + struct device *dev = &intf->dev; + struct usbatm_data *instance = usb_get_intfdata(intf); + int i; + + dev_dbg(dev, "%s entered\n", __func__); + + if (!instance) { + dev_dbg(dev, "%s: NULL instance!\n", __func__); + return; + } + + usb_set_intfdata(intf, NULL); + + down(&instance->serialize); + if (instance->thread_pid >= 0) + kill_proc(instance->thread_pid, SIGTERM, 1); + up(&instance->serialize); + + wait_for_completion(&instance->thread_exited); + + tasklet_disable(&instance->rx_channel.tasklet); + tasklet_disable(&instance->tx_channel.tasklet); + + for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) + usb_kill_urb(instance->urbs[i]); + + del_timer_sync(&instance->rx_channel.delay); + del_timer_sync(&instance->tx_channel.delay); + + if (instance->atm_dev && instance->driver->atm_stop) + instance->driver->atm_stop(instance, instance->atm_dev); + + if (instance->driver->unbind) + instance->driver->unbind(instance, intf); + + instance->driver_data = NULL; + + /* turn usbatm_[rt]x_process into noop */ + /* no need to take the spinlock */ + INIT_LIST_HEAD(&instance->rx_channel.list); + INIT_LIST_HEAD(&instance->tx_channel.list); + + tasklet_enable(&instance->rx_channel.tasklet); + tasklet_enable(&instance->tx_channel.tasklet); + + for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { + kfree(instance->urbs[i]->transfer_buffer); + usb_free_urb(instance->urbs[i]); + } + + /* ATM finalize */ + if (instance->atm_dev) + shutdown_atm_dev(instance->atm_dev); + + usbatm_put_instance(instance); /* taken in usbatm_usb_probe */ +} +EXPORT_SYMBOL_GPL(usbatm_usb_disconnect); + + +/*********** +** init ** +***********/ + +static int __init usbatm_usb_init(void) +{ + dbg("%s: driver version %s", __func__, DRIVER_VERSION); + + if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) { + printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name); + return -EIO; + } + + if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) + || (num_snd_urbs > UDSL_MAX_SND_URBS) + || (rcv_buf_size < 1) + || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) + || (snd_buf_size < 1) + || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE)) + return -EINVAL; + + return 0; +} +module_init(usbatm_usb_init); + +static void __exit usbatm_usb_exit(void) +{ + dbg("%s", __func__); +} +module_exit(usbatm_usb_exit); + +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_LICENSE("GPL"); +MODULE_VERSION(DRIVER_VERSION); + +/************ +** debug ** +************/ + +#ifdef VERBOSE_DEBUG +static int usbatm_print_packet(const unsigned char *data, int len) +{ + unsigned char buffer[256]; + int i = 0, j = 0; + + for (i = 0; i < len;) { + buffer[0] = '\0'; + sprintf(buffer, "%.3d :", i); + for (j = 0; (j < 16) && (i < len); j++, i++) { + sprintf(buffer, "%s %2.2x", buffer, data[i]); + } + dbg("%s", buffer); + } + return i; +} +#endif diff --git a/drivers/usb/atm/usbatm.h b/drivers/usb/atm/usbatm.h new file mode 100644 index 000000000000..936646457935 --- /dev/null +++ b/drivers/usb/atm/usbatm.h @@ -0,0 +1,184 @@ +/****************************************************************************** + * usbatm.h - Generic USB xDSL driver core + * + * Copyright (C) 2001, Alcatel + * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas + * Copyright (C) 2004, David Woodhouse + * + * 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ******************************************************************************/ + +#ifndef _USBATM_H_ +#define _USBATM_H_ + +#include <linux/config.h> + +/* +#define DEBUG +#define VERBOSE_DEBUG +*/ + +#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG) +# define DEBUG +#endif + +#include <asm/semaphore.h> +#include <linux/atm.h> +#include <linux/atmdev.h> +#include <linux/completion.h> +#include <linux/device.h> +#include <linux/kref.h> +#include <linux/list.h> +#include <linux/stringify.h> +#include <linux/usb.h> + +#ifdef DEBUG +#define UDSL_ASSERT(x) BUG_ON(!(x)) +#else +#define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '%s' at line %d", __stringify(x), __LINE__); } while(0) +#endif + +#define usb_err(instance, format, arg...) \ + dev_err(&(instance)->usb_intf->dev , format , ## arg) +#define usb_info(instance, format, arg...) \ + dev_info(&(instance)->usb_intf->dev , format , ## arg) +#define usb_warn(instance, format, arg...) \ + dev_warn(&(instance)->usb_intf->dev , format , ## arg) +#define usb_dbg(instance, format, arg...) \ + dev_dbg(&(instance)->usb_intf->dev , format , ## arg) + +/* FIXME: move to dev_* once ATM is driver model aware */ +#define atm_printk(level, instance, format, arg...) \ + printk(level "ATM dev %d: " format , \ + (instance)->atm_dev->number , ## arg) + +#define atm_err(instance, format, arg...) \ + atm_printk(KERN_ERR, instance , format , ## arg) +#define atm_info(instance, format, arg...) \ + atm_printk(KERN_INFO, instance , format , ## arg) +#define atm_warn(instance, format, arg...) \ + atm_printk(KERN_WARNING, instance , format , ## arg) +#ifdef DEBUG +#define atm_dbg(instance, format, arg...) \ + atm_printk(KERN_DEBUG, instance , format , ## arg) +#else +#define atm_dbg(instance, format, arg...) \ + do {} while (0) +#endif + + +/* mini driver */ + +struct usbatm_data; + +/* +* Assuming all methods exist and succeed, they are called in this order: +* +* bind, heavy_init, atm_start, ..., atm_stop, unbind +*/ + +struct usbatm_driver { + struct module *owner; + + const char *driver_name; + + /* + * init device ... can sleep, or cause probe() failure. Drivers with a heavy_init + * method can avoid having it called by setting need_heavy_init to zero. + */ + int (*bind) (struct usbatm_data *, struct usb_interface *, + const struct usb_device_id *id, int *need_heavy_init); + + /* additional device initialization that is too slow to be done in probe() */ + int (*heavy_init) (struct usbatm_data *, struct usb_interface *); + + /* cleanup device ... can sleep, but can't fail */ + void (*unbind) (struct usbatm_data *, struct usb_interface *); + + /* init ATM device ... can sleep, or cause ATM initialization failure */ + int (*atm_start) (struct usbatm_data *, struct atm_dev *); + + /* cleanup ATM device ... can sleep, but can't fail */ + void (*atm_stop) (struct usbatm_data *, struct atm_dev *); + + int in; /* rx endpoint */ + int out; /* tx endpoint */ + + unsigned rx_padding; + unsigned tx_padding; +}; + +extern int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id, + struct usbatm_driver *driver); +extern void usbatm_usb_disconnect(struct usb_interface *intf); + + +struct usbatm_channel { + int endpoint; /* usb pipe */ + unsigned int stride; /* ATM cell size + padding */ + unsigned int buf_size; /* urb buffer size */ + spinlock_t lock; + struct list_head list; + struct tasklet_struct tasklet; + struct timer_list delay; + struct usbatm_data *usbatm; +}; + +/* main driver data */ + +struct usbatm_data { + /****************** + * public fields * + ******************/ + + /* mini driver */ + struct usbatm_driver *driver; + void *driver_data; + char driver_name[16]; + + /* USB device */ + struct usb_device *usb_dev; + struct usb_interface *usb_intf; + char description[64]; + + /* ATM device */ + struct atm_dev *atm_dev; + + /******************************** + * private fields - do not use * + ********************************/ + + struct kref refcount; + struct semaphore serialize; + + /* heavy init */ + int thread_pid; + struct completion thread_started; + struct completion thread_exited; + + /* ATM device */ + struct list_head vcc_list; + + struct usbatm_channel rx_channel; + struct usbatm_channel tx_channel; + + struct sk_buff_head sndqueue; + struct sk_buff *current_skb; /* being emptied */ + + struct urb *urbs[0]; +}; + +#endif /* _USBATM_H_ */ diff --git a/drivers/usb/atm/xusbatm.c b/drivers/usb/atm/xusbatm.c new file mode 100644 index 000000000000..7fe7fb484d10 --- /dev/null +++ b/drivers/usb/atm/xusbatm.c @@ -0,0 +1,196 @@ +/****************************************************************************** + * xusbatm.c - dumb usbatm-based driver for modems initialized in userspace + * + * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru) + * + * 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ******************************************************************************/ + +#include <linux/module.h> +#include <linux/netdevice.h> /* FIXME: required by linux/etherdevice.h */ +#include <linux/etherdevice.h> /* for random_ether_addr() */ + +#include "usbatm.h" + + +#define XUSBATM_DRIVERS_MAX 8 + +#define XUSBATM_PARM(name, type, parmtype, desc) \ + static type name[XUSBATM_DRIVERS_MAX]; \ + static int num_##name; \ + module_param_array(name, parmtype, &num_##name, 0444); \ + MODULE_PARM_DESC(name, desc) + +XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor"); +XUSBATM_PARM(product, unsigned short, ushort, "USB device product"); + +XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number"); +XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number"); +XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)"); +XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)"); + +static const char xusbatm_driver_name[] = "xusbatm"; + +static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX]; +static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1]; +static struct usb_driver xusbatm_usb_driver; + +static int usb_intf_has_ep(const struct usb_interface *intf, u8 ep) +{ + int i, j; + + for (i = 0; i < intf->num_altsetting; i++) { + struct usb_host_interface *alt = intf->altsetting; + for (j = 0; j < alt->desc.bNumEndpoints; j++) + if ((alt->endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) == ep) + return 1; + } + return 0; +} + +static int xusbatm_bind(struct usbatm_data *usbatm_instance, + struct usb_interface *intf, const struct usb_device_id *id, + int *need_heavy_init) +{ + struct usb_device *usb_dev = interface_to_usbdev(intf); + int drv_ix = id - xusbatm_usb_ids; + int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]); + int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]); + u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix]; + int i, ret; + + usb_dbg(usbatm_instance, "%s: binding driver %d: vendor %#x product %#x" + " rx: ep %#x padd %d tx: ep %#x padd %d\n", + __func__, drv_ix, vendor[drv_ix], product[drv_ix], + rx_endpoint[drv_ix], rx_padding[drv_ix], + tx_endpoint[drv_ix], tx_padding[drv_ix]); + + if (!rx_ep_present && !tx_ep_present) { + usb_dbg(usbatm_instance, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n", + __func__, intf->altsetting->desc.bInterfaceNumber, + rx_endpoint[drv_ix], tx_endpoint[drv_ix]); + return -ENODEV; + } + + if (rx_ep_present && tx_ep_present) + return 0; + + for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) { + struct usb_interface *cur_if = usb_dev->actconfig->interface[i]; + + if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) { + ret = usb_driver_claim_interface(&xusbatm_usb_driver, + cur_if, usbatm_instance); + if (!ret) + usb_err(usbatm_instance, "%s: failed to claim interface #%d (%d)\n", + __func__, cur_if->altsetting->desc.bInterfaceNumber, ret); + return ret; + } + } + + usb_err(usbatm_instance, "%s: no interface has endpoint %#x\n", + __func__, searched_ep); + return -ENODEV; +} + +static void xusbatm_unbind(struct usbatm_data *usbatm_instance, + struct usb_interface *intf) +{ + struct usb_device *usb_dev = interface_to_usbdev(intf); + int i; + usb_dbg(usbatm_instance, "%s entered\n", __func__); + + for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) { + struct usb_interface *cur_if = usb_dev->actconfig->interface[i]; + usb_set_intfdata(cur_if, NULL); + usb_driver_release_interface(&xusbatm_usb_driver, cur_if); + } +} + +static int xusbatm_atm_start(struct usbatm_data *usbatm_instance, + struct atm_dev *atm_dev) +{ + atm_dbg(usbatm_instance, "%s entered\n", __func__); + + /* use random MAC as we've no way to get it from the device */ + random_ether_addr(atm_dev->esi); + + return 0; +} + + +static int xusbatm_usb_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + return usbatm_usb_probe(intf, id, + xusbatm_drivers + (id - xusbatm_usb_ids)); +} + +static struct usb_driver xusbatm_usb_driver = { + .owner = THIS_MODULE, + .name = xusbatm_driver_name, + .probe = xusbatm_usb_probe, + .disconnect = usbatm_usb_disconnect, + .id_table = xusbatm_usb_ids +}; + +static int __init xusbatm_init(void) +{ + int i; + + dbg("xusbatm_init"); + + if (!num_vendor || + num_vendor != num_product || + num_vendor != num_rx_endpoint || + num_vendor != num_tx_endpoint) { + warn("malformed module parameters"); + return -EINVAL; + } + + for (i = 0; i < num_vendor; i++) { + xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE; + xusbatm_usb_ids[i].idVendor = vendor[i]; + xusbatm_usb_ids[i].idProduct = product[i]; + + + xusbatm_drivers[i].owner = THIS_MODULE; + xusbatm_drivers[i].driver_name = xusbatm_driver_name; + xusbatm_drivers[i].bind = xusbatm_bind; + xusbatm_drivers[i].unbind = xusbatm_unbind; + xusbatm_drivers[i].atm_start = xusbatm_atm_start; + xusbatm_drivers[i].in = rx_endpoint[i]; + xusbatm_drivers[i].out = tx_endpoint[i]; + xusbatm_drivers[i].rx_padding = rx_padding[i]; + xusbatm_drivers[i].tx_padding = tx_padding[i]; + } + + return usb_register(&xusbatm_usb_driver); +} +module_init(xusbatm_init); + +static void __exit xusbatm_exit(void) +{ + dbg("xusbatm_exit entered"); + + usb_deregister(&xusbatm_usb_driver); +} +module_exit(xusbatm_exit); + +MODULE_AUTHOR("Roman Kagan, Duncan Sands"); +MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1"); |