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