Merge branches 'release', 'asus', 'sony-laptop' and 'thinkpad' into release
[pandora-kernel.git] / drivers / isdn / gigaset / bas-gigaset.c
1 /*
2  * USB driver for Gigaset 307x base via direct USB connection.
3  *
4  * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5  *                       Tilman Schmidt <tilman@imap.cc>,
6  *                       Stefan Eilers.
7  *
8  * =====================================================================
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License as
11  *      published by the Free Software Foundation; either version 2 of
12  *      the License, or (at your option) any later version.
13  * =====================================================================
14  */
15
16 #include "gigaset.h"
17
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/usb.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25
26 /* Version Information */
27 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
28 #define DRIVER_DESC "USB Driver for Gigaset 307x"
29
30
31 /* Module parameters */
32
33 static int startmode = SM_ISDN;
34 static int cidmode = 1;
35
36 module_param(startmode, int, S_IRUGO);
37 module_param(cidmode, int, S_IRUGO);
38 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39 MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41 #define GIGASET_MINORS     1
42 #define GIGASET_MINOR      16
43 #define GIGASET_MODULENAME "bas_gigaset"
44 #define GIGASET_DEVNAME    "ttyGB"
45
46 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47 #define IF_WRITEBUF 264
48
49 /* Values for the Gigaset 307x */
50 #define USB_GIGA_VENDOR_ID      0x0681
51 #define USB_3070_PRODUCT_ID     0x0001
52 #define USB_3075_PRODUCT_ID     0x0002
53 #define USB_SX303_PRODUCT_ID    0x0021
54 #define USB_SX353_PRODUCT_ID    0x0022
55
56 /* table of devices that work with this driver */
57 static const struct usb_device_id gigaset_table [] = {
58         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
59         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
60         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
61         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
62         { } /* Terminating entry */
63 };
64
65 MODULE_DEVICE_TABLE(usb, gigaset_table);
66
67 /*======================= local function prototypes ==========================*/
68
69 /* function called if a new device belonging to this driver is connected */
70 static int gigaset_probe(struct usb_interface *interface,
71                          const struct usb_device_id *id);
72
73 /* Function will be called if the device is unplugged */
74 static void gigaset_disconnect(struct usb_interface *interface);
75
76 /* functions called before/after suspend */
77 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
78 static int gigaset_resume(struct usb_interface *intf);
79
80 /* functions called before/after device reset */
81 static int gigaset_pre_reset(struct usb_interface *intf);
82 static int gigaset_post_reset(struct usb_interface *intf);
83
84 static int atread_submit(struct cardstate *, int);
85 static void stopurbs(struct bas_bc_state *);
86 static int req_submit(struct bc_state *, int, int, int);
87 static int atwrite_submit(struct cardstate *, unsigned char *, int);
88 static int start_cbsend(struct cardstate *);
89
90 /*============================================================================*/
91
92 struct bas_cardstate {
93         struct usb_device       *udev;          /* USB device pointer */
94         struct usb_interface    *interface;     /* interface for this device */
95         unsigned char           minor;          /* starting minor number */
96
97         struct urb              *urb_ctrl;      /* control pipe default URB */
98         struct usb_ctrlrequest  dr_ctrl;
99         struct timer_list       timer_ctrl;     /* control request timeout */
100         int                     retry_ctrl;
101
102         struct timer_list       timer_atrdy;    /* AT command ready timeout */
103         struct urb              *urb_cmd_out;   /* for sending AT commands */
104         struct usb_ctrlrequest  dr_cmd_out;
105         int                     retry_cmd_out;
106
107         struct urb              *urb_cmd_in;    /* for receiving AT replies */
108         struct usb_ctrlrequest  dr_cmd_in;
109         struct timer_list       timer_cmd_in;   /* receive request timeout */
110         unsigned char           *rcvbuf;        /* AT reply receive buffer */
111
112         struct urb              *urb_int_in;    /* URB for interrupt pipe */
113         unsigned char           int_in_buf[3];
114
115         spinlock_t              lock;           /* locks all following */
116         int                     basstate;       /* bitmap (BS_*) */
117         int                     pending;        /* uncompleted base request */
118         wait_queue_head_t       waitqueue;
119         int                     rcvbuf_size;    /* size of AT receive buffer */
120                                                 /* 0: no receive in progress */
121         int                     retry_cmd_in;   /* receive req retry count */
122 };
123
124 /* 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
135
136 static struct gigaset_driver *driver = NULL;
137
138 /* usb specific object needed to register this driver with the usb subsystem */
139 static struct usb_driver gigaset_usb_driver = {
140         .name =         GIGASET_MODULENAME,
141         .probe =        gigaset_probe,
142         .disconnect =   gigaset_disconnect,
143         .id_table =     gigaset_table,
144         .suspend =      gigaset_suspend,
145         .resume =       gigaset_resume,
146         .reset_resume = gigaset_post_reset,
147         .pre_reset =    gigaset_pre_reset,
148         .post_reset =   gigaset_post_reset,
149 };
150
151 /* get message text for usb_submit_urb return code
152  */
153 static char *get_usb_rcmsg(int rc)
154 {
155         static char unkmsg[28];
156
157         switch (rc) {
158         case 0:
159                 return "success";
160         case -ENOMEM:
161                 return "out of memory";
162         case -ENODEV:
163                 return "device not present";
164         case -ENOENT:
165                 return "endpoint not present";
166         case -ENXIO:
167                 return "URB type not supported";
168         case -EINVAL:
169                 return "invalid argument";
170         case -EAGAIN:
171                 return "start frame too early or too much scheduled";
172         case -EFBIG:
173                 return "too many isochronous frames requested";
174         case -EPIPE:
175                 return "endpoint stalled";
176         case -EMSGSIZE:
177                 return "invalid packet size";
178         case -ENOSPC:
179                 return "would overcommit USB bandwidth";
180         case -ESHUTDOWN:
181                 return "device shut down";
182         case -EPERM:
183                 return "reject flag set";
184         case -EHOSTUNREACH:
185                 return "device suspended";
186         default:
187                 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
188                 return unkmsg;
189         }
190 }
191
192 /* get message text for USB status code
193  */
194 static char *get_usb_statmsg(int status)
195 {
196         static char unkmsg[28];
197
198         switch (status) {
199         case 0:
200                 return "success";
201         case -ENOENT:
202                 return "unlinked (sync)";
203         case -EINPROGRESS:
204                 return "pending";
205         case -EPROTO:
206                 return "bit stuffing error, timeout, or unknown USB error";
207         case -EILSEQ:
208                 return "CRC mismatch, timeout, or unknown USB error";
209         case -ETIME:
210                 return "timed out";
211         case -EPIPE:
212                 return "endpoint stalled";
213         case -ECOMM:
214                 return "IN buffer overrun";
215         case -ENOSR:
216                 return "OUT buffer underrun";
217         case -EOVERFLOW:
218                 return "too much data";
219         case -EREMOTEIO:
220                 return "short packet detected";
221         case -ENODEV:
222                 return "device removed";
223         case -EXDEV:
224                 return "partial isochronous transfer";
225         case -EINVAL:
226                 return "invalid argument";
227         case -ECONNRESET:
228                 return "unlinked (async)";
229         case -ESHUTDOWN:
230                 return "device shut down";
231         default:
232                 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
233                 return unkmsg;
234         }
235 }
236
237 /* usb_pipetype_str
238  * retrieve string representation of USB pipe type
239  */
240 static inline char *usb_pipetype_str(int pipe)
241 {
242         if (usb_pipeisoc(pipe))
243                 return "Isoc";
244         if (usb_pipeint(pipe))
245                 return "Int";
246         if (usb_pipecontrol(pipe))
247                 return "Ctrl";
248         if (usb_pipebulk(pipe))
249                 return "Bulk";
250         return "?";
251 }
252
253 /* dump_urb
254  * write content of URB to syslog for debugging
255  */
256 static inline void dump_urb(enum debuglevel level, const char *tag,
257                             struct urb *urb)
258 {
259 #ifdef CONFIG_GIGASET_DEBUG
260         int i;
261         gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
262         if (urb) {
263                 gig_dbg(level,
264                         "  dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
265                         "hcpriv=0x%08lx, transfer_flags=0x%x,",
266                         (unsigned long) urb->dev,
267                         usb_pipetype_str(urb->pipe),
268                         usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
269                         usb_pipein(urb->pipe) ? "in" : "out",
270                         (unsigned long) urb->hcpriv,
271                         urb->transfer_flags);
272                 gig_dbg(level,
273                         "  transfer_buffer=0x%08lx[%d], actual_length=%d, "
274                         "setup_packet=0x%08lx,",
275                         (unsigned long) urb->transfer_buffer,
276                         urb->transfer_buffer_length, urb->actual_length,
277                         (unsigned long) urb->setup_packet);
278                 gig_dbg(level,
279                         "  start_frame=%d, number_of_packets=%d, interval=%d, "
280                         "error_count=%d,",
281                         urb->start_frame, urb->number_of_packets, urb->interval,
282                         urb->error_count);
283                 gig_dbg(level,
284                         "  context=0x%08lx, complete=0x%08lx, "
285                         "iso_frame_desc[]={",
286                         (unsigned long) urb->context,
287                         (unsigned long) urb->complete);
288                 for (i = 0; i < urb->number_of_packets; i++) {
289                         struct usb_iso_packet_descriptor *pifd
290                                 = &urb->iso_frame_desc[i];
291                         gig_dbg(level,
292                                 "    {offset=%u, length=%u, actual_length=%u, "
293                                 "status=%u}",
294                                 pifd->offset, pifd->length, pifd->actual_length,
295                                 pifd->status);
296                 }
297         }
298         gig_dbg(level, "}}");
299 #endif
300 }
301
302 /* read/set modem control bits etc. (m10x only) */
303 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
304                                   unsigned new_state)
305 {
306         return -EINVAL;
307 }
308
309 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
310 {
311         return -EINVAL;
312 }
313
314 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
315 {
316         return -EINVAL;
317 }
318
319 /* error_hangup
320  * hang up any existing connection because of an unrecoverable error
321  * This function may be called from any context and takes care of scheduling
322  * the necessary actions for execution outside of interrupt context.
323  * cs->lock must not be held.
324  * argument:
325  *      B channel control structure
326  */
327 static inline void error_hangup(struct bc_state *bcs)
328 {
329         struct cardstate *cs = bcs->cs;
330
331         gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
332                 __func__, bcs->channel);
333
334         if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
335                 dev_err(cs->dev, "event queue full\n");
336
337         gigaset_schedule_event(cs);
338 }
339
340 /* error_reset
341  * reset Gigaset device because of an unrecoverable error
342  * This function may be called from any context, and takes care of
343  * scheduling the necessary actions for execution outside of interrupt context.
344  * cs->lock must not be held.
345  * argument:
346  *      controller state structure
347  */
348 static inline void error_reset(struct cardstate *cs)
349 {
350         /* close AT command channel to recover (ignore errors) */
351         req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
352
353         //FIXME try to recover without bothering the user
354         dev_err(cs->dev,
355             "unrecoverable error - please disconnect Gigaset base to reset\n");
356 }
357
358 /* check_pending
359  * check for completion of pending control request
360  * parameter:
361  *      ucs     hardware specific controller state structure
362  */
363 static void check_pending(struct bas_cardstate *ucs)
364 {
365         unsigned long flags;
366
367         spin_lock_irqsave(&ucs->lock, flags);
368         switch (ucs->pending) {
369         case 0:
370                 break;
371         case HD_OPEN_ATCHANNEL:
372                 if (ucs->basstate & BS_ATOPEN)
373                         ucs->pending = 0;
374                 break;
375         case HD_OPEN_B1CHANNEL:
376                 if (ucs->basstate & BS_B1OPEN)
377                         ucs->pending = 0;
378                 break;
379         case HD_OPEN_B2CHANNEL:
380                 if (ucs->basstate & BS_B2OPEN)
381                         ucs->pending = 0;
382                 break;
383         case HD_CLOSE_ATCHANNEL:
384                 if (!(ucs->basstate & BS_ATOPEN))
385                         ucs->pending = 0;
386                 break;
387         case HD_CLOSE_B1CHANNEL:
388                 if (!(ucs->basstate & BS_B1OPEN))
389                         ucs->pending = 0;
390                 break;
391         case HD_CLOSE_B2CHANNEL:
392                 if (!(ucs->basstate & BS_B2OPEN))
393                         ucs->pending = 0;
394                 break;
395         case HD_DEVICE_INIT_ACK:                /* no reply expected */
396                 ucs->pending = 0;
397                 break;
398         /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
399          * are handled separately and should never end up here
400          */
401         default:
402                 dev_warn(&ucs->interface->dev,
403                          "unknown pending request 0x%02x cleared\n",
404                          ucs->pending);
405                 ucs->pending = 0;
406         }
407
408         if (!ucs->pending)
409                 del_timer(&ucs->timer_ctrl);
410
411         spin_unlock_irqrestore(&ucs->lock, flags);
412 }
413
414 /* cmd_in_timeout
415  * timeout routine for command input request
416  * argument:
417  *      controller state structure
418  */
419 static void cmd_in_timeout(unsigned long data)
420 {
421         struct cardstate *cs = (struct cardstate *) data;
422         struct bas_cardstate *ucs = cs->hw.bas;
423         int rc;
424
425         if (!ucs->rcvbuf_size) {
426                 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
427                 return;
428         }
429
430         if (ucs->retry_cmd_in++ < BAS_RETRY) {
431                 dev_notice(cs->dev, "control read: timeout, retry %d\n",
432                            ucs->retry_cmd_in);
433                 rc = atread_submit(cs, BAS_TIMEOUT);
434                 if (rc >= 0 || rc == -ENODEV)
435                         /* resubmitted or disconnected */
436                         /* - bypass regular exit block */
437                         return;
438         } else {
439                 dev_err(cs->dev,
440                         "control read: timeout, giving up after %d tries\n",
441                         ucs->retry_cmd_in);
442         }
443         kfree(ucs->rcvbuf);
444         ucs->rcvbuf = NULL;
445         ucs->rcvbuf_size = 0;
446         error_reset(cs);
447 }
448
449 /* set/clear bits in base connection state, return previous state
450  */
451 inline static int update_basstate(struct bas_cardstate *ucs,
452                                   int set, int clear)
453 {
454         unsigned long flags;
455         int state;
456
457         spin_lock_irqsave(&ucs->lock, flags);
458         state = ucs->basstate;
459         ucs->basstate = (state & ~clear) | set;
460         spin_unlock_irqrestore(&ucs->lock, flags);
461         return state;
462 }
463
464 /* read_ctrl_callback
465  * USB completion handler for control pipe input
466  * called by the USB subsystem in interrupt context
467  * parameter:
468  *      urb     USB request block
469  *              urb->context = inbuf structure for controller state
470  */
471 static void read_ctrl_callback(struct urb *urb)
472 {
473         struct inbuf_t *inbuf = urb->context;
474         struct cardstate *cs = inbuf->cs;
475         struct bas_cardstate *ucs = cs->hw.bas;
476         int status = urb->status;
477         int have_data = 0;
478         unsigned numbytes;
479         int rc;
480
481         update_basstate(ucs, 0, BS_ATRDPEND);
482         wake_up(&ucs->waitqueue);
483
484         if (!ucs->rcvbuf_size) {
485                 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
486                 return;
487         }
488
489         del_timer(&ucs->timer_cmd_in);
490
491         switch (status) {
492         case 0:                         /* normal completion */
493                 numbytes = urb->actual_length;
494                 if (unlikely(numbytes != ucs->rcvbuf_size)) {
495                         dev_warn(cs->dev,
496                                "control read: received %d chars, expected %d\n",
497                                  numbytes, ucs->rcvbuf_size);
498                         if (numbytes > ucs->rcvbuf_size)
499                                 numbytes = ucs->rcvbuf_size;
500                 }
501
502                 /* copy received bytes to inbuf */
503                 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
504
505                 if (unlikely(numbytes < ucs->rcvbuf_size)) {
506                         /* incomplete - resubmit for remaining bytes */
507                         ucs->rcvbuf_size -= numbytes;
508                         ucs->retry_cmd_in = 0;
509                         rc = atread_submit(cs, BAS_TIMEOUT);
510                         if (rc >= 0 || rc == -ENODEV)
511                                 /* resubmitted or disconnected */
512                                 /* - bypass regular exit block */
513                                 return;
514                         error_reset(cs);
515                 }
516                 break;
517
518         case -ENOENT:                   /* cancelled */
519         case -ECONNRESET:               /* cancelled (async) */
520         case -EINPROGRESS:              /* pending */
521         case -ENODEV:                   /* device removed */
522         case -ESHUTDOWN:                /* device shut down */
523                 /* no action necessary */
524                 gig_dbg(DEBUG_USBREQ, "%s: %s",
525                         __func__, get_usb_statmsg(status));
526                 break;
527
528         default:                        /* severe trouble */
529                 dev_warn(cs->dev, "control read: %s\n",
530                          get_usb_statmsg(status));
531                 if (ucs->retry_cmd_in++ < BAS_RETRY) {
532                         dev_notice(cs->dev, "control read: retry %d\n",
533                                    ucs->retry_cmd_in);
534                         rc = atread_submit(cs, BAS_TIMEOUT);
535                         if (rc >= 0 || rc == -ENODEV)
536                                 /* resubmitted or disconnected */
537                                 /* - bypass regular exit block */
538                                 return;
539                 } else {
540                         dev_err(cs->dev,
541                                 "control read: giving up after %d tries\n",
542                                 ucs->retry_cmd_in);
543                 }
544                 error_reset(cs);
545         }
546
547         kfree(ucs->rcvbuf);
548         ucs->rcvbuf = NULL;
549         ucs->rcvbuf_size = 0;
550         if (have_data) {
551                 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
552                 gigaset_schedule_event(cs);
553         }
554 }
555
556 /* atread_submit
557  * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
558  * parameters:
559  *      cs      controller state structure
560  *      timeout timeout in 1/10 sec., 0: none
561  * return value:
562  *      0 on success
563  *      -EBUSY if another request is pending
564  *      any URB submission error code
565  */
566 static int atread_submit(struct cardstate *cs, int timeout)
567 {
568         struct bas_cardstate *ucs = cs->hw.bas;
569         int basstate;
570         int ret;
571
572         gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
573                 ucs->rcvbuf_size);
574
575         basstate = update_basstate(ucs, BS_ATRDPEND, 0);
576         if (basstate & BS_ATRDPEND) {
577                 dev_err(cs->dev,
578                         "could not submit HD_READ_ATMESSAGE: URB busy\n");
579                 return -EBUSY;
580         }
581
582         if (basstate & BS_SUSPEND) {
583                 dev_notice(cs->dev,
584                            "HD_READ_ATMESSAGE not submitted, "
585                            "suspend in progress\n");
586                 update_basstate(ucs, 0, BS_ATRDPEND);
587                 /* treat like disconnect */
588                 return -ENODEV;
589         }
590
591         ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
592         ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
593         ucs->dr_cmd_in.wValue = 0;
594         ucs->dr_cmd_in.wIndex = 0;
595         ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
596         usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
597                              usb_rcvctrlpipe(ucs->udev, 0),
598                              (unsigned char*) & ucs->dr_cmd_in,
599                              ucs->rcvbuf, ucs->rcvbuf_size,
600                              read_ctrl_callback, cs->inbuf);
601
602         if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
603                 update_basstate(ucs, 0, BS_ATRDPEND);
604                 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
605                         get_usb_rcmsg(ret));
606                 return ret;
607         }
608
609         if (timeout > 0) {
610                 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
611                 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
612                 ucs->timer_cmd_in.data = (unsigned long) cs;
613                 ucs->timer_cmd_in.function = cmd_in_timeout;
614                 add_timer(&ucs->timer_cmd_in);
615         }
616         return 0;
617 }
618
619 /* read_int_callback
620  * USB completion handler for interrupt pipe input
621  * called by the USB subsystem in interrupt context
622  * parameter:
623  *      urb     USB request block
624  *              urb->context = controller state structure
625  */
626 static void read_int_callback(struct urb *urb)
627 {
628         struct cardstate *cs = urb->context;
629         struct bas_cardstate *ucs = cs->hw.bas;
630         struct bc_state *bcs;
631         int status = urb->status;
632         unsigned long flags;
633         int rc;
634         unsigned l;
635         int channel;
636
637         switch (status) {
638         case 0:                 /* success */
639                 break;
640         case -ENOENT:                   /* cancelled */
641         case -ECONNRESET:               /* cancelled (async) */
642         case -EINPROGRESS:              /* pending */
643                 /* ignore silently */
644                 gig_dbg(DEBUG_USBREQ, "%s: %s",
645                         __func__, get_usb_statmsg(status));
646                 return;
647         case -ENODEV:                   /* device removed */
648         case -ESHUTDOWN:                /* device shut down */
649                 //FIXME use this as disconnect indicator?
650                 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
651                 return;
652         default:                /* severe trouble */
653                 dev_warn(cs->dev, "interrupt read: %s\n",
654                          get_usb_statmsg(status));
655                 //FIXME corrective action? resubmission always ok?
656                 goto resubmit;
657         }
658
659         /* drop incomplete packets even if the missing bytes wouldn't matter */
660         if (unlikely(urb->actual_length < 3)) {
661                 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
662                          urb->actual_length);
663                 goto resubmit;
664         }
665
666         l = (unsigned) ucs->int_in_buf[1] +
667             (((unsigned) ucs->int_in_buf[2]) << 8);
668
669         gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
670                 urb->actual_length, (int)ucs->int_in_buf[0], l,
671                 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
672
673         channel = 0;
674
675         switch (ucs->int_in_buf[0]) {
676         case HD_DEVICE_INIT_OK:
677                 update_basstate(ucs, BS_INIT, 0);
678                 break;
679
680         case HD_READY_SEND_ATDATA:
681                 del_timer(&ucs->timer_atrdy);
682                 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
683                 start_cbsend(cs);
684                 break;
685
686         case HD_OPEN_B2CHANNEL_ACK:
687                 ++channel;
688         case HD_OPEN_B1CHANNEL_ACK:
689                 bcs = cs->bcs + channel;
690                 update_basstate(ucs, BS_B1OPEN << channel, 0);
691                 gigaset_bchannel_up(bcs);
692                 break;
693
694         case HD_OPEN_ATCHANNEL_ACK:
695                 update_basstate(ucs, BS_ATOPEN, 0);
696                 start_cbsend(cs);
697                 break;
698
699         case HD_CLOSE_B2CHANNEL_ACK:
700                 ++channel;
701         case HD_CLOSE_B1CHANNEL_ACK:
702                 bcs = cs->bcs + channel;
703                 update_basstate(ucs, 0, BS_B1OPEN << channel);
704                 stopurbs(bcs->hw.bas);
705                 gigaset_bchannel_down(bcs);
706                 break;
707
708         case HD_CLOSE_ATCHANNEL_ACK:
709                 update_basstate(ucs, 0, BS_ATOPEN);
710                 break;
711
712         case HD_B2_FLOW_CONTROL:
713                 ++channel;
714         case HD_B1_FLOW_CONTROL:
715                 bcs = cs->bcs + channel;
716                 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
717                            &bcs->hw.bas->corrbytes);
718                 gig_dbg(DEBUG_ISO,
719                         "Flow control (channel %d, sub %d): 0x%02x => %d",
720                         channel, bcs->hw.bas->numsub, l,
721                         atomic_read(&bcs->hw.bas->corrbytes));
722                 break;
723
724         case HD_RECEIVEATDATA_ACK:      /* AT response ready to be received */
725                 if (!l) {
726                         dev_warn(cs->dev,
727                                 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
728                         break;
729                 }
730                 spin_lock_irqsave(&cs->lock, flags);
731                 if (ucs->rcvbuf_size) {
732                         /* throw away previous buffer - we have no queue */
733                         dev_err(cs->dev,
734                                 "receive AT data overrun, %d bytes lost\n",
735                                 ucs->rcvbuf_size);
736                         kfree(ucs->rcvbuf);
737                         ucs->rcvbuf_size = 0;
738                 }
739                 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
740                         spin_unlock_irqrestore(&cs->lock, flags);
741                         dev_err(cs->dev, "out of memory receiving AT data\n");
742                         error_reset(cs);
743                         break;
744                 }
745                 ucs->rcvbuf_size = l;
746                 ucs->retry_cmd_in = 0;
747                 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
748                         kfree(ucs->rcvbuf);
749                         ucs->rcvbuf = NULL;
750                         ucs->rcvbuf_size = 0;
751                         if (rc != -ENODEV) {
752                                 //FIXME corrective action?
753                                 spin_unlock_irqrestore(&cs->lock, flags);
754                                 error_reset(cs);
755                                 break;
756                         }
757                 }
758                 spin_unlock_irqrestore(&cs->lock, flags);
759                 break;
760
761         case HD_RESET_INTERRUPT_PIPE_ACK:
762                 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
763                 break;
764
765         case HD_SUSPEND_END:
766                 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
767                 break;
768
769         default:
770                 dev_warn(cs->dev,
771                          "unknown Gigaset signal 0x%02x (%u) ignored\n",
772                          (int) ucs->int_in_buf[0], l);
773         }
774
775         check_pending(ucs);
776         wake_up(&ucs->waitqueue);
777
778 resubmit:
779         rc = usb_submit_urb(urb, GFP_ATOMIC);
780         if (unlikely(rc != 0 && rc != -ENODEV)) {
781                 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
782                         get_usb_rcmsg(rc));
783                 error_reset(cs);
784         }
785 }
786
787 /* read_iso_callback
788  * USB completion handler for B channel isochronous input
789  * called by the USB subsystem in interrupt context
790  * parameter:
791  *      urb     USB request block of completed request
792  *              urb->context = bc_state structure
793  */
794 static void read_iso_callback(struct urb *urb)
795 {
796         struct bc_state *bcs;
797         struct bas_bc_state *ubc;
798         int status = urb->status;
799         unsigned long flags;
800         int i, rc;
801
802         /* status codes not worth bothering the tasklet with */
803         if (unlikely(status == -ENOENT ||
804                      status == -ECONNRESET ||
805                      status == -EINPROGRESS ||
806                      status == -ENODEV ||
807                      status == -ESHUTDOWN)) {
808                 gig_dbg(DEBUG_ISO, "%s: %s",
809                         __func__, get_usb_statmsg(status));
810                 return;
811         }
812
813         bcs = urb->context;
814         ubc = bcs->hw.bas;
815
816         spin_lock_irqsave(&ubc->isoinlock, flags);
817         if (likely(ubc->isoindone == NULL)) {
818                 /* pass URB to tasklet */
819                 ubc->isoindone = urb;
820                 ubc->isoinstatus = status;
821                 tasklet_schedule(&ubc->rcvd_tasklet);
822         } else {
823                 /* tasklet still busy, drop data and resubmit URB */
824                 ubc->loststatus = status;
825                 for (i = 0; i < BAS_NUMFRAMES; i++) {
826                         ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
827                         if (unlikely(urb->iso_frame_desc[i].status != 0 &&
828                                      urb->iso_frame_desc[i].status !=
829                                                                 -EINPROGRESS))
830                                 ubc->loststatus = urb->iso_frame_desc[i].status;
831                         urb->iso_frame_desc[i].status = 0;
832                         urb->iso_frame_desc[i].actual_length = 0;
833                 }
834                 if (likely(ubc->running)) {
835                         /* urb->dev is clobbered by USB subsystem */
836                         urb->dev = bcs->cs->hw.bas->udev;
837                         urb->transfer_flags = URB_ISO_ASAP;
838                         urb->number_of_packets = BAS_NUMFRAMES;
839                         gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
840                                 __func__);
841                         rc = usb_submit_urb(urb, GFP_ATOMIC);
842                         if (unlikely(rc != 0 && rc != -ENODEV)) {
843                                 dev_err(bcs->cs->dev,
844                                         "could not resubmit isochronous read "
845                                         "URB: %s\n", get_usb_rcmsg(rc));
846                                 dump_urb(DEBUG_ISO, "isoc read", urb);
847                                 error_hangup(bcs);
848                         }
849                 }
850         }
851         spin_unlock_irqrestore(&ubc->isoinlock, flags);
852 }
853
854 /* write_iso_callback
855  * USB completion handler for B channel isochronous output
856  * called by the USB subsystem in interrupt context
857  * parameter:
858  *      urb     USB request block of completed request
859  *              urb->context = isow_urbctx_t structure
860  */
861 static void write_iso_callback(struct urb *urb)
862 {
863         struct isow_urbctx_t *ucx;
864         struct bas_bc_state *ubc;
865         int status = urb->status;
866         unsigned long flags;
867
868         /* status codes not worth bothering the tasklet with */
869         if (unlikely(status == -ENOENT ||
870                      status == -ECONNRESET ||
871                      status == -EINPROGRESS ||
872                      status == -ENODEV ||
873                      status == -ESHUTDOWN)) {
874                 gig_dbg(DEBUG_ISO, "%s: %s",
875                         __func__, get_usb_statmsg(status));
876                 return;
877         }
878
879         /* pass URB context to tasklet */
880         ucx = urb->context;
881         ubc = ucx->bcs->hw.bas;
882         ucx->status = status;
883
884         spin_lock_irqsave(&ubc->isooutlock, flags);
885         ubc->isooutovfl = ubc->isooutdone;
886         ubc->isooutdone = ucx;
887         spin_unlock_irqrestore(&ubc->isooutlock, flags);
888         tasklet_schedule(&ubc->sent_tasklet);
889 }
890
891 /* starturbs
892  * prepare and submit USB request blocks for isochronous input and output
893  * argument:
894  *      B channel control structure
895  * return value:
896  *      0 on success
897  *      < 0 on error (no URBs submitted)
898  */
899 static int starturbs(struct bc_state *bcs)
900 {
901         struct bas_bc_state *ubc = bcs->hw.bas;
902         struct urb *urb;
903         int j, k;
904         int rc;
905
906         /* initialize L2 reception */
907         if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
908                 bcs->inputstate |= INS_flag_hunt;
909
910         /* submit all isochronous input URBs */
911         ubc->running = 1;
912         for (k = 0; k < BAS_INURBS; k++) {
913                 urb = ubc->isoinurbs[k];
914                 if (!urb) {
915                         rc = -EFAULT;
916                         goto error;
917                 }
918
919                 urb->dev = bcs->cs->hw.bas->udev;
920                 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
921                 urb->transfer_flags = URB_ISO_ASAP;
922                 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
923                 urb->transfer_buffer_length = BAS_INBUFSIZE;
924                 urb->number_of_packets = BAS_NUMFRAMES;
925                 urb->interval = BAS_FRAMETIME;
926                 urb->complete = read_iso_callback;
927                 urb->context = bcs;
928                 for (j = 0; j < BAS_NUMFRAMES; j++) {
929                         urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
930                         urb->iso_frame_desc[j].length = BAS_MAXFRAME;
931                         urb->iso_frame_desc[j].status = 0;
932                         urb->iso_frame_desc[j].actual_length = 0;
933                 }
934
935                 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
936                 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
937                         goto error;
938         }
939
940         /* initialize L2 transmission */
941         gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
942
943         /* set up isochronous output URBs for flag idling */
944         for (k = 0; k < BAS_OUTURBS; ++k) {
945                 urb = ubc->isoouturbs[k].urb;
946                 if (!urb) {
947                         rc = -EFAULT;
948                         goto error;
949                 }
950                 urb->dev = bcs->cs->hw.bas->udev;
951                 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
952                 urb->transfer_flags = URB_ISO_ASAP;
953                 urb->transfer_buffer = ubc->isooutbuf->data;
954                 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
955                 urb->number_of_packets = BAS_NUMFRAMES;
956                 urb->interval = BAS_FRAMETIME;
957                 urb->complete = write_iso_callback;
958                 urb->context = &ubc->isoouturbs[k];
959                 for (j = 0; j < BAS_NUMFRAMES; ++j) {
960                         urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
961                         urb->iso_frame_desc[j].length = BAS_NORMFRAME;
962                         urb->iso_frame_desc[j].status = 0;
963                         urb->iso_frame_desc[j].actual_length = 0;
964                 }
965                 ubc->isoouturbs[k].limit = -1;
966         }
967
968         /* keep one URB free, submit the others */
969         for (k = 0; k < BAS_OUTURBS-1; ++k) {
970                 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
971                 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
972                 if (rc != 0)
973                         goto error;
974         }
975         dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
976         ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
977         ubc->isooutdone = ubc->isooutovfl = NULL;
978         return 0;
979  error:
980         stopurbs(ubc);
981         return rc;
982 }
983
984 /* stopurbs
985  * cancel the USB request blocks for isochronous input and output
986  * errors are silently ignored
987  * argument:
988  *      B channel control structure
989  */
990 static void stopurbs(struct bas_bc_state *ubc)
991 {
992         int k, rc;
993
994         ubc->running = 0;
995
996         for (k = 0; k < BAS_INURBS; ++k) {
997                 rc = usb_unlink_urb(ubc->isoinurbs[k]);
998                 gig_dbg(DEBUG_ISO,
999                         "%s: isoc input URB %d unlinked, result = %s",
1000                         __func__, k, get_usb_rcmsg(rc));
1001         }
1002
1003         for (k = 0; k < BAS_OUTURBS; ++k) {
1004                 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1005                 gig_dbg(DEBUG_ISO,
1006                         "%s: isoc output URB %d unlinked, result = %s",
1007                         __func__, k, get_usb_rcmsg(rc));
1008         }
1009 }
1010
1011 /* Isochronous Write - Bottom Half */
1012 /* =============================== */
1013
1014 /* submit_iso_write_urb
1015  * fill and submit the next isochronous write URB
1016  * parameters:
1017  *      ucx     context structure containing URB
1018  * return value:
1019  *      number of frames submitted in URB
1020  *      0 if URB not submitted because no data available (isooutbuf busy)
1021  *      error code < 0 on error
1022  */
1023 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1024 {
1025         struct urb *urb = ucx->urb;
1026         struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1027         struct usb_iso_packet_descriptor *ifd;
1028         int corrbytes, nframe, rc;
1029
1030         /* urb->dev is clobbered by USB subsystem */
1031         urb->dev = ucx->bcs->cs->hw.bas->udev;
1032         urb->transfer_flags = URB_ISO_ASAP;
1033         urb->transfer_buffer = ubc->isooutbuf->data;
1034         urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1035
1036         for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1037                 ifd = &urb->iso_frame_desc[nframe];
1038
1039                 /* compute frame length according to flow control */
1040                 ifd->length = BAS_NORMFRAME;
1041                 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
1042                         gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1043                                 __func__, corrbytes);
1044                         if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1045                                 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1046                         else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1047                                 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1048                         ifd->length += corrbytes;
1049                         atomic_add(-corrbytes, &ubc->corrbytes);
1050                 }
1051
1052                 /* retrieve block of data to send */
1053                 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1054                                                        ifd->length);
1055                 if (ifd->offset < 0) {
1056                         if (ifd->offset == -EBUSY) {
1057                                 gig_dbg(DEBUG_ISO,
1058                                         "%s: buffer busy at frame %d",
1059                                         __func__, nframe);
1060                                 /* tasklet will be restarted from
1061                                    gigaset_send_skb() */
1062                         } else {
1063                                 dev_err(ucx->bcs->cs->dev,
1064                                         "%s: buffer error %d at frame %d\n",
1065                                         __func__, ifd->offset, nframe);
1066                                 return ifd->offset;
1067                         }
1068                         break;
1069                 }
1070                 ucx->limit = ubc->isooutbuf->nextread;
1071                 ifd->status = 0;
1072                 ifd->actual_length = 0;
1073         }
1074         if (unlikely(nframe == 0))
1075                 return 0;       /* no data to send */
1076         urb->number_of_packets = nframe;
1077
1078         rc = usb_submit_urb(urb, GFP_ATOMIC);
1079         if (unlikely(rc)) {
1080                 if (rc == -ENODEV)
1081                         /* device removed - give up silently */
1082                         gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1083                 else
1084                         dev_err(ucx->bcs->cs->dev,
1085                                 "could not submit isochronous write URB: %s\n",
1086                                 get_usb_rcmsg(rc));
1087                 return rc;
1088         }
1089         ++ubc->numsub;
1090         return nframe;
1091 }
1092
1093 /* write_iso_tasklet
1094  * tasklet scheduled when an isochronous output URB from the Gigaset device
1095  * has completed
1096  * parameter:
1097  *      data    B channel state structure
1098  */
1099 static void write_iso_tasklet(unsigned long data)
1100 {
1101         struct bc_state *bcs = (struct bc_state *) data;
1102         struct bas_bc_state *ubc = bcs->hw.bas;
1103         struct cardstate *cs = bcs->cs;
1104         struct isow_urbctx_t *done, *next, *ovfl;
1105         struct urb *urb;
1106         int status;
1107         struct usb_iso_packet_descriptor *ifd;
1108         int offset;
1109         unsigned long flags;
1110         int i;
1111         struct sk_buff *skb;
1112         int len;
1113         int rc;
1114
1115         /* loop while completed URBs arrive in time */
1116         for (;;) {
1117                 if (unlikely(!(ubc->running))) {
1118                         gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1119                         return;
1120                 }
1121
1122                 /* retrieve completed URBs */
1123                 spin_lock_irqsave(&ubc->isooutlock, flags);
1124                 done = ubc->isooutdone;
1125                 ubc->isooutdone = NULL;
1126                 ovfl = ubc->isooutovfl;
1127                 ubc->isooutovfl = NULL;
1128                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1129                 if (ovfl) {
1130                         dev_err(cs->dev, "isochronous write buffer underrun\n");
1131                         error_hangup(bcs);
1132                         break;
1133                 }
1134                 if (!done)
1135                         break;
1136
1137                 /* submit free URB if available */
1138                 spin_lock_irqsave(&ubc->isooutlock, flags);
1139                 next = ubc->isooutfree;
1140                 ubc->isooutfree = NULL;
1141                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1142                 if (next) {
1143                         rc = submit_iso_write_urb(next);
1144                         if (unlikely(rc <= 0 && rc != -ENODEV)) {
1145                                 /* could not submit URB, put it back */
1146                                 spin_lock_irqsave(&ubc->isooutlock, flags);
1147                                 if (ubc->isooutfree == NULL) {
1148                                         ubc->isooutfree = next;
1149                                         next = NULL;
1150                                 }
1151                                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1152                                 if (next) {
1153                                         /* couldn't put it back */
1154                                         dev_err(cs->dev,
1155                                               "losing isochronous write URB\n");
1156                                         error_hangup(bcs);
1157                                 }
1158                         }
1159                 }
1160
1161                 /* process completed URB */
1162                 urb = done->urb;
1163                 status = done->status;
1164                 switch (status) {
1165                 case -EXDEV:                    /* partial completion */
1166                         gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1167                                 __func__);
1168                         /* fall through - what's the difference anyway? */
1169                 case 0:                         /* normal completion */
1170                         /* inspect individual frames
1171                          * assumptions (for lack of documentation):
1172                          * - actual_length bytes of first frame in error are
1173                          *   successfully sent
1174                          * - all following frames are not sent at all
1175                          */
1176                         offset = done->limit;   /* default (no error) */
1177                         for (i = 0; i < BAS_NUMFRAMES; i++) {
1178                                 ifd = &urb->iso_frame_desc[i];
1179                                 if (ifd->status ||
1180                                     ifd->actual_length != ifd->length) {
1181                                         dev_warn(cs->dev,
1182                                              "isochronous write: frame %d: %s, "
1183                                              "only %d of %d bytes sent\n",
1184                                              i, get_usb_statmsg(ifd->status),
1185                                              ifd->actual_length, ifd->length);
1186                                         offset = (ifd->offset +
1187                                                   ifd->actual_length)
1188                                                  % BAS_OUTBUFSIZE;
1189                                         break;
1190                                 }
1191                         }
1192 #ifdef CONFIG_GIGASET_DEBUG
1193                         /* check assumption on remaining frames */
1194                         for (; i < BAS_NUMFRAMES; i++) {
1195                                 ifd = &urb->iso_frame_desc[i];
1196                                 if (ifd->status != -EINPROGRESS
1197                                     || ifd->actual_length != 0) {
1198                                         dev_warn(cs->dev,
1199                                              "isochronous write: frame %d: %s, "
1200                                              "%d of %d bytes sent\n",
1201                                              i, get_usb_statmsg(ifd->status),
1202                                              ifd->actual_length, ifd->length);
1203                                         offset = (ifd->offset +
1204                                                   ifd->actual_length)
1205                                                  % BAS_OUTBUFSIZE;
1206                                         break;
1207                                 }
1208                         }
1209 #endif
1210                         break;
1211                 case -EPIPE:                    /* stall - probably underrun */
1212                         dev_err(cs->dev, "isochronous write stalled\n");
1213                         error_hangup(bcs);
1214                         break;
1215                 default:                        /* severe trouble */
1216                         dev_warn(cs->dev, "isochronous write: %s\n",
1217                                  get_usb_statmsg(status));
1218                 }
1219
1220                 /* mark the write buffer area covered by this URB as free */
1221                 if (done->limit >= 0)
1222                         ubc->isooutbuf->read = done->limit;
1223
1224                 /* mark URB as free */
1225                 spin_lock_irqsave(&ubc->isooutlock, flags);
1226                 next = ubc->isooutfree;
1227                 ubc->isooutfree = done;
1228                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1229                 if (next) {
1230                         /* only one URB still active - resubmit one */
1231                         rc = submit_iso_write_urb(next);
1232                         if (unlikely(rc <= 0 && rc != -ENODEV)) {
1233                                 /* couldn't submit */
1234                                 error_hangup(bcs);
1235                         }
1236                 }
1237         }
1238
1239         /* process queued SKBs */
1240         while ((skb = skb_dequeue(&bcs->squeue))) {
1241                 /* copy to output buffer, doing L2 encapsulation */
1242                 len = skb->len;
1243                 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1244                         /* insufficient buffer space, push back onto queue */
1245                         skb_queue_head(&bcs->squeue, skb);
1246                         gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1247                                 __func__, skb_queue_len(&bcs->squeue));
1248                         break;
1249                 }
1250                 skb_pull(skb, len);
1251                 gigaset_skb_sent(bcs, skb);
1252                 dev_kfree_skb_any(skb);
1253         }
1254 }
1255
1256 /* Isochronous Read - Bottom Half */
1257 /* ============================== */
1258
1259 /* read_iso_tasklet
1260  * tasklet scheduled when an isochronous input URB from the Gigaset device
1261  * has completed
1262  * parameter:
1263  *      data    B channel state structure
1264  */
1265 static void read_iso_tasklet(unsigned long data)
1266 {
1267         struct bc_state *bcs = (struct bc_state *) data;
1268         struct bas_bc_state *ubc = bcs->hw.bas;
1269         struct cardstate *cs = bcs->cs;
1270         struct urb *urb;
1271         int status;
1272         char *rcvbuf;
1273         unsigned long flags;
1274         int totleft, numbytes, offset, frame, rc;
1275
1276         /* loop while more completed URBs arrive in the meantime */
1277         for (;;) {
1278                 /* retrieve URB */
1279                 spin_lock_irqsave(&ubc->isoinlock, flags);
1280                 if (!(urb = ubc->isoindone)) {
1281                         spin_unlock_irqrestore(&ubc->isoinlock, flags);
1282                         return;
1283                 }
1284                 status = ubc->isoinstatus;
1285                 ubc->isoindone = NULL;
1286                 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1287                         dev_warn(cs->dev,
1288                                  "isochronous read overrun, "
1289                                  "dropped URB with status: %s, %d bytes lost\n",
1290                                  get_usb_statmsg(ubc->loststatus),
1291                                  ubc->isoinlost);
1292                         ubc->loststatus = -EINPROGRESS;
1293                 }
1294                 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1295
1296                 if (unlikely(!(ubc->running))) {
1297                         gig_dbg(DEBUG_ISO,
1298                                 "%s: channel not running, "
1299                                 "dropped URB with status: %s",
1300                                 __func__, get_usb_statmsg(status));
1301                         return;
1302                 }
1303
1304                 switch (status) {
1305                 case 0:                         /* normal completion */
1306                         break;
1307                 case -EXDEV:                    /* inspect individual frames
1308                                                    (we do that anyway) */
1309                         gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1310                                 __func__);
1311                         break;
1312                 case -ENOENT:
1313                 case -ECONNRESET:
1314                 case -EINPROGRESS:
1315                         gig_dbg(DEBUG_ISO, "%s: %s",
1316                                 __func__, get_usb_statmsg(status));
1317                         continue;               /* -> skip */
1318                 case -EPIPE:
1319                         dev_err(cs->dev, "isochronous read stalled\n");
1320                         error_hangup(bcs);
1321                         continue;               /* -> skip */
1322                 default:                        /* severe trouble */
1323                         dev_warn(cs->dev, "isochronous read: %s\n",
1324                                  get_usb_statmsg(status));
1325                         goto error;
1326                 }
1327
1328                 rcvbuf = urb->transfer_buffer;
1329                 totleft = urb->actual_length;
1330                 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1331                         if (unlikely(urb->iso_frame_desc[frame].status)) {
1332                                 dev_warn(cs->dev,
1333                                          "isochronous read: frame %d: %s\n",
1334                                          frame,
1335                                          get_usb_statmsg(
1336                                             urb->iso_frame_desc[frame].status));
1337                                 break;
1338                         }
1339                         numbytes = urb->iso_frame_desc[frame].actual_length;
1340                         if (unlikely(numbytes > BAS_MAXFRAME)) {
1341                                 dev_warn(cs->dev,
1342                                          "isochronous read: frame %d: "
1343                                          "numbytes (%d) > BAS_MAXFRAME\n",
1344                                          frame, numbytes);
1345                                 break;
1346                         }
1347                         if (unlikely(numbytes > totleft)) {
1348                                 dev_warn(cs->dev,
1349                                          "isochronous read: frame %d: "
1350                                          "numbytes (%d) > totleft (%d)\n",
1351                                          frame, numbytes, totleft);
1352                                 break;
1353                         }
1354                         offset = urb->iso_frame_desc[frame].offset;
1355                         if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1356                                 dev_warn(cs->dev,
1357                                          "isochronous read: frame %d: "
1358                                          "offset (%d) + numbytes (%d) "
1359                                          "> BAS_INBUFSIZE\n",
1360                                          frame, offset, numbytes);
1361                                 break;
1362                         }
1363                         gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1364                         totleft -= numbytes;
1365                 }
1366                 if (unlikely(totleft > 0))
1367                         dev_warn(cs->dev,
1368                                  "isochronous read: %d data bytes missing\n",
1369                                  totleft);
1370
1371         error:
1372                 /* URB processed, resubmit */
1373                 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1374                         urb->iso_frame_desc[frame].status = 0;
1375                         urb->iso_frame_desc[frame].actual_length = 0;
1376                 }
1377                 /* urb->dev is clobbered by USB subsystem */
1378                 urb->dev = bcs->cs->hw.bas->udev;
1379                 urb->transfer_flags = URB_ISO_ASAP;
1380                 urb->number_of_packets = BAS_NUMFRAMES;
1381                 rc = usb_submit_urb(urb, GFP_ATOMIC);
1382                 if (unlikely(rc != 0 && rc != -ENODEV)) {
1383                         dev_err(cs->dev,
1384                                 "could not resubmit isochronous read URB: %s\n",
1385                                 get_usb_rcmsg(rc));
1386                         dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1387                         error_hangup(bcs);
1388                 }
1389         }
1390 }
1391
1392 /* Channel Operations */
1393 /* ================== */
1394
1395 /* req_timeout
1396  * timeout routine for control output request
1397  * argument:
1398  *      B channel control structure
1399  */
1400 static void req_timeout(unsigned long data)
1401 {
1402         struct bc_state *bcs = (struct bc_state *) data;
1403         struct bas_cardstate *ucs = bcs->cs->hw.bas;
1404         int pending;
1405         unsigned long flags;
1406
1407         check_pending(ucs);
1408
1409         spin_lock_irqsave(&ucs->lock, flags);
1410         pending = ucs->pending;
1411         ucs->pending = 0;
1412         spin_unlock_irqrestore(&ucs->lock, flags);
1413
1414         switch (pending) {
1415         case 0:                                 /* no pending request */
1416                 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1417                 break;
1418
1419         case HD_OPEN_ATCHANNEL:
1420                 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1421                 error_reset(bcs->cs);
1422                 break;
1423
1424         case HD_OPEN_B2CHANNEL:
1425         case HD_OPEN_B1CHANNEL:
1426                 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1427                         bcs->channel + 1);
1428                 error_hangup(bcs);
1429                 break;
1430
1431         case HD_CLOSE_ATCHANNEL:
1432                 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1433                 break;
1434
1435         case HD_CLOSE_B2CHANNEL:
1436         case HD_CLOSE_B1CHANNEL:
1437                 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1438                         bcs->channel + 1);
1439                 error_reset(bcs->cs);
1440                 break;
1441
1442         default:
1443                 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1444                          pending);
1445         }
1446
1447         wake_up(&ucs->waitqueue);
1448 }
1449
1450 /* write_ctrl_callback
1451  * USB completion handler for control pipe output
1452  * called by the USB subsystem in interrupt context
1453  * parameter:
1454  *      urb     USB request block of completed request
1455  *              urb->context = hardware specific controller state structure
1456  */
1457 static void write_ctrl_callback(struct urb *urb)
1458 {
1459         struct bas_cardstate *ucs = urb->context;
1460         int status = urb->status;
1461         int rc;
1462         unsigned long flags;
1463
1464         /* check status */
1465         switch (status) {
1466         case 0:                                 /* normal completion */
1467                 spin_lock_irqsave(&ucs->lock, flags);
1468                 switch (ucs->pending) {
1469                 case HD_DEVICE_INIT_ACK:        /* no reply expected */
1470                         del_timer(&ucs->timer_ctrl);
1471                         ucs->pending = 0;
1472                         break;
1473                 }
1474                 spin_unlock_irqrestore(&ucs->lock, flags);
1475                 return;
1476
1477         case -ENOENT:                   /* cancelled */
1478         case -ECONNRESET:               /* cancelled (async) */
1479         case -EINPROGRESS:              /* pending */
1480         case -ENODEV:                   /* device removed */
1481         case -ESHUTDOWN:                /* device shut down */
1482                 /* ignore silently */
1483                 gig_dbg(DEBUG_USBREQ, "%s: %s",
1484                         __func__, get_usb_statmsg(status));
1485                 break;
1486
1487         default:                                /* any failure */
1488                 /* don't retry if suspend requested */
1489                 if (++ucs->retry_ctrl > BAS_RETRY ||
1490                     (ucs->basstate & BS_SUSPEND)) {
1491                         dev_err(&ucs->interface->dev,
1492                                 "control request 0x%02x failed: %s\n",
1493                                 ucs->dr_ctrl.bRequest,
1494                                 get_usb_statmsg(status));
1495                         break;          /* give up */
1496                 }
1497                 dev_notice(&ucs->interface->dev,
1498                            "control request 0x%02x: %s, retry %d\n",
1499                            ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1500                            ucs->retry_ctrl);
1501                 /* urb->dev is clobbered by USB subsystem */
1502                 urb->dev = ucs->udev;
1503                 rc = usb_submit_urb(urb, GFP_ATOMIC);
1504                 if (unlikely(rc)) {
1505                         dev_err(&ucs->interface->dev,
1506                                 "could not resubmit request 0x%02x: %s\n",
1507                                 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1508                         break;
1509                 }
1510                 /* resubmitted */
1511                 return;
1512         }
1513
1514         /* failed, clear pending request */
1515         spin_lock_irqsave(&ucs->lock, flags);
1516         del_timer(&ucs->timer_ctrl);
1517         ucs->pending = 0;
1518         spin_unlock_irqrestore(&ucs->lock, flags);
1519         wake_up(&ucs->waitqueue);
1520 }
1521
1522 /* req_submit
1523  * submit a control output request without message buffer to the Gigaset base
1524  * and optionally start a timeout
1525  * parameters:
1526  *      bcs     B channel control structure
1527  *      req     control request code (HD_*)
1528  *      val     control request parameter value (set to 0 if unused)
1529  *      timeout timeout in seconds (0: no timeout)
1530  * return value:
1531  *      0 on success
1532  *      -EBUSY if another request is pending
1533  *      any URB submission error code
1534  */
1535 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1536 {
1537         struct bas_cardstate *ucs = bcs->cs->hw.bas;
1538         int ret;
1539         unsigned long flags;
1540
1541         gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1542
1543         spin_lock_irqsave(&ucs->lock, flags);
1544         if (ucs->pending) {
1545                 spin_unlock_irqrestore(&ucs->lock, flags);
1546                 dev_err(bcs->cs->dev,
1547                         "submission of request 0x%02x failed: "
1548                         "request 0x%02x still pending\n",
1549                         req, ucs->pending);
1550                 return -EBUSY;
1551         }
1552
1553         ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1554         ucs->dr_ctrl.bRequest = req;
1555         ucs->dr_ctrl.wValue = cpu_to_le16(val);
1556         ucs->dr_ctrl.wIndex = 0;
1557         ucs->dr_ctrl.wLength = 0;
1558         usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1559                              usb_sndctrlpipe(ucs->udev, 0),
1560                              (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1561                              write_ctrl_callback, ucs);
1562         ucs->retry_ctrl = 0;
1563         ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1564         if (unlikely(ret)) {
1565                 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1566                         req, get_usb_rcmsg(ret));
1567                 spin_unlock_irqrestore(&ucs->lock, flags);
1568                 return ret;
1569         }
1570         ucs->pending = req;
1571
1572         if (timeout > 0) {
1573                 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1574                 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1575                 ucs->timer_ctrl.data = (unsigned long) bcs;
1576                 ucs->timer_ctrl.function = req_timeout;
1577                 add_timer(&ucs->timer_ctrl);
1578         }
1579
1580         spin_unlock_irqrestore(&ucs->lock, flags);
1581         return 0;
1582 }
1583
1584 /* gigaset_init_bchannel
1585  * called by common.c to connect a B channel
1586  * initialize isochronous I/O and tell the Gigaset base to open the channel
1587  * argument:
1588  *      B channel control structure
1589  * return value:
1590  *      0 on success, error code < 0 on error
1591  */
1592 static int gigaset_init_bchannel(struct bc_state *bcs)
1593 {
1594         struct cardstate *cs = bcs->cs;
1595         int req, ret;
1596         unsigned long flags;
1597
1598         spin_lock_irqsave(&cs->lock, flags);
1599         if (unlikely(!cs->connected)) {
1600                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1601                 spin_unlock_irqrestore(&cs->lock, flags);
1602                 return -ENODEV;
1603         }
1604
1605         if (cs->hw.bas->basstate & BS_SUSPEND) {
1606                 dev_notice(cs->dev,
1607                            "not starting isochronous I/O, "
1608                            "suspend in progress\n");
1609                 spin_unlock_irqrestore(&cs->lock, flags);
1610                 return -EHOSTUNREACH;
1611         }
1612
1613         if ((ret = starturbs(bcs)) < 0) {
1614                 dev_err(cs->dev,
1615                         "could not start isochronous I/O for channel B%d: %s\n",
1616                         bcs->channel + 1,
1617                         ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1618                 if (ret != -ENODEV)
1619                         error_hangup(bcs);
1620                 spin_unlock_irqrestore(&cs->lock, flags);
1621                 return ret;
1622         }
1623
1624         req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1625         if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1626                 dev_err(cs->dev, "could not open channel B%d\n",
1627                         bcs->channel + 1);
1628                 stopurbs(bcs->hw.bas);
1629                 if (ret != -ENODEV)
1630                         error_hangup(bcs);
1631         }
1632
1633         spin_unlock_irqrestore(&cs->lock, flags);
1634         return ret;
1635 }
1636
1637 /* gigaset_close_bchannel
1638  * called by common.c to disconnect a B channel
1639  * tell the Gigaset base to close the channel
1640  * stopping isochronous I/O and LL notification will be done when the
1641  * acknowledgement for the close arrives
1642  * argument:
1643  *      B channel control structure
1644  * return value:
1645  *      0 on success, error code < 0 on error
1646  */
1647 static int gigaset_close_bchannel(struct bc_state *bcs)
1648 {
1649         struct cardstate *cs = bcs->cs;
1650         int req, ret;
1651         unsigned long flags;
1652
1653         spin_lock_irqsave(&cs->lock, flags);
1654         if (unlikely(!cs->connected)) {
1655                 spin_unlock_irqrestore(&cs->lock, flags);
1656                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1657                 return -ENODEV;
1658         }
1659
1660         if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1661                 /* channel not running: just signal common.c */
1662                 spin_unlock_irqrestore(&cs->lock, flags);
1663                 gigaset_bchannel_down(bcs);
1664                 return 0;
1665         }
1666
1667         /* channel running: tell device to close it */
1668         req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1669         if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1670                 dev_err(cs->dev, "closing channel B%d failed\n",
1671                         bcs->channel + 1);
1672
1673         spin_unlock_irqrestore(&cs->lock, flags);
1674         return ret;
1675 }
1676
1677 /* Device Operations */
1678 /* ================= */
1679
1680 /* complete_cb
1681  * unqueue first command buffer from queue, waking any sleepers
1682  * must be called with cs->cmdlock held
1683  * parameter:
1684  *      cs      controller state structure
1685  */
1686 static void complete_cb(struct cardstate *cs)
1687 {
1688         struct cmdbuf_t *cb = cs->cmdbuf;
1689
1690         /* unqueue completed buffer */
1691         cs->cmdbytes -= cs->curlen;
1692         gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1693                 "write_command: sent %u bytes, %u left",
1694                 cs->curlen, cs->cmdbytes);
1695         if ((cs->cmdbuf = cb->next) != NULL) {
1696                 cs->cmdbuf->prev = NULL;
1697                 cs->curlen = cs->cmdbuf->len;
1698         } else {
1699                 cs->lastcmdbuf = NULL;
1700                 cs->curlen = 0;
1701         }
1702
1703         if (cb->wake_tasklet)
1704                 tasklet_schedule(cb->wake_tasklet);
1705
1706         kfree(cb);
1707 }
1708
1709 /* write_command_callback
1710  * USB completion handler for AT command transmission
1711  * called by the USB subsystem in interrupt context
1712  * parameter:
1713  *      urb     USB request block of completed request
1714  *              urb->context = controller state structure
1715  */
1716 static void write_command_callback(struct urb *urb)
1717 {
1718         struct cardstate *cs = urb->context;
1719         struct bas_cardstate *ucs = cs->hw.bas;
1720         int status = urb->status;
1721         unsigned long flags;
1722
1723         update_basstate(ucs, 0, BS_ATWRPEND);
1724         wake_up(&ucs->waitqueue);
1725
1726         /* check status */
1727         switch (status) {
1728         case 0:                                 /* normal completion */
1729                 break;
1730         case -ENOENT:                   /* cancelled */
1731         case -ECONNRESET:               /* cancelled (async) */
1732         case -EINPROGRESS:              /* pending */
1733         case -ENODEV:                   /* device removed */
1734         case -ESHUTDOWN:                /* device shut down */
1735                 /* ignore silently */
1736                 gig_dbg(DEBUG_USBREQ, "%s: %s",
1737                         __func__, get_usb_statmsg(status));
1738                 return;
1739         default:                                /* any failure */
1740                 if (++ucs->retry_cmd_out > BAS_RETRY) {
1741                         dev_warn(cs->dev,
1742                                  "command write: %s, "
1743                                  "giving up after %d retries\n",
1744                                  get_usb_statmsg(status),
1745                                  ucs->retry_cmd_out);
1746                         break;
1747                 }
1748                 if (ucs->basstate & BS_SUSPEND) {
1749                         dev_warn(cs->dev,
1750                                  "command write: %s, "
1751                                  "won't retry - suspend requested\n",
1752                                  get_usb_statmsg(status));
1753                         break;
1754                 }
1755                 if (cs->cmdbuf == NULL) {
1756                         dev_warn(cs->dev,
1757                                  "command write: %s, "
1758                                  "cannot retry - cmdbuf gone\n",
1759                                  get_usb_statmsg(status));
1760                         break;
1761                 }
1762                 dev_notice(cs->dev, "command write: %s, retry %d\n",
1763                            get_usb_statmsg(status), ucs->retry_cmd_out);
1764                 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1765                         /* resubmitted - bypass regular exit block */
1766                         return;
1767                 /* command send failed, assume base still waiting */
1768                 update_basstate(ucs, BS_ATREADY, 0);
1769         }
1770
1771         spin_lock_irqsave(&cs->cmdlock, flags);
1772         if (cs->cmdbuf != NULL)
1773                 complete_cb(cs);
1774         spin_unlock_irqrestore(&cs->cmdlock, flags);
1775 }
1776
1777 /* atrdy_timeout
1778  * timeout routine for AT command transmission
1779  * argument:
1780  *      controller state structure
1781  */
1782 static void atrdy_timeout(unsigned long data)
1783 {
1784         struct cardstate *cs = (struct cardstate *) data;
1785         struct bas_cardstate *ucs = cs->hw.bas;
1786
1787         dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1788
1789         /* fake the missing signal - what else can I do? */
1790         update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1791         start_cbsend(cs);
1792 }
1793
1794 /* atwrite_submit
1795  * submit an HD_WRITE_ATMESSAGE command URB
1796  * parameters:
1797  *      cs      controller state structure
1798  *      buf     buffer containing command to send
1799  *      len     length of command to send
1800  * return value:
1801  *      0 on success
1802  *      -EBUSY if another request is pending
1803  *      any URB submission error code
1804  */
1805 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1806 {
1807         struct bas_cardstate *ucs = cs->hw.bas;
1808         int rc;
1809
1810         gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1811
1812         if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1813                 dev_err(cs->dev,
1814                         "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1815                 return -EBUSY;
1816         }
1817
1818         ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1819         ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1820         ucs->dr_cmd_out.wValue = 0;
1821         ucs->dr_cmd_out.wIndex = 0;
1822         ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1823         usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1824                              usb_sndctrlpipe(ucs->udev, 0),
1825                              (unsigned char*) &ucs->dr_cmd_out, buf, len,
1826                              write_command_callback, cs);
1827         rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1828         if (unlikely(rc)) {
1829                 update_basstate(ucs, 0, BS_ATWRPEND);
1830                 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1831                         get_usb_rcmsg(rc));
1832                 return rc;
1833         }
1834
1835         /* submitted successfully, start timeout if necessary */
1836         if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1837                 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1838                         ATRDY_TIMEOUT);
1839                 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1840                 ucs->timer_atrdy.data = (unsigned long) cs;
1841                 ucs->timer_atrdy.function = atrdy_timeout;
1842                 add_timer(&ucs->timer_atrdy);
1843         }
1844         return 0;
1845 }
1846
1847 /* start_cbsend
1848  * start transmission of AT command queue if necessary
1849  * parameter:
1850  *      cs              controller state structure
1851  * return value:
1852  *      0 on success
1853  *      error code < 0 on error
1854  */
1855 static int start_cbsend(struct cardstate *cs)
1856 {
1857         struct cmdbuf_t *cb;
1858         struct bas_cardstate *ucs = cs->hw.bas;
1859         unsigned long flags;
1860         int rc;
1861         int retval = 0;
1862
1863         /* check if suspend requested */
1864         if (ucs->basstate & BS_SUSPEND) {
1865                 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1866                 return -EHOSTUNREACH;
1867         }
1868
1869         /* check if AT channel is open */
1870         if (!(ucs->basstate & BS_ATOPEN)) {
1871                 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1872                 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1873                 if (rc < 0) {
1874                         /* flush command queue */
1875                         spin_lock_irqsave(&cs->cmdlock, flags);
1876                         while (cs->cmdbuf != NULL)
1877                                 complete_cb(cs);
1878                         spin_unlock_irqrestore(&cs->cmdlock, flags);
1879                 }
1880                 return rc;
1881         }
1882
1883         /* try to send first command in queue */
1884         spin_lock_irqsave(&cs->cmdlock, flags);
1885
1886         while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1887                 ucs->retry_cmd_out = 0;
1888                 rc = atwrite_submit(cs, cb->buf, cb->len);
1889                 if (unlikely(rc)) {
1890                         retval = rc;
1891                         complete_cb(cs);
1892                 }
1893         }
1894
1895         spin_unlock_irqrestore(&cs->cmdlock, flags);
1896         return retval;
1897 }
1898
1899 /* gigaset_write_cmd
1900  * This function is called by the device independent part of the driver
1901  * to transmit an AT command string to the Gigaset device.
1902  * It encapsulates the device specific method for transmission over the
1903  * direct USB connection to the base.
1904  * The command string is added to the queue of commands to send, and
1905  * USB transmission is started if necessary.
1906  * parameters:
1907  *      cs              controller state structure
1908  *      buf             command string to send
1909  *      len             number of bytes to send (max. IF_WRITEBUF)
1910  *      wake_tasklet    tasklet to run when transmission is completed
1911  *                      (NULL if none)
1912  * return value:
1913  *      number of bytes queued on success
1914  *      error code < 0 on error
1915  */
1916 static int gigaset_write_cmd(struct cardstate *cs,
1917                              const unsigned char *buf, int len,
1918                              struct tasklet_struct *wake_tasklet)
1919 {
1920         struct cmdbuf_t *cb;
1921         unsigned long flags;
1922         int rc;
1923
1924         gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1925                              DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1926                            "CMD Transmit", len, buf);
1927
1928         if (len <= 0) {
1929                 /* nothing to do */
1930                 rc = 0;
1931                 goto notqueued;
1932         }
1933
1934         if (len > IF_WRITEBUF)
1935                 len = IF_WRITEBUF;
1936         if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1937                 dev_err(cs->dev, "%s: out of memory\n", __func__);
1938                 rc = -ENOMEM;
1939                 goto notqueued;
1940         }
1941
1942         memcpy(cb->buf, buf, len);
1943         cb->len = len;
1944         cb->offset = 0;
1945         cb->next = NULL;
1946         cb->wake_tasklet = wake_tasklet;
1947
1948         spin_lock_irqsave(&cs->cmdlock, flags);
1949         cb->prev = cs->lastcmdbuf;
1950         if (cs->lastcmdbuf)
1951                 cs->lastcmdbuf->next = cb;
1952         else {
1953                 cs->cmdbuf = cb;
1954                 cs->curlen = len;
1955         }
1956         cs->cmdbytes += len;
1957         cs->lastcmdbuf = cb;
1958         spin_unlock_irqrestore(&cs->cmdlock, flags);
1959
1960         spin_lock_irqsave(&cs->lock, flags);
1961         if (unlikely(!cs->connected)) {
1962                 spin_unlock_irqrestore(&cs->lock, flags);
1963                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1964                 /* flush command queue */
1965                 spin_lock_irqsave(&cs->cmdlock, flags);
1966                 while (cs->cmdbuf != NULL)
1967                         complete_cb(cs);
1968                 spin_unlock_irqrestore(&cs->cmdlock, flags);
1969                 return -ENODEV;
1970         }
1971         rc = start_cbsend(cs);
1972         spin_unlock_irqrestore(&cs->lock, flags);
1973         return rc < 0 ? rc : len;
1974
1975 notqueued:                      /* request handled without queuing */
1976         if (wake_tasklet)
1977                 tasklet_schedule(wake_tasklet);
1978         return rc;
1979 }
1980
1981 /* gigaset_write_room
1982  * tty_driver.write_room interface routine
1983  * return number of characters the driver will accept to be written via
1984  * gigaset_write_cmd
1985  * parameter:
1986  *      controller state structure
1987  * return value:
1988  *      number of characters
1989  */
1990 static int gigaset_write_room(struct cardstate *cs)
1991 {
1992         return IF_WRITEBUF;
1993 }
1994
1995 /* gigaset_chars_in_buffer
1996  * tty_driver.chars_in_buffer interface routine
1997  * return number of characters waiting to be sent
1998  * parameter:
1999  *      controller state structure
2000  * return value:
2001  *      number of characters
2002  */
2003 static int gigaset_chars_in_buffer(struct cardstate *cs)
2004 {
2005         return cs->cmdbytes;
2006 }
2007
2008 /* gigaset_brkchars
2009  * implementation of ioctl(GIGASET_BRKCHARS)
2010  * parameter:
2011  *      controller state structure
2012  * return value:
2013  *      -EINVAL (unimplemented function)
2014  */
2015 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2016 {
2017         return -EINVAL;
2018 }
2019
2020
2021 /* Device Initialization/Shutdown */
2022 /* ============================== */
2023
2024 /* Free hardware dependent part of the B channel structure
2025  * parameter:
2026  *      bcs     B channel structure
2027  * return value:
2028  *      !=0 on success
2029  */
2030 static int gigaset_freebcshw(struct bc_state *bcs)
2031 {
2032         struct bas_bc_state *ubc = bcs->hw.bas;
2033         int i;
2034
2035         if (!ubc)
2036                 return 0;
2037
2038         /* kill URBs and tasklets before freeing - better safe than sorry */
2039         ubc->running = 0;
2040         gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2041         for (i = 0; i < BAS_OUTURBS; ++i) {
2042                 usb_kill_urb(ubc->isoouturbs[i].urb);
2043                 usb_free_urb(ubc->isoouturbs[i].urb);
2044         }
2045         for (i = 0; i < BAS_INURBS; ++i) {
2046                 usb_kill_urb(ubc->isoinurbs[i]);
2047                 usb_free_urb(ubc->isoinurbs[i]);
2048         }
2049         tasklet_kill(&ubc->sent_tasklet);
2050         tasklet_kill(&ubc->rcvd_tasklet);
2051         kfree(ubc->isooutbuf);
2052         kfree(ubc);
2053         bcs->hw.bas = NULL;
2054         return 1;
2055 }
2056
2057 /* Initialize hardware dependent part of the B channel structure
2058  * parameter:
2059  *      bcs     B channel structure
2060  * return value:
2061  *      !=0 on success
2062  */
2063 static int gigaset_initbcshw(struct bc_state *bcs)
2064 {
2065         int i;
2066         struct bas_bc_state *ubc;
2067
2068         bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2069         if (!ubc) {
2070                 err("could not allocate bas_bc_state");
2071                 return 0;
2072         }
2073
2074         ubc->running = 0;
2075         atomic_set(&ubc->corrbytes, 0);
2076         spin_lock_init(&ubc->isooutlock);
2077         for (i = 0; i < BAS_OUTURBS; ++i) {
2078                 ubc->isoouturbs[i].urb = NULL;
2079                 ubc->isoouturbs[i].bcs = bcs;
2080         }
2081         ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2082         ubc->numsub = 0;
2083         if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2084                 err("could not allocate isochronous output buffer");
2085                 kfree(ubc);
2086                 bcs->hw.bas = NULL;
2087                 return 0;
2088         }
2089         tasklet_init(&ubc->sent_tasklet,
2090                      &write_iso_tasklet, (unsigned long) bcs);
2091
2092         spin_lock_init(&ubc->isoinlock);
2093         for (i = 0; i < BAS_INURBS; ++i)
2094                 ubc->isoinurbs[i] = NULL;
2095         ubc->isoindone = NULL;
2096         ubc->loststatus = -EINPROGRESS;
2097         ubc->isoinlost = 0;
2098         ubc->seqlen = 0;
2099         ubc->inbyte = 0;
2100         ubc->inbits = 0;
2101         ubc->goodbytes = 0;
2102         ubc->alignerrs = 0;
2103         ubc->fcserrs = 0;
2104         ubc->frameerrs = 0;
2105         ubc->giants = 0;
2106         ubc->runts = 0;
2107         ubc->aborts = 0;
2108         ubc->shared0s = 0;
2109         ubc->stolen0s = 0;
2110         tasklet_init(&ubc->rcvd_tasklet,
2111                      &read_iso_tasklet, (unsigned long) bcs);
2112         return 1;
2113 }
2114
2115 static void gigaset_reinitbcshw(struct bc_state *bcs)
2116 {
2117         struct bas_bc_state *ubc = bcs->hw.bas;
2118
2119         bcs->hw.bas->running = 0;
2120         atomic_set(&bcs->hw.bas->corrbytes, 0);
2121         bcs->hw.bas->numsub = 0;
2122         spin_lock_init(&ubc->isooutlock);
2123         spin_lock_init(&ubc->isoinlock);
2124         ubc->loststatus = -EINPROGRESS;
2125 }
2126
2127 static void gigaset_freecshw(struct cardstate *cs)
2128 {
2129         /* timers, URBs and rcvbuf are disposed of in disconnect */
2130         kfree(cs->hw.bas);
2131         cs->hw.bas = NULL;
2132 }
2133
2134 static int gigaset_initcshw(struct cardstate *cs)
2135 {
2136         struct bas_cardstate *ucs;
2137
2138         cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2139         if (!ucs)
2140                 return 0;
2141
2142         ucs->urb_cmd_in = NULL;
2143         ucs->urb_cmd_out = NULL;
2144         ucs->rcvbuf = NULL;
2145         ucs->rcvbuf_size = 0;
2146
2147         spin_lock_init(&ucs->lock);
2148         ucs->pending = 0;
2149
2150         ucs->basstate = 0;
2151         init_timer(&ucs->timer_ctrl);
2152         init_timer(&ucs->timer_atrdy);
2153         init_timer(&ucs->timer_cmd_in);
2154         init_waitqueue_head(&ucs->waitqueue);
2155
2156         return 1;
2157 }
2158
2159 /* freeurbs
2160  * unlink and deallocate all URBs unconditionally
2161  * caller must make sure that no commands are still in progress
2162  * parameter:
2163  *      cs      controller state structure
2164  */
2165 static void freeurbs(struct cardstate *cs)
2166 {
2167         struct bas_cardstate *ucs = cs->hw.bas;
2168         struct bas_bc_state *ubc;
2169         int i, j;
2170
2171         gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2172         for (j = 0; j < BAS_CHANNELS; ++j) {
2173                 ubc = cs->bcs[j].hw.bas;
2174                 for (i = 0; i < BAS_OUTURBS; ++i) {
2175                         usb_kill_urb(ubc->isoouturbs[i].urb);
2176                         usb_free_urb(ubc->isoouturbs[i].urb);
2177                         ubc->isoouturbs[i].urb = NULL;
2178                 }
2179                 for (i = 0; i < BAS_INURBS; ++i) {
2180                         usb_kill_urb(ubc->isoinurbs[i]);
2181                         usb_free_urb(ubc->isoinurbs[i]);
2182                         ubc->isoinurbs[i] = NULL;
2183                 }
2184         }
2185         usb_kill_urb(ucs->urb_int_in);
2186         usb_free_urb(ucs->urb_int_in);
2187         ucs->urb_int_in = NULL;
2188         usb_kill_urb(ucs->urb_cmd_out);
2189         usb_free_urb(ucs->urb_cmd_out);
2190         ucs->urb_cmd_out = NULL;
2191         usb_kill_urb(ucs->urb_cmd_in);
2192         usb_free_urb(ucs->urb_cmd_in);
2193         ucs->urb_cmd_in = NULL;
2194         usb_kill_urb(ucs->urb_ctrl);
2195         usb_free_urb(ucs->urb_ctrl);
2196         ucs->urb_ctrl = NULL;
2197 }
2198
2199 /* gigaset_probe
2200  * This function is called when a new USB device is connected.
2201  * It checks whether the new device is handled by this driver.
2202  */
2203 static int gigaset_probe(struct usb_interface *interface,
2204                          const struct usb_device_id *id)
2205 {
2206         struct usb_host_interface *hostif;
2207         struct usb_device *udev = interface_to_usbdev(interface);
2208         struct cardstate *cs = NULL;
2209         struct bas_cardstate *ucs = NULL;
2210         struct bas_bc_state *ubc;
2211         struct usb_endpoint_descriptor *endpoint;
2212         int i, j;
2213         int rc;
2214
2215         gig_dbg(DEBUG_ANY,
2216                 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2217                 __func__, le16_to_cpu(udev->descriptor.idVendor),
2218                 le16_to_cpu(udev->descriptor.idProduct));
2219
2220         /* set required alternate setting */
2221         hostif = interface->cur_altsetting;
2222         if (hostif->desc.bAlternateSetting != 3) {
2223                 gig_dbg(DEBUG_ANY,
2224                         "%s: wrong alternate setting %d - trying to switch",
2225                         __func__, hostif->desc.bAlternateSetting);
2226                 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2227                         dev_warn(&udev->dev, "usb_set_interface failed, "
2228                                  "device %d interface %d altsetting %d\n",
2229                                  udev->devnum, hostif->desc.bInterfaceNumber,
2230                                  hostif->desc.bAlternateSetting);
2231                         return -ENODEV;
2232                 }
2233                 hostif = interface->cur_altsetting;
2234         }
2235
2236         /* Reject application specific interfaces
2237          */
2238         if (hostif->desc.bInterfaceClass != 255) {
2239                 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2240                          __func__, hostif->desc.bInterfaceClass);
2241                 return -ENODEV;
2242         }
2243
2244         dev_info(&udev->dev,
2245                  "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2246                  __func__, le16_to_cpu(udev->descriptor.idVendor),
2247                  le16_to_cpu(udev->descriptor.idProduct));
2248
2249         /* allocate memory for our device state and intialize it */
2250         cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2251                             GIGASET_MODULENAME);
2252         if (!cs)
2253                 return -ENODEV;
2254         ucs = cs->hw.bas;
2255
2256         /* save off device structure ptrs for later use */
2257         usb_get_dev(udev);
2258         ucs->udev = udev;
2259         ucs->interface = interface;
2260         cs->dev = &interface->dev;
2261
2262         /* allocate URBs:
2263          * - one for the interrupt pipe
2264          * - three for the different uses of the default control pipe
2265          * - three for each isochronous pipe
2266          */
2267         if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2268             !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2269             !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2270             !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2271                 goto allocerr;
2272
2273         for (j = 0; j < BAS_CHANNELS; ++j) {
2274                 ubc = cs->bcs[j].hw.bas;
2275                 for (i = 0; i < BAS_OUTURBS; ++i)
2276                         if (!(ubc->isoouturbs[i].urb =
2277                               usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2278                                 goto allocerr;
2279                 for (i = 0; i < BAS_INURBS; ++i)
2280                         if (!(ubc->isoinurbs[i] =
2281                               usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2282                                 goto allocerr;
2283         }
2284
2285         ucs->rcvbuf = NULL;
2286         ucs->rcvbuf_size = 0;
2287
2288         /* Fill the interrupt urb and send it to the core */
2289         endpoint = &hostif->endpoint[0].desc;
2290         usb_fill_int_urb(ucs->urb_int_in, udev,
2291                          usb_rcvintpipe(udev,
2292                                         (endpoint->bEndpointAddress) & 0x0f),
2293                          ucs->int_in_buf, 3, read_int_callback, cs,
2294                          endpoint->bInterval);
2295         if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
2296                 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2297                         get_usb_rcmsg(rc));
2298                 goto error;
2299         }
2300
2301         /* tell the device that the driver is ready */
2302         if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2303                 goto error;
2304
2305         /* tell common part that the device is ready */
2306         if (startmode == SM_LOCKED)
2307                 cs->mstate = MS_LOCKED;
2308
2309         /* save address of controller structure */
2310         usb_set_intfdata(interface, cs);
2311
2312         if (!gigaset_start(cs))
2313                 goto error;
2314
2315         return 0;
2316
2317 allocerr:
2318         dev_err(cs->dev, "could not allocate URBs\n");
2319 error:
2320         freeurbs(cs);
2321         usb_set_intfdata(interface, NULL);
2322         gigaset_freecs(cs);
2323         return -ENODEV;
2324 }
2325
2326 /* gigaset_disconnect
2327  * This function is called when the Gigaset base is unplugged.
2328  */
2329 static void gigaset_disconnect(struct usb_interface *interface)
2330 {
2331         struct cardstate *cs;
2332         struct bas_cardstate *ucs;
2333         int j;
2334
2335         cs = usb_get_intfdata(interface);
2336
2337         ucs = cs->hw.bas;
2338
2339         dev_info(cs->dev, "disconnecting Gigaset base\n");
2340
2341         /* mark base as not ready, all channels disconnected */
2342         ucs->basstate = 0;
2343
2344         /* tell LL all channels are down */
2345         for (j = 0; j < BAS_CHANNELS; ++j)
2346                 gigaset_bchannel_down(cs->bcs + j);
2347
2348         /* stop driver (common part) */
2349         gigaset_stop(cs);
2350
2351         /* stop timers and URBs, free ressources */
2352         del_timer_sync(&ucs->timer_ctrl);
2353         del_timer_sync(&ucs->timer_atrdy);
2354         del_timer_sync(&ucs->timer_cmd_in);
2355         freeurbs(cs);
2356         usb_set_intfdata(interface, NULL);
2357         kfree(ucs->rcvbuf);
2358         ucs->rcvbuf = NULL;
2359         ucs->rcvbuf_size = 0;
2360         usb_put_dev(ucs->udev);
2361         ucs->interface = NULL;
2362         ucs->udev = NULL;
2363         cs->dev = NULL;
2364         gigaset_freecs(cs);
2365 }
2366
2367 /* gigaset_suspend
2368  * This function is called before the USB connection is suspended.
2369  */
2370 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2371 {
2372         struct cardstate *cs = usb_get_intfdata(intf);
2373         struct bas_cardstate *ucs = cs->hw.bas;
2374         int rc;
2375
2376         /* set suspend flag; this stops AT command/response traffic */
2377         if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2378                 gig_dbg(DEBUG_SUSPEND, "already suspended");
2379                 return 0;
2380         }
2381
2382         /* wait a bit for blocking conditions to go away */
2383         rc = wait_event_timeout(ucs->waitqueue,
2384                         !(ucs->basstate &
2385                           (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2386                         BAS_TIMEOUT*HZ/10);
2387         gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2388
2389         /* check for conditions preventing suspend */
2390         if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
2391                 dev_warn(cs->dev, "cannot suspend:\n");
2392                 if (ucs->basstate & BS_B1OPEN)
2393                         dev_warn(cs->dev, " B channel 1 open\n");
2394                 if (ucs->basstate & BS_B2OPEN)
2395                         dev_warn(cs->dev, " B channel 2 open\n");
2396                 if (ucs->basstate & BS_ATRDPEND)
2397                         dev_warn(cs->dev, " receiving AT reply\n");
2398                 if (ucs->basstate & BS_ATWRPEND)
2399                         dev_warn(cs->dev, " sending AT command\n");
2400                 update_basstate(ucs, 0, BS_SUSPEND);
2401                 return -EBUSY;
2402         }
2403
2404         /* close AT channel if open */
2405         if (ucs->basstate & BS_ATOPEN) {
2406                 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2407                 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2408                 if (rc) {
2409                         update_basstate(ucs, 0, BS_SUSPEND);
2410                         return rc;
2411                 }
2412                 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2413                                    BAS_TIMEOUT*HZ/10);
2414                 /* in case of timeout, proceed anyway */
2415         }
2416
2417         /* kill all URBs and timers that might still be pending */
2418         usb_kill_urb(ucs->urb_ctrl);
2419         usb_kill_urb(ucs->urb_int_in);
2420         del_timer_sync(&ucs->timer_ctrl);
2421
2422         gig_dbg(DEBUG_SUSPEND, "suspend complete");
2423         return 0;
2424 }
2425
2426 /* gigaset_resume
2427  * This function is called after the USB connection has been resumed.
2428  */
2429 static int gigaset_resume(struct usb_interface *intf)
2430 {
2431         struct cardstate *cs = usb_get_intfdata(intf);
2432         struct bas_cardstate *ucs = cs->hw.bas;
2433         int rc;
2434
2435         /* resubmit interrupt URB for spontaneous messages from base */
2436         rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2437         if (rc) {
2438                 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2439                         get_usb_rcmsg(rc));
2440                 return rc;
2441         }
2442
2443         /* clear suspend flag to reallow activity */
2444         update_basstate(ucs, 0, BS_SUSPEND);
2445
2446         gig_dbg(DEBUG_SUSPEND, "resume complete");
2447         return 0;
2448 }
2449
2450 /* gigaset_pre_reset
2451  * This function is called before the USB connection is reset.
2452  */
2453 static int gigaset_pre_reset(struct usb_interface *intf)
2454 {
2455         /* handle just like suspend */
2456         return gigaset_suspend(intf, PMSG_ON);
2457 }
2458
2459 /* gigaset_post_reset
2460  * This function is called after the USB connection has been reset.
2461  */
2462 static int gigaset_post_reset(struct usb_interface *intf)
2463 {
2464         /* FIXME: send HD_DEVICE_INIT_ACK? */
2465
2466         /* resume operations */
2467         return gigaset_resume(intf);
2468 }
2469
2470
2471 static const struct gigaset_ops gigops = {
2472         gigaset_write_cmd,
2473         gigaset_write_room,
2474         gigaset_chars_in_buffer,
2475         gigaset_brkchars,
2476         gigaset_init_bchannel,
2477         gigaset_close_bchannel,
2478         gigaset_initbcshw,
2479         gigaset_freebcshw,
2480         gigaset_reinitbcshw,
2481         gigaset_initcshw,
2482         gigaset_freecshw,
2483         gigaset_set_modem_ctrl,
2484         gigaset_baud_rate,
2485         gigaset_set_line_ctrl,
2486         gigaset_isoc_send_skb,
2487         gigaset_isoc_input,
2488 };
2489
2490 /* bas_gigaset_init
2491  * This function is called after the kernel module is loaded.
2492  */
2493 static int __init bas_gigaset_init(void)
2494 {
2495         int result;
2496
2497         /* allocate memory for our driver state and intialize it */
2498         if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2499                                        GIGASET_MODULENAME, GIGASET_DEVNAME,
2500                                        &gigops, THIS_MODULE)) == NULL)
2501                 goto error;
2502
2503         /* register this driver with the USB subsystem */
2504         result = usb_register(&gigaset_usb_driver);
2505         if (result < 0) {
2506                 err("usb_register failed (error %d)", -result);
2507                 goto error;
2508         }
2509
2510         info(DRIVER_AUTHOR);
2511         info(DRIVER_DESC);
2512         return 0;
2513
2514 error:
2515         if (driver)
2516                 gigaset_freedriver(driver);
2517         driver = NULL;
2518         return -1;
2519 }
2520
2521 /* bas_gigaset_exit
2522  * This function is called before the kernel module is unloaded.
2523  */
2524 static void __exit bas_gigaset_exit(void)
2525 {
2526         struct bas_cardstate *ucs;
2527         int i;
2528
2529         gigaset_blockdriver(driver); /* => probe will fail
2530                                       * => no gigaset_start any more
2531                                       */
2532
2533         /* stop all connected devices */
2534         for (i = 0; i < driver->minors; i++) {
2535                 if (gigaset_shutdown(driver->cs + i) < 0)
2536                         continue;               /* no device */
2537                 /* from now on, no isdn callback should be possible */
2538
2539                 /* close all still open channels */
2540                 ucs = driver->cs[i].hw.bas;
2541                 if (ucs->basstate & BS_B1OPEN) {
2542                         gig_dbg(DEBUG_INIT, "closing B1 channel");
2543                         usb_control_msg(ucs->udev,
2544                                         usb_sndctrlpipe(ucs->udev, 0),
2545                                         HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2546                                         0, 0, NULL, 0, BAS_TIMEOUT);
2547                 }
2548                 if (ucs->basstate & BS_B2OPEN) {
2549                         gig_dbg(DEBUG_INIT, "closing B2 channel");
2550                         usb_control_msg(ucs->udev,
2551                                         usb_sndctrlpipe(ucs->udev, 0),
2552                                         HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2553                                         0, 0, NULL, 0, BAS_TIMEOUT);
2554                 }
2555                 if (ucs->basstate & BS_ATOPEN) {
2556                         gig_dbg(DEBUG_INIT, "closing AT channel");
2557                         usb_control_msg(ucs->udev,
2558                                         usb_sndctrlpipe(ucs->udev, 0),
2559                                         HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2560                                         0, 0, NULL, 0, BAS_TIMEOUT);
2561                 }
2562                 ucs->basstate = 0;
2563         }
2564
2565         /* deregister this driver with the USB subsystem */
2566         usb_deregister(&gigaset_usb_driver);
2567         /* this will call the disconnect-callback */
2568         /* from now on, no disconnect/probe callback should be running */
2569
2570         gigaset_freedriver(driver);
2571         driver = NULL;
2572 }
2573
2574
2575 module_init(bas_gigaset_init);
2576 module_exit(bas_gigaset_exit);
2577
2578 MODULE_AUTHOR(DRIVER_AUTHOR);
2579 MODULE_DESCRIPTION(DRIVER_DESC);
2580 MODULE_LICENSE("GPL");