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