Path: blob/master/drivers/isdn/gigaset/bas-gigaset.c
15112 views
/*1* USB driver for Gigaset 307x base via direct USB connection.2*3* Copyright (c) 2001 by Hansjoerg Lipp <[email protected]>,4* Tilman Schmidt <[email protected]>,5* Stefan Eilers.6*7* =====================================================================8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License as10* published by the Free Software Foundation; either version 2 of11* the License, or (at your option) any later version.12* =====================================================================13*/1415#include "gigaset.h"16#include <linux/usb.h>17#include <linux/module.h>18#include <linux/moduleparam.h>1920/* Version Information */21#define DRIVER_AUTHOR "Tilman Schmidt <[email protected]>, Hansjoerg Lipp <[email protected]>, Stefan Eilers"22#define DRIVER_DESC "USB Driver for Gigaset 307x"232425/* Module parameters */2627static int startmode = SM_ISDN;28static int cidmode = 1;2930module_param(startmode, int, S_IRUGO);31module_param(cidmode, int, S_IRUGO);32MODULE_PARM_DESC(startmode, "start in isdn4linux mode");33MODULE_PARM_DESC(cidmode, "Call-ID mode");3435#define GIGASET_MINORS 136#define GIGASET_MINOR 1637#define GIGASET_MODULENAME "bas_gigaset"38#define GIGASET_DEVNAME "ttyGB"3940/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */41#define IF_WRITEBUF 2644243/* interrupt pipe message size according to ibid. ch. 2.2 */44#define IP_MSGSIZE 34546/* Values for the Gigaset 307x */47#define USB_GIGA_VENDOR_ID 0x068148#define USB_3070_PRODUCT_ID 0x000149#define USB_3075_PRODUCT_ID 0x000250#define USB_SX303_PRODUCT_ID 0x002151#define USB_SX353_PRODUCT_ID 0x00225253/* table of devices that work with this driver */54static const struct usb_device_id gigaset_table[] = {55{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },56{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },57{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },58{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },59{ } /* Terminating entry */60};6162MODULE_DEVICE_TABLE(usb, gigaset_table);6364/*======================= local function prototypes ==========================*/6566/* function called if a new device belonging to this driver is connected */67static int gigaset_probe(struct usb_interface *interface,68const struct usb_device_id *id);6970/* Function will be called if the device is unplugged */71static void gigaset_disconnect(struct usb_interface *interface);7273/* functions called before/after suspend */74static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);75static int gigaset_resume(struct usb_interface *intf);7677/* functions called before/after device reset */78static int gigaset_pre_reset(struct usb_interface *intf);79static int gigaset_post_reset(struct usb_interface *intf);8081static int atread_submit(struct cardstate *, int);82static void stopurbs(struct bas_bc_state *);83static int req_submit(struct bc_state *, int, int, int);84static int atwrite_submit(struct cardstate *, unsigned char *, int);85static int start_cbsend(struct cardstate *);8687/*============================================================================*/8889struct bas_cardstate {90struct usb_device *udev; /* USB device pointer */91struct usb_interface *interface; /* interface for this device */92unsigned char minor; /* starting minor number */9394struct urb *urb_ctrl; /* control pipe default URB */95struct usb_ctrlrequest dr_ctrl;96struct timer_list timer_ctrl; /* control request timeout */97int retry_ctrl;9899struct timer_list timer_atrdy; /* AT command ready timeout */100struct urb *urb_cmd_out; /* for sending AT commands */101struct usb_ctrlrequest dr_cmd_out;102int retry_cmd_out;103104struct urb *urb_cmd_in; /* for receiving AT replies */105struct usb_ctrlrequest dr_cmd_in;106struct timer_list timer_cmd_in; /* receive request timeout */107unsigned char *rcvbuf; /* AT reply receive buffer */108109struct urb *urb_int_in; /* URB for interrupt pipe */110unsigned char *int_in_buf;111struct work_struct int_in_wq; /* for usb_clear_halt() */112struct timer_list timer_int_in; /* int read retry delay */113int retry_int_in;114115spinlock_t lock; /* locks all following */116int basstate; /* bitmap (BS_*) */117int pending; /* uncompleted base request */118wait_queue_head_t waitqueue;119int rcvbuf_size; /* size of AT receive buffer */120/* 0: no receive in progress */121int retry_cmd_in; /* receive req retry count */122};123124/* status of direct USB connection to 307x base (bits in basstate) */125#define BS_ATOPEN 0x001 /* AT channel open */126#define BS_B1OPEN 0x002 /* B channel 1 open */127#define BS_B2OPEN 0x004 /* B channel 2 open */128#define BS_ATREADY 0x008 /* base ready for AT command */129#define BS_INIT 0x010 /* base has signalled INIT_OK */130#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */131#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */132#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */133#define BS_SUSPEND 0x100 /* USB port suspended */134#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */135136137static struct gigaset_driver *driver;138139/* usb specific object needed to register this driver with the usb subsystem */140static struct usb_driver gigaset_usb_driver = {141.name = GIGASET_MODULENAME,142.probe = gigaset_probe,143.disconnect = gigaset_disconnect,144.id_table = gigaset_table,145.suspend = gigaset_suspend,146.resume = gigaset_resume,147.reset_resume = gigaset_post_reset,148.pre_reset = gigaset_pre_reset,149.post_reset = gigaset_post_reset,150};151152/* get message text for usb_submit_urb return code153*/154static char *get_usb_rcmsg(int rc)155{156static char unkmsg[28];157158switch (rc) {159case 0:160return "success";161case -ENOMEM:162return "out of memory";163case -ENODEV:164return "device not present";165case -ENOENT:166return "endpoint not present";167case -ENXIO:168return "URB type not supported";169case -EINVAL:170return "invalid argument";171case -EAGAIN:172return "start frame too early or too much scheduled";173case -EFBIG:174return "too many isoc frames requested";175case -EPIPE:176return "endpoint stalled";177case -EMSGSIZE:178return "invalid packet size";179case -ENOSPC:180return "would overcommit USB bandwidth";181case -ESHUTDOWN:182return "device shut down";183case -EPERM:184return "reject flag set";185case -EHOSTUNREACH:186return "device suspended";187default:188snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);189return unkmsg;190}191}192193/* get message text for USB status code194*/195static char *get_usb_statmsg(int status)196{197static char unkmsg[28];198199switch (status) {200case 0:201return "success";202case -ENOENT:203return "unlinked (sync)";204case -EINPROGRESS:205return "URB still pending";206case -EPROTO:207return "bitstuff error, timeout, or unknown USB error";208case -EILSEQ:209return "CRC mismatch, timeout, or unknown USB error";210case -ETIME:211return "USB response timeout";212case -EPIPE:213return "endpoint stalled";214case -ECOMM:215return "IN buffer overrun";216case -ENOSR:217return "OUT buffer underrun";218case -EOVERFLOW:219return "endpoint babble";220case -EREMOTEIO:221return "short packet";222case -ENODEV:223return "device removed";224case -EXDEV:225return "partial isoc transfer";226case -EINVAL:227return "ISO madness";228case -ECONNRESET:229return "unlinked (async)";230case -ESHUTDOWN:231return "device shut down";232default:233snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);234return unkmsg;235}236}237238/* usb_pipetype_str239* retrieve string representation of USB pipe type240*/241static inline char *usb_pipetype_str(int pipe)242{243if (usb_pipeisoc(pipe))244return "Isoc";245if (usb_pipeint(pipe))246return "Int";247if (usb_pipecontrol(pipe))248return "Ctrl";249if (usb_pipebulk(pipe))250return "Bulk";251return "?";252}253254/* dump_urb255* write content of URB to syslog for debugging256*/257static inline void dump_urb(enum debuglevel level, const char *tag,258struct urb *urb)259{260#ifdef CONFIG_GIGASET_DEBUG261int i;262gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);263if (urb) {264gig_dbg(level,265" dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "266"hcpriv=0x%08lx, transfer_flags=0x%x,",267(unsigned long) urb->dev,268usb_pipetype_str(urb->pipe),269usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),270usb_pipein(urb->pipe) ? "in" : "out",271(unsigned long) urb->hcpriv,272urb->transfer_flags);273gig_dbg(level,274" transfer_buffer=0x%08lx[%d], actual_length=%d, "275"setup_packet=0x%08lx,",276(unsigned long) urb->transfer_buffer,277urb->transfer_buffer_length, urb->actual_length,278(unsigned long) urb->setup_packet);279gig_dbg(level,280" start_frame=%d, number_of_packets=%d, interval=%d, "281"error_count=%d,",282urb->start_frame, urb->number_of_packets, urb->interval,283urb->error_count);284gig_dbg(level,285" context=0x%08lx, complete=0x%08lx, "286"iso_frame_desc[]={",287(unsigned long) urb->context,288(unsigned long) urb->complete);289for (i = 0; i < urb->number_of_packets; i++) {290struct usb_iso_packet_descriptor *pifd291= &urb->iso_frame_desc[i];292gig_dbg(level,293" {offset=%u, length=%u, actual_length=%u, "294"status=%u}",295pifd->offset, pifd->length, pifd->actual_length,296pifd->status);297}298}299gig_dbg(level, "}}");300#endif301}302303/* read/set modem control bits etc. (m10x only) */304static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,305unsigned new_state)306{307return -EINVAL;308}309310static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)311{312return -EINVAL;313}314315static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)316{317return -EINVAL;318}319320/* set/clear bits in base connection state, return previous state321*/322static inline int update_basstate(struct bas_cardstate *ucs,323int set, int clear)324{325unsigned long flags;326int state;327328spin_lock_irqsave(&ucs->lock, flags);329state = ucs->basstate;330ucs->basstate = (state & ~clear) | set;331spin_unlock_irqrestore(&ucs->lock, flags);332return state;333}334335/* error_hangup336* hang up any existing connection because of an unrecoverable error337* This function may be called from any context and takes care of scheduling338* the necessary actions for execution outside of interrupt context.339* cs->lock must not be held.340* argument:341* B channel control structure342*/343static inline void error_hangup(struct bc_state *bcs)344{345struct cardstate *cs = bcs->cs;346347gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);348gigaset_schedule_event(cs);349}350351/* error_reset352* reset Gigaset device because of an unrecoverable error353* This function may be called from any context, and takes care of354* scheduling the necessary actions for execution outside of interrupt context.355* cs->hw.bas->lock must not be held.356* argument:357* controller state structure358*/359static inline void error_reset(struct cardstate *cs)360{361/* reset interrupt pipe to recover (ignore errors) */362update_basstate(cs->hw.bas, BS_RESETTING, 0);363if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))364/* submission failed, escalate to USB port reset */365usb_queue_reset_device(cs->hw.bas->interface);366}367368/* check_pending369* check for completion of pending control request370* parameter:371* ucs hardware specific controller state structure372*/373static void check_pending(struct bas_cardstate *ucs)374{375unsigned long flags;376377spin_lock_irqsave(&ucs->lock, flags);378switch (ucs->pending) {379case 0:380break;381case HD_OPEN_ATCHANNEL:382if (ucs->basstate & BS_ATOPEN)383ucs->pending = 0;384break;385case HD_OPEN_B1CHANNEL:386if (ucs->basstate & BS_B1OPEN)387ucs->pending = 0;388break;389case HD_OPEN_B2CHANNEL:390if (ucs->basstate & BS_B2OPEN)391ucs->pending = 0;392break;393case HD_CLOSE_ATCHANNEL:394if (!(ucs->basstate & BS_ATOPEN))395ucs->pending = 0;396break;397case HD_CLOSE_B1CHANNEL:398if (!(ucs->basstate & BS_B1OPEN))399ucs->pending = 0;400break;401case HD_CLOSE_B2CHANNEL:402if (!(ucs->basstate & BS_B2OPEN))403ucs->pending = 0;404break;405case HD_DEVICE_INIT_ACK: /* no reply expected */406ucs->pending = 0;407break;408case HD_RESET_INTERRUPT_PIPE:409if (!(ucs->basstate & BS_RESETTING))410ucs->pending = 0;411break;412/*413* HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately414* and should never end up here415*/416default:417dev_warn(&ucs->interface->dev,418"unknown pending request 0x%02x cleared\n",419ucs->pending);420ucs->pending = 0;421}422423if (!ucs->pending)424del_timer(&ucs->timer_ctrl);425426spin_unlock_irqrestore(&ucs->lock, flags);427}428429/* cmd_in_timeout430* timeout routine for command input request431* argument:432* controller state structure433*/434static void cmd_in_timeout(unsigned long data)435{436struct cardstate *cs = (struct cardstate *) data;437struct bas_cardstate *ucs = cs->hw.bas;438int rc;439440if (!ucs->rcvbuf_size) {441gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);442return;443}444445if (ucs->retry_cmd_in++ >= BAS_RETRY) {446dev_err(cs->dev,447"control read: timeout, giving up after %d tries\n",448ucs->retry_cmd_in);449kfree(ucs->rcvbuf);450ucs->rcvbuf = NULL;451ucs->rcvbuf_size = 0;452error_reset(cs);453return;454}455456gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",457__func__, ucs->retry_cmd_in);458rc = atread_submit(cs, BAS_TIMEOUT);459if (rc < 0) {460kfree(ucs->rcvbuf);461ucs->rcvbuf = NULL;462ucs->rcvbuf_size = 0;463if (rc != -ENODEV)464error_reset(cs);465}466}467468/* read_ctrl_callback469* USB completion handler for control pipe input470* called by the USB subsystem in interrupt context471* parameter:472* urb USB request block473* urb->context = inbuf structure for controller state474*/475static void read_ctrl_callback(struct urb *urb)476{477struct inbuf_t *inbuf = urb->context;478struct cardstate *cs = inbuf->cs;479struct bas_cardstate *ucs = cs->hw.bas;480int status = urb->status;481unsigned numbytes;482int rc;483484update_basstate(ucs, 0, BS_ATRDPEND);485wake_up(&ucs->waitqueue);486del_timer(&ucs->timer_cmd_in);487488switch (status) {489case 0: /* normal completion */490numbytes = urb->actual_length;491if (unlikely(numbytes != ucs->rcvbuf_size)) {492dev_warn(cs->dev,493"control read: received %d chars, expected %d\n",494numbytes, ucs->rcvbuf_size);495if (numbytes > ucs->rcvbuf_size)496numbytes = ucs->rcvbuf_size;497}498499/* copy received bytes to inbuf, notify event layer */500if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {501gig_dbg(DEBUG_INTR, "%s-->BH", __func__);502gigaset_schedule_event(cs);503}504break;505506case -ENOENT: /* cancelled */507case -ECONNRESET: /* cancelled (async) */508case -EINPROGRESS: /* pending */509case -ENODEV: /* device removed */510case -ESHUTDOWN: /* device shut down */511/* no further action necessary */512gig_dbg(DEBUG_USBREQ, "%s: %s",513__func__, get_usb_statmsg(status));514break;515516default: /* other errors: retry */517if (ucs->retry_cmd_in++ < BAS_RETRY) {518gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,519get_usb_statmsg(status), ucs->retry_cmd_in);520rc = atread_submit(cs, BAS_TIMEOUT);521if (rc >= 0)522/* successfully resubmitted, skip freeing */523return;524if (rc == -ENODEV)525/* disconnect, no further action necessary */526break;527}528dev_err(cs->dev, "control read: %s, giving up after %d tries\n",529get_usb_statmsg(status), ucs->retry_cmd_in);530error_reset(cs);531}532533/* read finished, free buffer */534kfree(ucs->rcvbuf);535ucs->rcvbuf = NULL;536ucs->rcvbuf_size = 0;537}538539/* atread_submit540* submit an HD_READ_ATMESSAGE command URB and optionally start a timeout541* parameters:542* cs controller state structure543* timeout timeout in 1/10 sec., 0: none544* return value:545* 0 on success546* -EBUSY if another request is pending547* any URB submission error code548*/549static int atread_submit(struct cardstate *cs, int timeout)550{551struct bas_cardstate *ucs = cs->hw.bas;552int basstate;553int ret;554555gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",556ucs->rcvbuf_size);557558basstate = update_basstate(ucs, BS_ATRDPEND, 0);559if (basstate & BS_ATRDPEND) {560dev_err(cs->dev,561"could not submit HD_READ_ATMESSAGE: URB busy\n");562return -EBUSY;563}564565if (basstate & BS_SUSPEND) {566dev_notice(cs->dev,567"HD_READ_ATMESSAGE not submitted, "568"suspend in progress\n");569update_basstate(ucs, 0, BS_ATRDPEND);570/* treat like disconnect */571return -ENODEV;572}573574ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;575ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;576ucs->dr_cmd_in.wValue = 0;577ucs->dr_cmd_in.wIndex = 0;578ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);579usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,580usb_rcvctrlpipe(ucs->udev, 0),581(unsigned char *) &ucs->dr_cmd_in,582ucs->rcvbuf, ucs->rcvbuf_size,583read_ctrl_callback, cs->inbuf);584585ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);586if (ret != 0) {587update_basstate(ucs, 0, BS_ATRDPEND);588dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",589get_usb_rcmsg(ret));590return ret;591}592593if (timeout > 0) {594gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);595mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);596}597return 0;598}599600/* int_in_work601* workqueue routine to clear halt on interrupt in endpoint602*/603604static void int_in_work(struct work_struct *work)605{606struct bas_cardstate *ucs =607container_of(work, struct bas_cardstate, int_in_wq);608struct urb *urb = ucs->urb_int_in;609struct cardstate *cs = urb->context;610int rc;611612/* clear halt condition */613rc = usb_clear_halt(ucs->udev, urb->pipe);614gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));615if (rc == 0)616/* success, resubmit interrupt read URB */617rc = usb_submit_urb(urb, GFP_ATOMIC);618if (rc != 0 && rc != -ENODEV) {619dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));620rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);621if (rc == 0) {622rc = usb_reset_device(ucs->udev);623usb_unlock_device(ucs->udev);624}625}626ucs->retry_int_in = 0;627}628629/* int_in_resubmit630* timer routine for interrupt read delayed resubmit631* argument:632* controller state structure633*/634static void int_in_resubmit(unsigned long data)635{636struct cardstate *cs = (struct cardstate *) data;637struct bas_cardstate *ucs = cs->hw.bas;638int rc;639640if (ucs->retry_int_in++ >= BAS_RETRY) {641dev_err(cs->dev, "interrupt read: giving up after %d tries\n",642ucs->retry_int_in);643usb_queue_reset_device(ucs->interface);644return;645}646647gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);648rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);649if (rc != 0 && rc != -ENODEV) {650dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",651get_usb_rcmsg(rc));652usb_queue_reset_device(ucs->interface);653}654}655656/* read_int_callback657* USB completion handler for interrupt pipe input658* called by the USB subsystem in interrupt context659* parameter:660* urb USB request block661* urb->context = controller state structure662*/663static void read_int_callback(struct urb *urb)664{665struct cardstate *cs = urb->context;666struct bas_cardstate *ucs = cs->hw.bas;667struct bc_state *bcs;668int status = urb->status;669unsigned long flags;670int rc;671unsigned l;672int channel;673674switch (status) {675case 0: /* success */676ucs->retry_int_in = 0;677break;678case -EPIPE: /* endpoint stalled */679schedule_work(&ucs->int_in_wq);680/* fall through */681case -ENOENT: /* cancelled */682case -ECONNRESET: /* cancelled (async) */683case -EINPROGRESS: /* pending */684case -ENODEV: /* device removed */685case -ESHUTDOWN: /* device shut down */686/* no further action necessary */687gig_dbg(DEBUG_USBREQ, "%s: %s",688__func__, get_usb_statmsg(status));689return;690case -EPROTO: /* protocol error or unplug */691case -EILSEQ:692case -ETIME:693/* resubmit after delay */694gig_dbg(DEBUG_USBREQ, "%s: %s",695__func__, get_usb_statmsg(status));696mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);697return;698default: /* other errors: just resubmit */699dev_warn(cs->dev, "interrupt read: %s\n",700get_usb_statmsg(status));701goto resubmit;702}703704/* drop incomplete packets even if the missing bytes wouldn't matter */705if (unlikely(urb->actual_length < IP_MSGSIZE)) {706dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",707urb->actual_length);708goto resubmit;709}710711l = (unsigned) ucs->int_in_buf[1] +712(((unsigned) ucs->int_in_buf[2]) << 8);713714gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",715urb->actual_length, (int)ucs->int_in_buf[0], l,716(int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);717718channel = 0;719720switch (ucs->int_in_buf[0]) {721case HD_DEVICE_INIT_OK:722update_basstate(ucs, BS_INIT, 0);723break;724725case HD_READY_SEND_ATDATA:726del_timer(&ucs->timer_atrdy);727update_basstate(ucs, BS_ATREADY, BS_ATTIMER);728start_cbsend(cs);729break;730731case HD_OPEN_B2CHANNEL_ACK:732++channel;733case HD_OPEN_B1CHANNEL_ACK:734bcs = cs->bcs + channel;735update_basstate(ucs, BS_B1OPEN << channel, 0);736gigaset_bchannel_up(bcs);737break;738739case HD_OPEN_ATCHANNEL_ACK:740update_basstate(ucs, BS_ATOPEN, 0);741start_cbsend(cs);742break;743744case HD_CLOSE_B2CHANNEL_ACK:745++channel;746case HD_CLOSE_B1CHANNEL_ACK:747bcs = cs->bcs + channel;748update_basstate(ucs, 0, BS_B1OPEN << channel);749stopurbs(bcs->hw.bas);750gigaset_bchannel_down(bcs);751break;752753case HD_CLOSE_ATCHANNEL_ACK:754update_basstate(ucs, 0, BS_ATOPEN);755break;756757case HD_B2_FLOW_CONTROL:758++channel;759case HD_B1_FLOW_CONTROL:760bcs = cs->bcs + channel;761atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,762&bcs->hw.bas->corrbytes);763gig_dbg(DEBUG_ISO,764"Flow control (channel %d, sub %d): 0x%02x => %d",765channel, bcs->hw.bas->numsub, l,766atomic_read(&bcs->hw.bas->corrbytes));767break;768769case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */770if (!l) {771dev_warn(cs->dev,772"HD_RECEIVEATDATA_ACK with length 0 ignored\n");773break;774}775spin_lock_irqsave(&cs->lock, flags);776if (ucs->basstate & BS_ATRDPEND) {777spin_unlock_irqrestore(&cs->lock, flags);778dev_warn(cs->dev,779"HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",780l, ucs->rcvbuf_size);781break;782}783if (ucs->rcvbuf_size) {784/* throw away previous buffer - we have no queue */785dev_err(cs->dev,786"receive AT data overrun, %d bytes lost\n",787ucs->rcvbuf_size);788kfree(ucs->rcvbuf);789ucs->rcvbuf_size = 0;790}791ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);792if (ucs->rcvbuf == NULL) {793spin_unlock_irqrestore(&cs->lock, flags);794dev_err(cs->dev, "out of memory receiving AT data\n");795break;796}797ucs->rcvbuf_size = l;798ucs->retry_cmd_in = 0;799rc = atread_submit(cs, BAS_TIMEOUT);800if (rc < 0) {801kfree(ucs->rcvbuf);802ucs->rcvbuf = NULL;803ucs->rcvbuf_size = 0;804}805spin_unlock_irqrestore(&cs->lock, flags);806if (rc < 0 && rc != -ENODEV)807error_reset(cs);808break;809810case HD_RESET_INTERRUPT_PIPE_ACK:811update_basstate(ucs, 0, BS_RESETTING);812dev_notice(cs->dev, "interrupt pipe reset\n");813break;814815case HD_SUSPEND_END:816gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");817break;818819default:820dev_warn(cs->dev,821"unknown Gigaset signal 0x%02x (%u) ignored\n",822(int) ucs->int_in_buf[0], l);823}824825check_pending(ucs);826wake_up(&ucs->waitqueue);827828resubmit:829rc = usb_submit_urb(urb, GFP_ATOMIC);830if (unlikely(rc != 0 && rc != -ENODEV)) {831dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",832get_usb_rcmsg(rc));833error_reset(cs);834}835}836837/* read_iso_callback838* USB completion handler for B channel isochronous input839* called by the USB subsystem in interrupt context840* parameter:841* urb USB request block of completed request842* urb->context = bc_state structure843*/844static void read_iso_callback(struct urb *urb)845{846struct bc_state *bcs;847struct bas_bc_state *ubc;848int status = urb->status;849unsigned long flags;850int i, rc;851852/* status codes not worth bothering the tasklet with */853if (unlikely(status == -ENOENT ||854status == -ECONNRESET ||855status == -EINPROGRESS ||856status == -ENODEV ||857status == -ESHUTDOWN)) {858gig_dbg(DEBUG_ISO, "%s: %s",859__func__, get_usb_statmsg(status));860return;861}862863bcs = urb->context;864ubc = bcs->hw.bas;865866spin_lock_irqsave(&ubc->isoinlock, flags);867if (likely(ubc->isoindone == NULL)) {868/* pass URB to tasklet */869ubc->isoindone = urb;870ubc->isoinstatus = status;871tasklet_hi_schedule(&ubc->rcvd_tasklet);872} else {873/* tasklet still busy, drop data and resubmit URB */874gig_dbg(DEBUG_ISO, "%s: overrun", __func__);875ubc->loststatus = status;876for (i = 0; i < BAS_NUMFRAMES; i++) {877ubc->isoinlost += urb->iso_frame_desc[i].actual_length;878if (unlikely(urb->iso_frame_desc[i].status != 0 &&879urb->iso_frame_desc[i].status !=880-EINPROGRESS))881ubc->loststatus = urb->iso_frame_desc[i].status;882urb->iso_frame_desc[i].status = 0;883urb->iso_frame_desc[i].actual_length = 0;884}885if (likely(ubc->running)) {886/* urb->dev is clobbered by USB subsystem */887urb->dev = bcs->cs->hw.bas->udev;888urb->transfer_flags = URB_ISO_ASAP;889urb->number_of_packets = BAS_NUMFRAMES;890rc = usb_submit_urb(urb, GFP_ATOMIC);891if (unlikely(rc != 0 && rc != -ENODEV)) {892dev_err(bcs->cs->dev,893"could not resubmit isoc read URB: %s\n",894get_usb_rcmsg(rc));895dump_urb(DEBUG_ISO, "isoc read", urb);896error_hangup(bcs);897}898}899}900spin_unlock_irqrestore(&ubc->isoinlock, flags);901}902903/* write_iso_callback904* USB completion handler for B channel isochronous output905* called by the USB subsystem in interrupt context906* parameter:907* urb USB request block of completed request908* urb->context = isow_urbctx_t structure909*/910static void write_iso_callback(struct urb *urb)911{912struct isow_urbctx_t *ucx;913struct bas_bc_state *ubc;914int status = urb->status;915unsigned long flags;916917/* status codes not worth bothering the tasklet with */918if (unlikely(status == -ENOENT ||919status == -ECONNRESET ||920status == -EINPROGRESS ||921status == -ENODEV ||922status == -ESHUTDOWN)) {923gig_dbg(DEBUG_ISO, "%s: %s",924__func__, get_usb_statmsg(status));925return;926}927928/* pass URB context to tasklet */929ucx = urb->context;930ubc = ucx->bcs->hw.bas;931ucx->status = status;932933spin_lock_irqsave(&ubc->isooutlock, flags);934ubc->isooutovfl = ubc->isooutdone;935ubc->isooutdone = ucx;936spin_unlock_irqrestore(&ubc->isooutlock, flags);937tasklet_hi_schedule(&ubc->sent_tasklet);938}939940/* starturbs941* prepare and submit USB request blocks for isochronous input and output942* argument:943* B channel control structure944* return value:945* 0 on success946* < 0 on error (no URBs submitted)947*/948static int starturbs(struct bc_state *bcs)949{950struct bas_bc_state *ubc = bcs->hw.bas;951struct urb *urb;952int j, k;953int rc;954955/* initialize L2 reception */956if (bcs->proto2 == L2_HDLC)957bcs->inputstate |= INS_flag_hunt;958959/* submit all isochronous input URBs */960ubc->running = 1;961for (k = 0; k < BAS_INURBS; k++) {962urb = ubc->isoinurbs[k];963if (!urb) {964rc = -EFAULT;965goto error;966}967968urb->dev = bcs->cs->hw.bas->udev;969urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);970urb->transfer_flags = URB_ISO_ASAP;971urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;972urb->transfer_buffer_length = BAS_INBUFSIZE;973urb->number_of_packets = BAS_NUMFRAMES;974urb->interval = BAS_FRAMETIME;975urb->complete = read_iso_callback;976urb->context = bcs;977for (j = 0; j < BAS_NUMFRAMES; j++) {978urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;979urb->iso_frame_desc[j].length = BAS_MAXFRAME;980urb->iso_frame_desc[j].status = 0;981urb->iso_frame_desc[j].actual_length = 0;982}983984dump_urb(DEBUG_ISO, "Initial isoc read", urb);985rc = usb_submit_urb(urb, GFP_ATOMIC);986if (rc != 0)987goto error;988}989990/* initialize L2 transmission */991gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);992993/* set up isochronous output URBs for flag idling */994for (k = 0; k < BAS_OUTURBS; ++k) {995urb = ubc->isoouturbs[k].urb;996if (!urb) {997rc = -EFAULT;998goto error;999}1000urb->dev = bcs->cs->hw.bas->udev;1001urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);1002urb->transfer_flags = URB_ISO_ASAP;1003urb->transfer_buffer = ubc->isooutbuf->data;1004urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);1005urb->number_of_packets = BAS_NUMFRAMES;1006urb->interval = BAS_FRAMETIME;1007urb->complete = write_iso_callback;1008urb->context = &ubc->isoouturbs[k];1009for (j = 0; j < BAS_NUMFRAMES; ++j) {1010urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;1011urb->iso_frame_desc[j].length = BAS_NORMFRAME;1012urb->iso_frame_desc[j].status = 0;1013urb->iso_frame_desc[j].actual_length = 0;1014}1015ubc->isoouturbs[k].limit = -1;1016}10171018/* keep one URB free, submit the others */1019for (k = 0; k < BAS_OUTURBS-1; ++k) {1020dump_urb(DEBUG_ISO, "Initial isoc write", urb);1021rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);1022if (rc != 0)1023goto error;1024}1025dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);1026ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];1027ubc->isooutdone = ubc->isooutovfl = NULL;1028return 0;1029error:1030stopurbs(ubc);1031return rc;1032}10331034/* stopurbs1035* cancel the USB request blocks for isochronous input and output1036* errors are silently ignored1037* argument:1038* B channel control structure1039*/1040static void stopurbs(struct bas_bc_state *ubc)1041{1042int k, rc;10431044ubc->running = 0;10451046for (k = 0; k < BAS_INURBS; ++k) {1047rc = usb_unlink_urb(ubc->isoinurbs[k]);1048gig_dbg(DEBUG_ISO,1049"%s: isoc input URB %d unlinked, result = %s",1050__func__, k, get_usb_rcmsg(rc));1051}10521053for (k = 0; k < BAS_OUTURBS; ++k) {1054rc = usb_unlink_urb(ubc->isoouturbs[k].urb);1055gig_dbg(DEBUG_ISO,1056"%s: isoc output URB %d unlinked, result = %s",1057__func__, k, get_usb_rcmsg(rc));1058}1059}10601061/* Isochronous Write - Bottom Half */1062/* =============================== */10631064/* submit_iso_write_urb1065* fill and submit the next isochronous write URB1066* parameters:1067* ucx context structure containing URB1068* return value:1069* number of frames submitted in URB1070* 0 if URB not submitted because no data available (isooutbuf busy)1071* error code < 0 on error1072*/1073static int submit_iso_write_urb(struct isow_urbctx_t *ucx)1074{1075struct urb *urb = ucx->urb;1076struct bas_bc_state *ubc = ucx->bcs->hw.bas;1077struct usb_iso_packet_descriptor *ifd;1078int corrbytes, nframe, rc;10791080/* urb->dev is clobbered by USB subsystem */1081urb->dev = ucx->bcs->cs->hw.bas->udev;1082urb->transfer_flags = URB_ISO_ASAP;1083urb->transfer_buffer = ubc->isooutbuf->data;1084urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);10851086for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {1087ifd = &urb->iso_frame_desc[nframe];10881089/* compute frame length according to flow control */1090ifd->length = BAS_NORMFRAME;1091corrbytes = atomic_read(&ubc->corrbytes);1092if (corrbytes != 0) {1093gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",1094__func__, corrbytes);1095if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)1096corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;1097else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)1098corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;1099ifd->length += corrbytes;1100atomic_add(-corrbytes, &ubc->corrbytes);1101}11021103/* retrieve block of data to send */1104rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);1105if (rc < 0) {1106if (rc == -EBUSY) {1107gig_dbg(DEBUG_ISO,1108"%s: buffer busy at frame %d",1109__func__, nframe);1110/* tasklet will be restarted from1111gigaset_isoc_send_skb() */1112} else {1113dev_err(ucx->bcs->cs->dev,1114"%s: buffer error %d at frame %d\n",1115__func__, rc, nframe);1116return rc;1117}1118break;1119}1120ifd->offset = rc;1121ucx->limit = ubc->isooutbuf->nextread;1122ifd->status = 0;1123ifd->actual_length = 0;1124}1125if (unlikely(nframe == 0))1126return 0; /* no data to send */1127urb->number_of_packets = nframe;11281129rc = usb_submit_urb(urb, GFP_ATOMIC);1130if (unlikely(rc)) {1131if (rc == -ENODEV)1132/* device removed - give up silently */1133gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);1134else1135dev_err(ucx->bcs->cs->dev,1136"could not submit isoc write URB: %s\n",1137get_usb_rcmsg(rc));1138return rc;1139}1140++ubc->numsub;1141return nframe;1142}11431144/* write_iso_tasklet1145* tasklet scheduled when an isochronous output URB from the Gigaset device1146* has completed1147* parameter:1148* data B channel state structure1149*/1150static void write_iso_tasklet(unsigned long data)1151{1152struct bc_state *bcs = (struct bc_state *) data;1153struct bas_bc_state *ubc = bcs->hw.bas;1154struct cardstate *cs = bcs->cs;1155struct isow_urbctx_t *done, *next, *ovfl;1156struct urb *urb;1157int status;1158struct usb_iso_packet_descriptor *ifd;1159unsigned long flags;1160int i;1161struct sk_buff *skb;1162int len;1163int rc;11641165/* loop while completed URBs arrive in time */1166for (;;) {1167if (unlikely(!(ubc->running))) {1168gig_dbg(DEBUG_ISO, "%s: not running", __func__);1169return;1170}11711172/* retrieve completed URBs */1173spin_lock_irqsave(&ubc->isooutlock, flags);1174done = ubc->isooutdone;1175ubc->isooutdone = NULL;1176ovfl = ubc->isooutovfl;1177ubc->isooutovfl = NULL;1178spin_unlock_irqrestore(&ubc->isooutlock, flags);1179if (ovfl) {1180dev_err(cs->dev, "isoc write underrun\n");1181error_hangup(bcs);1182break;1183}1184if (!done)1185break;11861187/* submit free URB if available */1188spin_lock_irqsave(&ubc->isooutlock, flags);1189next = ubc->isooutfree;1190ubc->isooutfree = NULL;1191spin_unlock_irqrestore(&ubc->isooutlock, flags);1192if (next) {1193rc = submit_iso_write_urb(next);1194if (unlikely(rc <= 0 && rc != -ENODEV)) {1195/* could not submit URB, put it back */1196spin_lock_irqsave(&ubc->isooutlock, flags);1197if (ubc->isooutfree == NULL) {1198ubc->isooutfree = next;1199next = NULL;1200}1201spin_unlock_irqrestore(&ubc->isooutlock, flags);1202if (next) {1203/* couldn't put it back */1204dev_err(cs->dev,1205"losing isoc write URB\n");1206error_hangup(bcs);1207}1208}1209}12101211/* process completed URB */1212urb = done->urb;1213status = done->status;1214switch (status) {1215case -EXDEV: /* partial completion */1216gig_dbg(DEBUG_ISO, "%s: URB partially completed",1217__func__);1218/* fall through - what's the difference anyway? */1219case 0: /* normal completion */1220/* inspect individual frames1221* assumptions (for lack of documentation):1222* - actual_length bytes of first frame in error are1223* successfully sent1224* - all following frames are not sent at all1225*/1226for (i = 0; i < BAS_NUMFRAMES; i++) {1227ifd = &urb->iso_frame_desc[i];1228if (ifd->status ||1229ifd->actual_length != ifd->length) {1230dev_warn(cs->dev,1231"isoc write: frame %d[%d/%d]: %s\n",1232i, ifd->actual_length,1233ifd->length,1234get_usb_statmsg(ifd->status));1235break;1236}1237}1238break;1239case -EPIPE: /* stall - probably underrun */1240dev_err(cs->dev, "isoc write: stalled\n");1241error_hangup(bcs);1242break;1243default: /* other errors */1244dev_warn(cs->dev, "isoc write: %s\n",1245get_usb_statmsg(status));1246}12471248/* mark the write buffer area covered by this URB as free */1249if (done->limit >= 0)1250ubc->isooutbuf->read = done->limit;12511252/* mark URB as free */1253spin_lock_irqsave(&ubc->isooutlock, flags);1254next = ubc->isooutfree;1255ubc->isooutfree = done;1256spin_unlock_irqrestore(&ubc->isooutlock, flags);1257if (next) {1258/* only one URB still active - resubmit one */1259rc = submit_iso_write_urb(next);1260if (unlikely(rc <= 0 && rc != -ENODEV)) {1261/* couldn't submit */1262error_hangup(bcs);1263}1264}1265}12661267/* process queued SKBs */1268while ((skb = skb_dequeue(&bcs->squeue))) {1269/* copy to output buffer, doing L2 encapsulation */1270len = skb->len;1271if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {1272/* insufficient buffer space, push back onto queue */1273skb_queue_head(&bcs->squeue, skb);1274gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",1275__func__, skb_queue_len(&bcs->squeue));1276break;1277}1278skb_pull(skb, len);1279gigaset_skb_sent(bcs, skb);1280dev_kfree_skb_any(skb);1281}1282}12831284/* Isochronous Read - Bottom Half */1285/* ============================== */12861287/* read_iso_tasklet1288* tasklet scheduled when an isochronous input URB from the Gigaset device1289* has completed1290* parameter:1291* data B channel state structure1292*/1293static void read_iso_tasklet(unsigned long data)1294{1295struct bc_state *bcs = (struct bc_state *) data;1296struct bas_bc_state *ubc = bcs->hw.bas;1297struct cardstate *cs = bcs->cs;1298struct urb *urb;1299int status;1300struct usb_iso_packet_descriptor *ifd;1301char *rcvbuf;1302unsigned long flags;1303int totleft, numbytes, offset, frame, rc;13041305/* loop while more completed URBs arrive in the meantime */1306for (;;) {1307/* retrieve URB */1308spin_lock_irqsave(&ubc->isoinlock, flags);1309urb = ubc->isoindone;1310if (!urb) {1311spin_unlock_irqrestore(&ubc->isoinlock, flags);1312return;1313}1314status = ubc->isoinstatus;1315ubc->isoindone = NULL;1316if (unlikely(ubc->loststatus != -EINPROGRESS)) {1317dev_warn(cs->dev,1318"isoc read overrun, URB dropped (status: %s, %d bytes)\n",1319get_usb_statmsg(ubc->loststatus),1320ubc->isoinlost);1321ubc->loststatus = -EINPROGRESS;1322}1323spin_unlock_irqrestore(&ubc->isoinlock, flags);13241325if (unlikely(!(ubc->running))) {1326gig_dbg(DEBUG_ISO,1327"%s: channel not running, "1328"dropped URB with status: %s",1329__func__, get_usb_statmsg(status));1330return;1331}13321333switch (status) {1334case 0: /* normal completion */1335break;1336case -EXDEV: /* inspect individual frames1337(we do that anyway) */1338gig_dbg(DEBUG_ISO, "%s: URB partially completed",1339__func__);1340break;1341case -ENOENT:1342case -ECONNRESET:1343case -EINPROGRESS:1344gig_dbg(DEBUG_ISO, "%s: %s",1345__func__, get_usb_statmsg(status));1346continue; /* -> skip */1347case -EPIPE:1348dev_err(cs->dev, "isoc read: stalled\n");1349error_hangup(bcs);1350continue; /* -> skip */1351default: /* other error */1352dev_warn(cs->dev, "isoc read: %s\n",1353get_usb_statmsg(status));1354goto error;1355}13561357rcvbuf = urb->transfer_buffer;1358totleft = urb->actual_length;1359for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {1360ifd = &urb->iso_frame_desc[frame];1361numbytes = ifd->actual_length;1362switch (ifd->status) {1363case 0: /* success */1364break;1365case -EPROTO: /* protocol error or unplug */1366case -EILSEQ:1367case -ETIME:1368/* probably just disconnected, ignore */1369gig_dbg(DEBUG_ISO,1370"isoc read: frame %d[%d]: %s\n",1371frame, numbytes,1372get_usb_statmsg(ifd->status));1373break;1374default: /* other error */1375/* report, assume transferred bytes are ok */1376dev_warn(cs->dev,1377"isoc read: frame %d[%d]: %s\n",1378frame, numbytes,1379get_usb_statmsg(ifd->status));1380}1381if (unlikely(numbytes > BAS_MAXFRAME))1382dev_warn(cs->dev,1383"isoc read: frame %d[%d]: %s\n",1384frame, numbytes,1385"exceeds max frame size");1386if (unlikely(numbytes > totleft)) {1387dev_warn(cs->dev,1388"isoc read: frame %d[%d]: %s\n",1389frame, numbytes,1390"exceeds total transfer length");1391numbytes = totleft;1392}1393offset = ifd->offset;1394if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {1395dev_warn(cs->dev,1396"isoc read: frame %d[%d]: %s\n",1397frame, numbytes,1398"exceeds end of buffer");1399numbytes = BAS_INBUFSIZE - offset;1400}1401gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);1402totleft -= numbytes;1403}1404if (unlikely(totleft > 0))1405dev_warn(cs->dev, "isoc read: %d data bytes missing\n",1406totleft);14071408error:1409/* URB processed, resubmit */1410for (frame = 0; frame < BAS_NUMFRAMES; frame++) {1411urb->iso_frame_desc[frame].status = 0;1412urb->iso_frame_desc[frame].actual_length = 0;1413}1414/* urb->dev is clobbered by USB subsystem */1415urb->dev = bcs->cs->hw.bas->udev;1416urb->transfer_flags = URB_ISO_ASAP;1417urb->number_of_packets = BAS_NUMFRAMES;1418rc = usb_submit_urb(urb, GFP_ATOMIC);1419if (unlikely(rc != 0 && rc != -ENODEV)) {1420dev_err(cs->dev,1421"could not resubmit isoc read URB: %s\n",1422get_usb_rcmsg(rc));1423dump_urb(DEBUG_ISO, "resubmit isoc read", urb);1424error_hangup(bcs);1425}1426}1427}14281429/* Channel Operations */1430/* ================== */14311432/* req_timeout1433* timeout routine for control output request1434* argument:1435* controller state structure1436*/1437static void req_timeout(unsigned long data)1438{1439struct cardstate *cs = (struct cardstate *) data;1440struct bas_cardstate *ucs = cs->hw.bas;1441int pending;1442unsigned long flags;14431444check_pending(ucs);14451446spin_lock_irqsave(&ucs->lock, flags);1447pending = ucs->pending;1448ucs->pending = 0;1449spin_unlock_irqrestore(&ucs->lock, flags);14501451switch (pending) {1452case 0: /* no pending request */1453gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);1454break;14551456case HD_OPEN_ATCHANNEL:1457dev_err(cs->dev, "timeout opening AT channel\n");1458error_reset(cs);1459break;14601461case HD_OPEN_B1CHANNEL:1462dev_err(cs->dev, "timeout opening channel 1\n");1463error_hangup(&cs->bcs[0]);1464break;14651466case HD_OPEN_B2CHANNEL:1467dev_err(cs->dev, "timeout opening channel 2\n");1468error_hangup(&cs->bcs[1]);1469break;14701471case HD_CLOSE_ATCHANNEL:1472dev_err(cs->dev, "timeout closing AT channel\n");1473error_reset(cs);1474break;14751476case HD_CLOSE_B1CHANNEL:1477dev_err(cs->dev, "timeout closing channel 1\n");1478error_reset(cs);1479break;14801481case HD_CLOSE_B2CHANNEL:1482dev_err(cs->dev, "timeout closing channel 2\n");1483error_reset(cs);1484break;14851486case HD_RESET_INTERRUPT_PIPE:1487/* error recovery escalation */1488dev_err(cs->dev,1489"reset interrupt pipe timeout, attempting USB reset\n");1490usb_queue_reset_device(ucs->interface);1491break;14921493default:1494dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",1495pending);1496}14971498wake_up(&ucs->waitqueue);1499}15001501/* write_ctrl_callback1502* USB completion handler for control pipe output1503* called by the USB subsystem in interrupt context1504* parameter:1505* urb USB request block of completed request1506* urb->context = hardware specific controller state structure1507*/1508static void write_ctrl_callback(struct urb *urb)1509{1510struct bas_cardstate *ucs = urb->context;1511int status = urb->status;1512int rc;1513unsigned long flags;15141515/* check status */1516switch (status) {1517case 0: /* normal completion */1518spin_lock_irqsave(&ucs->lock, flags);1519switch (ucs->pending) {1520case HD_DEVICE_INIT_ACK: /* no reply expected */1521del_timer(&ucs->timer_ctrl);1522ucs->pending = 0;1523break;1524}1525spin_unlock_irqrestore(&ucs->lock, flags);1526return;15271528case -ENOENT: /* cancelled */1529case -ECONNRESET: /* cancelled (async) */1530case -EINPROGRESS: /* pending */1531case -ENODEV: /* device removed */1532case -ESHUTDOWN: /* device shut down */1533/* ignore silently */1534gig_dbg(DEBUG_USBREQ, "%s: %s",1535__func__, get_usb_statmsg(status));1536break;15371538default: /* any failure */1539/* don't retry if suspend requested */1540if (++ucs->retry_ctrl > BAS_RETRY ||1541(ucs->basstate & BS_SUSPEND)) {1542dev_err(&ucs->interface->dev,1543"control request 0x%02x failed: %s\n",1544ucs->dr_ctrl.bRequest,1545get_usb_statmsg(status));1546break; /* give up */1547}1548dev_notice(&ucs->interface->dev,1549"control request 0x%02x: %s, retry %d\n",1550ucs->dr_ctrl.bRequest, get_usb_statmsg(status),1551ucs->retry_ctrl);1552/* urb->dev is clobbered by USB subsystem */1553urb->dev = ucs->udev;1554rc = usb_submit_urb(urb, GFP_ATOMIC);1555if (unlikely(rc)) {1556dev_err(&ucs->interface->dev,1557"could not resubmit request 0x%02x: %s\n",1558ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));1559break;1560}1561/* resubmitted */1562return;1563}15641565/* failed, clear pending request */1566spin_lock_irqsave(&ucs->lock, flags);1567del_timer(&ucs->timer_ctrl);1568ucs->pending = 0;1569spin_unlock_irqrestore(&ucs->lock, flags);1570wake_up(&ucs->waitqueue);1571}15721573/* req_submit1574* submit a control output request without message buffer to the Gigaset base1575* and optionally start a timeout1576* parameters:1577* bcs B channel control structure1578* req control request code (HD_*)1579* val control request parameter value (set to 0 if unused)1580* timeout timeout in seconds (0: no timeout)1581* return value:1582* 0 on success1583* -EBUSY if another request is pending1584* any URB submission error code1585*/1586static int req_submit(struct bc_state *bcs, int req, int val, int timeout)1587{1588struct bas_cardstate *ucs = bcs->cs->hw.bas;1589int ret;1590unsigned long flags;15911592gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);15931594spin_lock_irqsave(&ucs->lock, flags);1595if (ucs->pending) {1596spin_unlock_irqrestore(&ucs->lock, flags);1597dev_err(bcs->cs->dev,1598"submission of request 0x%02x failed: "1599"request 0x%02x still pending\n",1600req, ucs->pending);1601return -EBUSY;1602}16031604ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;1605ucs->dr_ctrl.bRequest = req;1606ucs->dr_ctrl.wValue = cpu_to_le16(val);1607ucs->dr_ctrl.wIndex = 0;1608ucs->dr_ctrl.wLength = 0;1609usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,1610usb_sndctrlpipe(ucs->udev, 0),1611(unsigned char *) &ucs->dr_ctrl, NULL, 0,1612write_ctrl_callback, ucs);1613ucs->retry_ctrl = 0;1614ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);1615if (unlikely(ret)) {1616dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",1617req, get_usb_rcmsg(ret));1618spin_unlock_irqrestore(&ucs->lock, flags);1619return ret;1620}1621ucs->pending = req;16221623if (timeout > 0) {1624gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);1625mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);1626}16271628spin_unlock_irqrestore(&ucs->lock, flags);1629return 0;1630}16311632/* gigaset_init_bchannel1633* called by common.c to connect a B channel1634* initialize isochronous I/O and tell the Gigaset base to open the channel1635* argument:1636* B channel control structure1637* return value:1638* 0 on success, error code < 0 on error1639*/1640static int gigaset_init_bchannel(struct bc_state *bcs)1641{1642struct cardstate *cs = bcs->cs;1643int req, ret;1644unsigned long flags;16451646spin_lock_irqsave(&cs->lock, flags);1647if (unlikely(!cs->connected)) {1648gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);1649spin_unlock_irqrestore(&cs->lock, flags);1650return -ENODEV;1651}16521653if (cs->hw.bas->basstate & BS_SUSPEND) {1654dev_notice(cs->dev,1655"not starting isoc I/O, suspend in progress\n");1656spin_unlock_irqrestore(&cs->lock, flags);1657return -EHOSTUNREACH;1658}16591660ret = starturbs(bcs);1661if (ret < 0) {1662spin_unlock_irqrestore(&cs->lock, flags);1663dev_err(cs->dev,1664"could not start isoc I/O for channel B%d: %s\n",1665bcs->channel + 1,1666ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));1667if (ret != -ENODEV)1668error_hangup(bcs);1669return ret;1670}16711672req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;1673ret = req_submit(bcs, req, 0, BAS_TIMEOUT);1674if (ret < 0) {1675dev_err(cs->dev, "could not open channel B%d\n",1676bcs->channel + 1);1677stopurbs(bcs->hw.bas);1678}16791680spin_unlock_irqrestore(&cs->lock, flags);1681if (ret < 0 && ret != -ENODEV)1682error_hangup(bcs);1683return ret;1684}16851686/* gigaset_close_bchannel1687* called by common.c to disconnect a B channel1688* tell the Gigaset base to close the channel1689* stopping isochronous I/O and LL notification will be done when the1690* acknowledgement for the close arrives1691* argument:1692* B channel control structure1693* return value:1694* 0 on success, error code < 0 on error1695*/1696static int gigaset_close_bchannel(struct bc_state *bcs)1697{1698struct cardstate *cs = bcs->cs;1699int req, ret;1700unsigned long flags;17011702spin_lock_irqsave(&cs->lock, flags);1703if (unlikely(!cs->connected)) {1704spin_unlock_irqrestore(&cs->lock, flags);1705gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);1706return -ENODEV;1707}17081709if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {1710/* channel not running: just signal common.c */1711spin_unlock_irqrestore(&cs->lock, flags);1712gigaset_bchannel_down(bcs);1713return 0;1714}17151716/* channel running: tell device to close it */1717req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;1718ret = req_submit(bcs, req, 0, BAS_TIMEOUT);1719if (ret < 0)1720dev_err(cs->dev, "closing channel B%d failed\n",1721bcs->channel + 1);17221723spin_unlock_irqrestore(&cs->lock, flags);1724return ret;1725}17261727/* Device Operations */1728/* ================= */17291730/* complete_cb1731* unqueue first command buffer from queue, waking any sleepers1732* must be called with cs->cmdlock held1733* parameter:1734* cs controller state structure1735*/1736static void complete_cb(struct cardstate *cs)1737{1738struct cmdbuf_t *cb = cs->cmdbuf;17391740/* unqueue completed buffer */1741cs->cmdbytes -= cs->curlen;1742gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",1743cs->curlen, cs->cmdbytes);1744if (cb->next != NULL) {1745cs->cmdbuf = cb->next;1746cs->cmdbuf->prev = NULL;1747cs->curlen = cs->cmdbuf->len;1748} else {1749cs->cmdbuf = NULL;1750cs->lastcmdbuf = NULL;1751cs->curlen = 0;1752}17531754if (cb->wake_tasklet)1755tasklet_schedule(cb->wake_tasklet);17561757kfree(cb);1758}17591760/* write_command_callback1761* USB completion handler for AT command transmission1762* called by the USB subsystem in interrupt context1763* parameter:1764* urb USB request block of completed request1765* urb->context = controller state structure1766*/1767static void write_command_callback(struct urb *urb)1768{1769struct cardstate *cs = urb->context;1770struct bas_cardstate *ucs = cs->hw.bas;1771int status = urb->status;1772unsigned long flags;17731774update_basstate(ucs, 0, BS_ATWRPEND);1775wake_up(&ucs->waitqueue);17761777/* check status */1778switch (status) {1779case 0: /* normal completion */1780break;1781case -ENOENT: /* cancelled */1782case -ECONNRESET: /* cancelled (async) */1783case -EINPROGRESS: /* pending */1784case -ENODEV: /* device removed */1785case -ESHUTDOWN: /* device shut down */1786/* ignore silently */1787gig_dbg(DEBUG_USBREQ, "%s: %s",1788__func__, get_usb_statmsg(status));1789return;1790default: /* any failure */1791if (++ucs->retry_cmd_out > BAS_RETRY) {1792dev_warn(cs->dev,1793"command write: %s, "1794"giving up after %d retries\n",1795get_usb_statmsg(status),1796ucs->retry_cmd_out);1797break;1798}1799if (ucs->basstate & BS_SUSPEND) {1800dev_warn(cs->dev,1801"command write: %s, "1802"won't retry - suspend requested\n",1803get_usb_statmsg(status));1804break;1805}1806if (cs->cmdbuf == NULL) {1807dev_warn(cs->dev,1808"command write: %s, "1809"cannot retry - cmdbuf gone\n",1810get_usb_statmsg(status));1811break;1812}1813dev_notice(cs->dev, "command write: %s, retry %d\n",1814get_usb_statmsg(status), ucs->retry_cmd_out);1815if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)1816/* resubmitted - bypass regular exit block */1817return;1818/* command send failed, assume base still waiting */1819update_basstate(ucs, BS_ATREADY, 0);1820}18211822spin_lock_irqsave(&cs->cmdlock, flags);1823if (cs->cmdbuf != NULL)1824complete_cb(cs);1825spin_unlock_irqrestore(&cs->cmdlock, flags);1826}18271828/* atrdy_timeout1829* timeout routine for AT command transmission1830* argument:1831* controller state structure1832*/1833static void atrdy_timeout(unsigned long data)1834{1835struct cardstate *cs = (struct cardstate *) data;1836struct bas_cardstate *ucs = cs->hw.bas;18371838dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");18391840/* fake the missing signal - what else can I do? */1841update_basstate(ucs, BS_ATREADY, BS_ATTIMER);1842start_cbsend(cs);1843}18441845/* atwrite_submit1846* submit an HD_WRITE_ATMESSAGE command URB1847* parameters:1848* cs controller state structure1849* buf buffer containing command to send1850* len length of command to send1851* return value:1852* 0 on success1853* -EBUSY if another request is pending1854* any URB submission error code1855*/1856static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)1857{1858struct bas_cardstate *ucs = cs->hw.bas;1859int rc;18601861gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);18621863if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {1864dev_err(cs->dev,1865"could not submit HD_WRITE_ATMESSAGE: URB busy\n");1866return -EBUSY;1867}18681869ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;1870ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;1871ucs->dr_cmd_out.wValue = 0;1872ucs->dr_cmd_out.wIndex = 0;1873ucs->dr_cmd_out.wLength = cpu_to_le16(len);1874usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,1875usb_sndctrlpipe(ucs->udev, 0),1876(unsigned char *) &ucs->dr_cmd_out, buf, len,1877write_command_callback, cs);1878rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);1879if (unlikely(rc)) {1880update_basstate(ucs, 0, BS_ATWRPEND);1881dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",1882get_usb_rcmsg(rc));1883return rc;1884}18851886/* submitted successfully, start timeout if necessary */1887if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {1888gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",1889ATRDY_TIMEOUT);1890mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);1891}1892return 0;1893}18941895/* start_cbsend1896* start transmission of AT command queue if necessary1897* parameter:1898* cs controller state structure1899* return value:1900* 0 on success1901* error code < 0 on error1902*/1903static int start_cbsend(struct cardstate *cs)1904{1905struct cmdbuf_t *cb;1906struct bas_cardstate *ucs = cs->hw.bas;1907unsigned long flags;1908int rc;1909int retval = 0;19101911/* check if suspend requested */1912if (ucs->basstate & BS_SUSPEND) {1913gig_dbg(DEBUG_OUTPUT, "suspending");1914return -EHOSTUNREACH;1915}19161917/* check if AT channel is open */1918if (!(ucs->basstate & BS_ATOPEN)) {1919gig_dbg(DEBUG_OUTPUT, "AT channel not open");1920rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);1921if (rc < 0) {1922/* flush command queue */1923spin_lock_irqsave(&cs->cmdlock, flags);1924while (cs->cmdbuf != NULL)1925complete_cb(cs);1926spin_unlock_irqrestore(&cs->cmdlock, flags);1927}1928return rc;1929}19301931/* try to send first command in queue */1932spin_lock_irqsave(&cs->cmdlock, flags);19331934while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {1935ucs->retry_cmd_out = 0;1936rc = atwrite_submit(cs, cb->buf, cb->len);1937if (unlikely(rc)) {1938retval = rc;1939complete_cb(cs);1940}1941}19421943spin_unlock_irqrestore(&cs->cmdlock, flags);1944return retval;1945}19461947/* gigaset_write_cmd1948* This function is called by the device independent part of the driver1949* to transmit an AT command string to the Gigaset device.1950* It encapsulates the device specific method for transmission over the1951* direct USB connection to the base.1952* The command string is added to the queue of commands to send, and1953* USB transmission is started if necessary.1954* parameters:1955* cs controller state structure1956* cb command buffer structure1957* return value:1958* number of bytes queued on success1959* error code < 0 on error1960*/1961static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)1962{1963unsigned long flags;1964int rc;19651966gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?1967DEBUG_TRANSCMD : DEBUG_LOCKCMD,1968"CMD Transmit", cb->len, cb->buf);19691970/* translate "+++" escape sequence sent as a single separate command1971* into "close AT channel" command for error recovery1972* The next command will reopen the AT channel automatically.1973*/1974if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {1975/* If an HD_RECEIVEATDATA_ACK message remains unhandled1976* because of an error, the base never sends another one.1977* The response channel is thus effectively blocked.1978* Closing and reopening the AT channel does *not* clear1979* this condition.1980* As a stopgap measure, submit a zero-length AT read1981* before closing the AT channel. This has the undocumented1982* effect of triggering a new HD_RECEIVEATDATA_ACK message1983* from the base if necessary.1984* The subsequent AT channel close then discards any pending1985* messages.1986*/1987spin_lock_irqsave(&cs->lock, flags);1988if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {1989kfree(cs->hw.bas->rcvbuf);1990cs->hw.bas->rcvbuf = NULL;1991cs->hw.bas->rcvbuf_size = 0;1992cs->hw.bas->retry_cmd_in = 0;1993atread_submit(cs, 0);1994}1995spin_unlock_irqrestore(&cs->lock, flags);19961997rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);1998if (cb->wake_tasklet)1999tasklet_schedule(cb->wake_tasklet);2000if (!rc)2001rc = cb->len;2002kfree(cb);2003return rc;2004}20052006spin_lock_irqsave(&cs->cmdlock, flags);2007cb->prev = cs->lastcmdbuf;2008if (cs->lastcmdbuf)2009cs->lastcmdbuf->next = cb;2010else {2011cs->cmdbuf = cb;2012cs->curlen = cb->len;2013}2014cs->cmdbytes += cb->len;2015cs->lastcmdbuf = cb;2016spin_unlock_irqrestore(&cs->cmdlock, flags);20172018spin_lock_irqsave(&cs->lock, flags);2019if (unlikely(!cs->connected)) {2020spin_unlock_irqrestore(&cs->lock, flags);2021gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);2022/* flush command queue */2023spin_lock_irqsave(&cs->cmdlock, flags);2024while (cs->cmdbuf != NULL)2025complete_cb(cs);2026spin_unlock_irqrestore(&cs->cmdlock, flags);2027return -ENODEV;2028}2029rc = start_cbsend(cs);2030spin_unlock_irqrestore(&cs->lock, flags);2031return rc < 0 ? rc : cb->len;2032}20332034/* gigaset_write_room2035* tty_driver.write_room interface routine2036* return number of characters the driver will accept to be written via2037* gigaset_write_cmd2038* parameter:2039* controller state structure2040* return value:2041* number of characters2042*/2043static int gigaset_write_room(struct cardstate *cs)2044{2045return IF_WRITEBUF;2046}20472048/* gigaset_chars_in_buffer2049* tty_driver.chars_in_buffer interface routine2050* return number of characters waiting to be sent2051* parameter:2052* controller state structure2053* return value:2054* number of characters2055*/2056static int gigaset_chars_in_buffer(struct cardstate *cs)2057{2058return cs->cmdbytes;2059}20602061/* gigaset_brkchars2062* implementation of ioctl(GIGASET_BRKCHARS)2063* parameter:2064* controller state structure2065* return value:2066* -EINVAL (unimplemented function)2067*/2068static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])2069{2070return -EINVAL;2071}207220732074/* Device Initialization/Shutdown */2075/* ============================== */20762077/* Free hardware dependent part of the B channel structure2078* parameter:2079* bcs B channel structure2080* return value:2081* !=0 on success2082*/2083static int gigaset_freebcshw(struct bc_state *bcs)2084{2085struct bas_bc_state *ubc = bcs->hw.bas;2086int i;20872088if (!ubc)2089return 0;20902091/* kill URBs and tasklets before freeing - better safe than sorry */2092ubc->running = 0;2093gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);2094for (i = 0; i < BAS_OUTURBS; ++i) {2095usb_kill_urb(ubc->isoouturbs[i].urb);2096usb_free_urb(ubc->isoouturbs[i].urb);2097}2098for (i = 0; i < BAS_INURBS; ++i) {2099usb_kill_urb(ubc->isoinurbs[i]);2100usb_free_urb(ubc->isoinurbs[i]);2101}2102tasklet_kill(&ubc->sent_tasklet);2103tasklet_kill(&ubc->rcvd_tasklet);2104kfree(ubc->isooutbuf);2105kfree(ubc);2106bcs->hw.bas = NULL;2107return 1;2108}21092110/* Initialize hardware dependent part of the B channel structure2111* parameter:2112* bcs B channel structure2113* return value:2114* !=0 on success2115*/2116static int gigaset_initbcshw(struct bc_state *bcs)2117{2118int i;2119struct bas_bc_state *ubc;21202121bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);2122if (!ubc) {2123pr_err("out of memory\n");2124return 0;2125}21262127ubc->running = 0;2128atomic_set(&ubc->corrbytes, 0);2129spin_lock_init(&ubc->isooutlock);2130for (i = 0; i < BAS_OUTURBS; ++i) {2131ubc->isoouturbs[i].urb = NULL;2132ubc->isoouturbs[i].bcs = bcs;2133}2134ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;2135ubc->numsub = 0;2136ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);2137if (!ubc->isooutbuf) {2138pr_err("out of memory\n");2139kfree(ubc);2140bcs->hw.bas = NULL;2141return 0;2142}2143tasklet_init(&ubc->sent_tasklet,2144write_iso_tasklet, (unsigned long) bcs);21452146spin_lock_init(&ubc->isoinlock);2147for (i = 0; i < BAS_INURBS; ++i)2148ubc->isoinurbs[i] = NULL;2149ubc->isoindone = NULL;2150ubc->loststatus = -EINPROGRESS;2151ubc->isoinlost = 0;2152ubc->seqlen = 0;2153ubc->inbyte = 0;2154ubc->inbits = 0;2155ubc->goodbytes = 0;2156ubc->alignerrs = 0;2157ubc->fcserrs = 0;2158ubc->frameerrs = 0;2159ubc->giants = 0;2160ubc->runts = 0;2161ubc->aborts = 0;2162ubc->shared0s = 0;2163ubc->stolen0s = 0;2164tasklet_init(&ubc->rcvd_tasklet,2165read_iso_tasklet, (unsigned long) bcs);2166return 1;2167}21682169static void gigaset_reinitbcshw(struct bc_state *bcs)2170{2171struct bas_bc_state *ubc = bcs->hw.bas;21722173bcs->hw.bas->running = 0;2174atomic_set(&bcs->hw.bas->corrbytes, 0);2175bcs->hw.bas->numsub = 0;2176spin_lock_init(&ubc->isooutlock);2177spin_lock_init(&ubc->isoinlock);2178ubc->loststatus = -EINPROGRESS;2179}21802181static void gigaset_freecshw(struct cardstate *cs)2182{2183/* timers, URBs and rcvbuf are disposed of in disconnect */2184kfree(cs->hw.bas->int_in_buf);2185kfree(cs->hw.bas);2186cs->hw.bas = NULL;2187}21882189static int gigaset_initcshw(struct cardstate *cs)2190{2191struct bas_cardstate *ucs;21922193cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);2194if (!ucs) {2195pr_err("out of memory\n");2196return 0;2197}2198ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);2199if (!ucs->int_in_buf) {2200kfree(ucs);2201pr_err("out of memory\n");2202return 0;2203}22042205ucs->urb_cmd_in = NULL;2206ucs->urb_cmd_out = NULL;2207ucs->rcvbuf = NULL;2208ucs->rcvbuf_size = 0;22092210spin_lock_init(&ucs->lock);2211ucs->pending = 0;22122213ucs->basstate = 0;2214setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);2215setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);2216setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);2217setup_timer(&ucs->timer_int_in, int_in_resubmit, (unsigned long) cs);2218init_waitqueue_head(&ucs->waitqueue);2219INIT_WORK(&ucs->int_in_wq, int_in_work);22202221return 1;2222}22232224/* freeurbs2225* unlink and deallocate all URBs unconditionally2226* caller must make sure that no commands are still in progress2227* parameter:2228* cs controller state structure2229*/2230static void freeurbs(struct cardstate *cs)2231{2232struct bas_cardstate *ucs = cs->hw.bas;2233struct bas_bc_state *ubc;2234int i, j;22352236gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);2237for (j = 0; j < BAS_CHANNELS; ++j) {2238ubc = cs->bcs[j].hw.bas;2239for (i = 0; i < BAS_OUTURBS; ++i) {2240usb_kill_urb(ubc->isoouturbs[i].urb);2241usb_free_urb(ubc->isoouturbs[i].urb);2242ubc->isoouturbs[i].urb = NULL;2243}2244for (i = 0; i < BAS_INURBS; ++i) {2245usb_kill_urb(ubc->isoinurbs[i]);2246usb_free_urb(ubc->isoinurbs[i]);2247ubc->isoinurbs[i] = NULL;2248}2249}2250usb_kill_urb(ucs->urb_int_in);2251usb_free_urb(ucs->urb_int_in);2252ucs->urb_int_in = NULL;2253usb_kill_urb(ucs->urb_cmd_out);2254usb_free_urb(ucs->urb_cmd_out);2255ucs->urb_cmd_out = NULL;2256usb_kill_urb(ucs->urb_cmd_in);2257usb_free_urb(ucs->urb_cmd_in);2258ucs->urb_cmd_in = NULL;2259usb_kill_urb(ucs->urb_ctrl);2260usb_free_urb(ucs->urb_ctrl);2261ucs->urb_ctrl = NULL;2262}22632264/* gigaset_probe2265* This function is called when a new USB device is connected.2266* It checks whether the new device is handled by this driver.2267*/2268static int gigaset_probe(struct usb_interface *interface,2269const struct usb_device_id *id)2270{2271struct usb_host_interface *hostif;2272struct usb_device *udev = interface_to_usbdev(interface);2273struct cardstate *cs = NULL;2274struct bas_cardstate *ucs = NULL;2275struct bas_bc_state *ubc;2276struct usb_endpoint_descriptor *endpoint;2277int i, j;2278int rc;22792280gig_dbg(DEBUG_INIT,2281"%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",2282__func__, le16_to_cpu(udev->descriptor.idVendor),2283le16_to_cpu(udev->descriptor.idProduct));22842285/* set required alternate setting */2286hostif = interface->cur_altsetting;2287if (hostif->desc.bAlternateSetting != 3) {2288gig_dbg(DEBUG_INIT,2289"%s: wrong alternate setting %d - trying to switch",2290__func__, hostif->desc.bAlternateSetting);2291if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)2292< 0) {2293dev_warn(&udev->dev, "usb_set_interface failed, "2294"device %d interface %d altsetting %d\n",2295udev->devnum, hostif->desc.bInterfaceNumber,2296hostif->desc.bAlternateSetting);2297return -ENODEV;2298}2299hostif = interface->cur_altsetting;2300}23012302/* Reject application specific interfaces2303*/2304if (hostif->desc.bInterfaceClass != 255) {2305dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",2306__func__, hostif->desc.bInterfaceClass);2307return -ENODEV;2308}23092310dev_info(&udev->dev,2311"%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",2312__func__, le16_to_cpu(udev->descriptor.idVendor),2313le16_to_cpu(udev->descriptor.idProduct));23142315/* allocate memory for our device state and initialize it */2316cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,2317GIGASET_MODULENAME);2318if (!cs)2319return -ENODEV;2320ucs = cs->hw.bas;23212322/* save off device structure ptrs for later use */2323usb_get_dev(udev);2324ucs->udev = udev;2325ucs->interface = interface;2326cs->dev = &interface->dev;23272328/* allocate URBs:2329* - one for the interrupt pipe2330* - three for the different uses of the default control pipe2331* - three for each isochronous pipe2332*/2333if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||2334!(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||2335!(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||2336!(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))2337goto allocerr;23382339for (j = 0; j < BAS_CHANNELS; ++j) {2340ubc = cs->bcs[j].hw.bas;2341for (i = 0; i < BAS_OUTURBS; ++i)2342if (!(ubc->isoouturbs[i].urb =2343usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))2344goto allocerr;2345for (i = 0; i < BAS_INURBS; ++i)2346if (!(ubc->isoinurbs[i] =2347usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))2348goto allocerr;2349}23502351ucs->rcvbuf = NULL;2352ucs->rcvbuf_size = 0;23532354/* Fill the interrupt urb and send it to the core */2355endpoint = &hostif->endpoint[0].desc;2356usb_fill_int_urb(ucs->urb_int_in, udev,2357usb_rcvintpipe(udev,2358(endpoint->bEndpointAddress) & 0x0f),2359ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,2360endpoint->bInterval);2361rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);2362if (rc != 0) {2363dev_err(cs->dev, "could not submit interrupt URB: %s\n",2364get_usb_rcmsg(rc));2365goto error;2366}2367ucs->retry_int_in = 0;23682369/* tell the device that the driver is ready */2370rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);2371if (rc != 0)2372goto error;23732374/* tell common part that the device is ready */2375if (startmode == SM_LOCKED)2376cs->mstate = MS_LOCKED;23772378/* save address of controller structure */2379usb_set_intfdata(interface, cs);23802381if (!gigaset_start(cs))2382goto error;23832384return 0;23852386allocerr:2387dev_err(cs->dev, "could not allocate URBs\n");2388error:2389freeurbs(cs);2390usb_set_intfdata(interface, NULL);2391gigaset_freecs(cs);2392return -ENODEV;2393}23942395/* gigaset_disconnect2396* This function is called when the Gigaset base is unplugged.2397*/2398static void gigaset_disconnect(struct usb_interface *interface)2399{2400struct cardstate *cs;2401struct bas_cardstate *ucs;2402int j;24032404cs = usb_get_intfdata(interface);24052406ucs = cs->hw.bas;24072408dev_info(cs->dev, "disconnecting Gigaset base\n");24092410/* mark base as not ready, all channels disconnected */2411ucs->basstate = 0;24122413/* tell LL all channels are down */2414for (j = 0; j < BAS_CHANNELS; ++j)2415gigaset_bchannel_down(cs->bcs + j);24162417/* stop driver (common part) */2418gigaset_stop(cs);24192420/* stop delayed work and URBs, free ressources */2421del_timer_sync(&ucs->timer_ctrl);2422del_timer_sync(&ucs->timer_atrdy);2423del_timer_sync(&ucs->timer_cmd_in);2424del_timer_sync(&ucs->timer_int_in);2425cancel_work_sync(&ucs->int_in_wq);2426freeurbs(cs);2427usb_set_intfdata(interface, NULL);2428kfree(ucs->rcvbuf);2429ucs->rcvbuf = NULL;2430ucs->rcvbuf_size = 0;2431usb_put_dev(ucs->udev);2432ucs->interface = NULL;2433ucs->udev = NULL;2434cs->dev = NULL;2435gigaset_freecs(cs);2436}24372438/* gigaset_suspend2439* This function is called before the USB connection is suspended.2440*/2441static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)2442{2443struct cardstate *cs = usb_get_intfdata(intf);2444struct bas_cardstate *ucs = cs->hw.bas;2445int rc;24462447/* set suspend flag; this stops AT command/response traffic */2448if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {2449gig_dbg(DEBUG_SUSPEND, "already suspended");2450return 0;2451}24522453/* wait a bit for blocking conditions to go away */2454rc = wait_event_timeout(ucs->waitqueue,2455!(ucs->basstate &2456(BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),2457BAS_TIMEOUT*HZ/10);2458gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);24592460/* check for conditions preventing suspend */2461if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {2462dev_warn(cs->dev, "cannot suspend:\n");2463if (ucs->basstate & BS_B1OPEN)2464dev_warn(cs->dev, " B channel 1 open\n");2465if (ucs->basstate & BS_B2OPEN)2466dev_warn(cs->dev, " B channel 2 open\n");2467if (ucs->basstate & BS_ATRDPEND)2468dev_warn(cs->dev, " receiving AT reply\n");2469if (ucs->basstate & BS_ATWRPEND)2470dev_warn(cs->dev, " sending AT command\n");2471update_basstate(ucs, 0, BS_SUSPEND);2472return -EBUSY;2473}24742475/* close AT channel if open */2476if (ucs->basstate & BS_ATOPEN) {2477gig_dbg(DEBUG_SUSPEND, "closing AT channel");2478rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);2479if (rc) {2480update_basstate(ucs, 0, BS_SUSPEND);2481return rc;2482}2483wait_event_timeout(ucs->waitqueue, !ucs->pending,2484BAS_TIMEOUT*HZ/10);2485/* in case of timeout, proceed anyway */2486}24872488/* kill all URBs and delayed work that might still be pending */2489usb_kill_urb(ucs->urb_ctrl);2490usb_kill_urb(ucs->urb_int_in);2491del_timer_sync(&ucs->timer_ctrl);2492del_timer_sync(&ucs->timer_atrdy);2493del_timer_sync(&ucs->timer_cmd_in);2494del_timer_sync(&ucs->timer_int_in);2495cancel_work_sync(&ucs->int_in_wq);24962497gig_dbg(DEBUG_SUSPEND, "suspend complete");2498return 0;2499}25002501/* gigaset_resume2502* This function is called after the USB connection has been resumed.2503*/2504static int gigaset_resume(struct usb_interface *intf)2505{2506struct cardstate *cs = usb_get_intfdata(intf);2507struct bas_cardstate *ucs = cs->hw.bas;2508int rc;25092510/* resubmit interrupt URB for spontaneous messages from base */2511rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);2512if (rc) {2513dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",2514get_usb_rcmsg(rc));2515return rc;2516}2517ucs->retry_int_in = 0;25182519/* clear suspend flag to reallow activity */2520update_basstate(ucs, 0, BS_SUSPEND);25212522gig_dbg(DEBUG_SUSPEND, "resume complete");2523return 0;2524}25252526/* gigaset_pre_reset2527* This function is called before the USB connection is reset.2528*/2529static int gigaset_pre_reset(struct usb_interface *intf)2530{2531/* handle just like suspend */2532return gigaset_suspend(intf, PMSG_ON);2533}25342535/* gigaset_post_reset2536* This function is called after the USB connection has been reset.2537*/2538static int gigaset_post_reset(struct usb_interface *intf)2539{2540/* FIXME: send HD_DEVICE_INIT_ACK? */25412542/* resume operations */2543return gigaset_resume(intf);2544}254525462547static const struct gigaset_ops gigops = {2548gigaset_write_cmd,2549gigaset_write_room,2550gigaset_chars_in_buffer,2551gigaset_brkchars,2552gigaset_init_bchannel,2553gigaset_close_bchannel,2554gigaset_initbcshw,2555gigaset_freebcshw,2556gigaset_reinitbcshw,2557gigaset_initcshw,2558gigaset_freecshw,2559gigaset_set_modem_ctrl,2560gigaset_baud_rate,2561gigaset_set_line_ctrl,2562gigaset_isoc_send_skb,2563gigaset_isoc_input,2564};25652566/* bas_gigaset_init2567* This function is called after the kernel module is loaded.2568*/2569static int __init bas_gigaset_init(void)2570{2571int result;25722573/* allocate memory for our driver state and initialize it */2574driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,2575GIGASET_MODULENAME, GIGASET_DEVNAME,2576&gigops, THIS_MODULE);2577if (driver == NULL)2578goto error;25792580/* register this driver with the USB subsystem */2581result = usb_register(&gigaset_usb_driver);2582if (result < 0) {2583pr_err("error %d registering USB driver\n", -result);2584goto error;2585}25862587pr_info(DRIVER_DESC "\n");2588return 0;25892590error:2591if (driver)2592gigaset_freedriver(driver);2593driver = NULL;2594return -1;2595}25962597/* bas_gigaset_exit2598* This function is called before the kernel module is unloaded.2599*/2600static void __exit bas_gigaset_exit(void)2601{2602struct bas_cardstate *ucs;2603int i;26042605gigaset_blockdriver(driver); /* => probe will fail2606* => no gigaset_start any more2607*/26082609/* stop all connected devices */2610for (i = 0; i < driver->minors; i++) {2611if (gigaset_shutdown(driver->cs + i) < 0)2612continue; /* no device */2613/* from now on, no isdn callback should be possible */26142615/* close all still open channels */2616ucs = driver->cs[i].hw.bas;2617if (ucs->basstate & BS_B1OPEN) {2618gig_dbg(DEBUG_INIT, "closing B1 channel");2619usb_control_msg(ucs->udev,2620usb_sndctrlpipe(ucs->udev, 0),2621HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,26220, 0, NULL, 0, BAS_TIMEOUT);2623}2624if (ucs->basstate & BS_B2OPEN) {2625gig_dbg(DEBUG_INIT, "closing B2 channel");2626usb_control_msg(ucs->udev,2627usb_sndctrlpipe(ucs->udev, 0),2628HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,26290, 0, NULL, 0, BAS_TIMEOUT);2630}2631if (ucs->basstate & BS_ATOPEN) {2632gig_dbg(DEBUG_INIT, "closing AT channel");2633usb_control_msg(ucs->udev,2634usb_sndctrlpipe(ucs->udev, 0),2635HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,26360, 0, NULL, 0, BAS_TIMEOUT);2637}2638ucs->basstate = 0;2639}26402641/* deregister this driver with the USB subsystem */2642usb_deregister(&gigaset_usb_driver);2643/* this will call the disconnect-callback */2644/* from now on, no disconnect/probe callback should be running */26452646gigaset_freedriver(driver);2647driver = NULL;2648}264926502651module_init(bas_gigaset_init);2652module_exit(bas_gigaset_exit);26532654MODULE_AUTHOR(DRIVER_AUTHOR);2655MODULE_DESCRIPTION(DRIVER_DESC);2656MODULE_LICENSE("GPL");265726582659