2 * USB driver for Gigaset 307x base via direct USB connection.
4 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5 * Tilman Schmidt <tilman@imap.cc>,
8 * Based on usb-gigaset.c.
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 * =====================================================================
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>
28 /* Version Information */
29 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
30 #define DRIVER_DESC "USB Driver for Gigaset 307x"
33 /* Module parameters */
35 static int startmode = SM_ISDN;
36 static int cidmode = 1;
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");
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"
49 #define IF_WRITEBUF 256 //FIXME
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
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 */
67 MODULE_DEVICE_TABLE(usb, gigaset_table);
69 /*======================= local function prototypes =============================*/
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.
74 static int gigaset_probe(struct usb_interface *interface,
75 const struct usb_device_id *id);
77 /* Function will be called if the device is unplugged */
78 static void gigaset_disconnect(struct usb_interface *interface);
81 /*==============================================================================*/
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 */
88 struct urb *urb_ctrl; /* control pipe default URB */
89 struct usb_ctrlrequest dr_ctrl;
90 struct timer_list timer_ctrl; /* control request timeout */
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;
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 */
102 struct urb *urb_int_in; /* URB for interrupt pipe */
103 unsigned char int_in_buf[3];
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 */
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
122 static struct gigaset_driver *driver = NULL;
123 static struct cardstate *cardstate = NULL;
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,
133 /* get message text for USB status code
135 static char *get_usb_statmsg(int status)
137 static char unkmsg[28];
145 return "canceled (async)";
149 return "bit stuffing or unknown USB error";
151 return "Illegal byte sequence (CRC mismatch)";
153 return "babble detect or endpoint stalled";
155 return "buffer error";
159 return "device not present";
161 return "short packet detected";
163 return "partial isochronous transfer";
165 return "invalid argument";
167 return "URB already queued";
169 return "isochronous start frame too early or too much scheduled";
171 return "too many isochronous frames requested";
173 return "endpoint message size zero";
175 return "endpoint shutdown";
177 return "another request pending";
179 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
185 * retrieve string representation of USB pipe type
187 static inline char *usb_pipetype_str(int pipe)
189 if (usb_pipeisoc(pipe))
191 if (usb_pipeint(pipe))
193 if (usb_pipecontrol(pipe))
195 if (usb_pipebulk(pipe))
201 * write content of URB to syslog for debugging
203 static inline void dump_urb(enum debuglevel level, const char *tag,
206 #ifdef CONFIG_GIGASET_DEBUG
208 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
211 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
212 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
213 (unsigned long) urb->dev,
214 usb_pipetype_str(urb->pipe),
215 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
216 usb_pipein(urb->pipe) ? "in" : "out",
217 urb->status, (unsigned long) urb->hcpriv,
218 urb->transfer_flags);
220 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
221 "bandwidth=%d, setup_packet=0x%08lx,",
222 (unsigned long) urb->transfer_buffer,
223 urb->transfer_buffer_length, urb->actual_length,
224 urb->bandwidth, (unsigned long) urb->setup_packet);
226 " start_frame=%d, number_of_packets=%d, interval=%d, "
228 urb->start_frame, urb->number_of_packets, urb->interval,
231 " context=0x%08lx, complete=0x%08lx, "
232 "iso_frame_desc[]={",
233 (unsigned long) urb->context,
234 (unsigned long) urb->complete);
235 for (i = 0; i < urb->number_of_packets; i++) {
236 struct usb_iso_packet_descriptor *pifd
237 = &urb->iso_frame_desc[i];
239 " {offset=%u, length=%u, actual_length=%u, "
241 pifd->offset, pifd->length, pifd->actual_length,
245 gig_dbg(level, "}}");
249 /* read/set modem control bits etc. (m10x only) */
250 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
256 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
261 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
267 * hang up any existing connection because of an unrecoverable error
268 * This function may be called from any context and takes care of scheduling
269 * the necessary actions for execution outside of interrupt context.
271 * B channel control structure
273 static inline void error_hangup(struct bc_state *bcs)
275 struct cardstate *cs = bcs->cs;
277 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
278 __func__, bcs->channel);
280 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
281 //FIXME what should we do?
285 gigaset_schedule_event(cs);
289 * reset Gigaset device because of an unrecoverable error
290 * This function may be called from any context and takes care of scheduling
291 * the necessary actions for execution outside of interrupt context.
293 * controller state structure
295 static inline void error_reset(struct cardstate *cs)
297 //FIXME try to recover without bothering the user
299 "unrecoverable error - please disconnect Gigaset base to reset\n");
303 * check for completion of pending control request
305 * ucs hardware specific controller state structure
307 static void check_pending(struct bas_cardstate *ucs)
311 spin_lock_irqsave(&ucs->lock, flags);
312 switch (ucs->pending) {
315 case HD_OPEN_ATCHANNEL:
316 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
319 case HD_OPEN_B1CHANNEL:
320 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
323 case HD_OPEN_B2CHANNEL:
324 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
327 case HD_CLOSE_ATCHANNEL:
328 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
331 case HD_CLOSE_B1CHANNEL:
332 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
335 case HD_CLOSE_B2CHANNEL:
336 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
339 case HD_DEVICE_INIT_ACK: /* no reply expected */
342 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
343 * are handled separately and should never end up here
346 dev_warn(&ucs->interface->dev,
347 "unknown pending request 0x%02x cleared\n",
353 del_timer(&ucs->timer_ctrl);
355 spin_unlock_irqrestore(&ucs->lock, flags);
359 * timeout routine for command input request
361 * controller state structure
363 static void cmd_in_timeout(unsigned long data)
365 struct cardstate *cs = (struct cardstate *) data;
366 struct bas_cardstate *ucs = cs->hw.bas;
369 spin_lock_irqsave(&cs->lock, flags);
370 if (unlikely(!atomic_read(&cs->connected))) {
371 gig_dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
372 spin_unlock_irqrestore(&cs->lock, flags);
375 if (!ucs->rcvbuf_size) {
376 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
377 spin_unlock_irqrestore(&cs->lock, flags);
380 spin_unlock_irqrestore(&cs->lock, flags);
382 dev_err(cs->dev, "timeout reading AT response\n");
383 error_reset(cs); //FIXME retry?
387 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
390 * submit an HD_READ_ATMESSAGE command URB
392 * cs controller state structure
393 * timeout timeout in 1/10 sec., 0: none
396 * -EINVAL if a NULL pointer is encountered somewhere
397 * -EBUSY if another request is pending
398 * any URB submission error code
400 static int atread_submit(struct cardstate *cs, int timeout)
402 struct bas_cardstate *ucs = cs->hw.bas;
405 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
408 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
410 "could not submit HD_READ_ATMESSAGE: URB busy\n");
414 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
415 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
416 ucs->dr_cmd_in.wValue = 0;
417 ucs->dr_cmd_in.wIndex = 0;
418 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
419 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
420 usb_rcvctrlpipe(ucs->udev, 0),
421 (unsigned char*) & ucs->dr_cmd_in,
422 ucs->rcvbuf, ucs->rcvbuf_size,
423 read_ctrl_callback, cs->inbuf);
425 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
426 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
427 get_usb_statmsg(ret));
432 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
433 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
434 ucs->timer_cmd_in.data = (unsigned long) cs;
435 ucs->timer_cmd_in.function = cmd_in_timeout;
436 add_timer(&ucs->timer_cmd_in);
441 static void stopurbs(struct bas_bc_state *);
442 static int start_cbsend(struct cardstate *);
444 /* set/clear bits in base connection state
446 inline static void update_basstate(struct bas_cardstate *ucs,
452 spin_lock_irqsave(&ucs->lock, flags);
453 state = atomic_read(&ucs->basstate);
456 atomic_set(&ucs->basstate, state);
457 spin_unlock_irqrestore(&ucs->lock, flags);
462 * USB completion handler for interrupt pipe input
463 * called by the USB subsystem in interrupt context
465 * urb USB request block
466 * urb->context = controller state structure
468 static void read_int_callback(struct urb *urb, struct pt_regs *regs)
470 struct cardstate *cs = urb->context;
471 struct bas_cardstate *ucs = cs->hw.bas;
472 struct bc_state *bcs;
478 if (unlikely(!atomic_read(&cs->connected))) {
479 warn("%s: disconnected", __func__);
483 switch (urb->status) {
484 case 0: /* success */
486 case -ENOENT: /* canceled */
487 case -ECONNRESET: /* canceled (async) */
488 case -EINPROGRESS: /* pending */
489 /* ignore silently */
490 gig_dbg(DEBUG_USBREQ, "%s: %s",
491 __func__, get_usb_statmsg(urb->status));
493 default: /* severe trouble */
494 dev_warn(cs->dev, "interrupt read: %s\n",
495 get_usb_statmsg(urb->status));
496 //FIXME corrective action? resubmission always ok?
500 l = (unsigned) ucs->int_in_buf[1] +
501 (((unsigned) ucs->int_in_buf[2]) << 8);
503 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
504 urb->actual_length, (int)ucs->int_in_buf[0], l,
505 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
509 switch (ucs->int_in_buf[0]) {
510 case HD_DEVICE_INIT_OK:
511 update_basstate(ucs, BS_INIT, 0);
514 case HD_READY_SEND_ATDATA:
515 del_timer(&ucs->timer_atrdy);
516 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
520 case HD_OPEN_B2CHANNEL_ACK:
522 case HD_OPEN_B1CHANNEL_ACK:
523 bcs = cs->bcs + channel;
524 update_basstate(ucs, BS_B1OPEN << channel, 0);
525 gigaset_bchannel_up(bcs);
528 case HD_OPEN_ATCHANNEL_ACK:
529 update_basstate(ucs, BS_ATOPEN, 0);
533 case HD_CLOSE_B2CHANNEL_ACK:
535 case HD_CLOSE_B1CHANNEL_ACK:
536 bcs = cs->bcs + channel;
537 update_basstate(ucs, 0, BS_B1OPEN << channel);
538 stopurbs(bcs->hw.bas);
539 gigaset_bchannel_down(bcs);
542 case HD_CLOSE_ATCHANNEL_ACK:
543 update_basstate(ucs, 0, BS_ATOPEN);
546 case HD_B2_FLOW_CONTROL:
548 case HD_B1_FLOW_CONTROL:
549 bcs = cs->bcs + channel;
550 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
551 &bcs->hw.bas->corrbytes);
553 "Flow control (channel %d, sub %d): 0x%02x => %d",
554 channel, bcs->hw.bas->numsub, l,
555 atomic_read(&bcs->hw.bas->corrbytes));
558 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
561 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
564 spin_lock_irqsave(&cs->lock, flags);
565 if (ucs->rcvbuf_size) {
566 spin_unlock_irqrestore(&cs->lock, flags);
568 "receive AT data overrun, %d bytes lost\n", l);
569 error_reset(cs); //FIXME reschedule
572 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
573 spin_unlock_irqrestore(&cs->lock, flags);
574 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
575 error_reset(cs); //FIXME reschedule
578 ucs->rcvbuf_size = l;
579 ucs->retry_cmd_in = 0;
580 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
583 ucs->rcvbuf_size = 0;
584 error_reset(cs); //FIXME reschedule
586 spin_unlock_irqrestore(&cs->lock, flags);
589 case HD_RESET_INTERRUPT_PIPE_ACK:
590 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
594 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
599 "unknown Gigaset signal 0x%02x (%u) ignored\n",
600 (int) ucs->int_in_buf[0], l);
606 status = usb_submit_urb(urb, SLAB_ATOMIC);
607 if (unlikely(status)) {
608 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
609 get_usb_statmsg(status));
614 /* read_ctrl_callback
615 * USB completion handler for control pipe input
616 * called by the USB subsystem in interrupt context
618 * urb USB request block
619 * urb->context = inbuf structure for controller state
621 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
623 struct inbuf_t *inbuf = urb->context;
624 struct cardstate *cs = inbuf->cs;
625 struct bas_cardstate *ucs = cs->hw.bas;
630 spin_lock_irqsave(&cs->lock, flags);
631 if (unlikely(!atomic_read(&cs->connected))) {
632 warn("%s: disconnected", __func__);
633 spin_unlock_irqrestore(&cs->lock, flags);
637 if (!ucs->rcvbuf_size) {
638 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
639 spin_unlock_irqrestore(&cs->lock, flags);
643 del_timer(&ucs->timer_cmd_in);
645 switch (urb->status) {
646 case 0: /* normal completion */
647 numbytes = urb->actual_length;
648 if (unlikely(numbytes == 0)) {
650 "control read: empty block received\n");
653 if (unlikely(numbytes != ucs->rcvbuf_size)) {
655 "control read: received %d chars, expected %d\n",
656 numbytes, ucs->rcvbuf_size);
657 if (numbytes > ucs->rcvbuf_size)
658 numbytes = ucs->rcvbuf_size;
661 /* copy received bytes to inbuf */
662 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
664 if (unlikely(numbytes < ucs->rcvbuf_size)) {
665 /* incomplete - resubmit for remaining bytes */
666 ucs->rcvbuf_size -= numbytes;
667 ucs->retry_cmd_in = 0;
672 case -ENOENT: /* canceled */
673 case -ECONNRESET: /* canceled (async) */
674 case -EINPROGRESS: /* pending */
675 /* no action necessary */
676 gig_dbg(DEBUG_USBREQ, "%s: %s",
677 __func__, get_usb_statmsg(urb->status));
680 default: /* severe trouble */
681 dev_warn(cs->dev, "control read: %s\n",
682 get_usb_statmsg(urb->status));
684 if (ucs->retry_cmd_in++ < BAS_RETRY) {
685 dev_notice(cs->dev, "control read: retry %d\n",
687 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
688 /* resubmitted - bypass regular exit block */
689 spin_unlock_irqrestore(&cs->lock, flags);
694 "control read: giving up after %d tries\n",
702 ucs->rcvbuf_size = 0;
703 spin_unlock_irqrestore(&cs->lock, flags);
705 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
706 gigaset_schedule_event(cs);
711 * USB completion handler for B channel isochronous input
712 * called by the USB subsystem in interrupt context
714 * urb USB request block of completed request
715 * urb->context = bc_state structure
717 static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
719 struct bc_state *bcs;
720 struct bas_bc_state *ubc;
724 /* status codes not worth bothering the tasklet with */
725 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
726 urb->status == -EINPROGRESS)) {
727 gig_dbg(DEBUG_ISO, "%s: %s",
728 __func__, get_usb_statmsg(urb->status));
735 spin_lock_irqsave(&ubc->isoinlock, flags);
736 if (likely(ubc->isoindone == NULL)) {
737 /* pass URB to tasklet */
738 ubc->isoindone = urb;
739 tasklet_schedule(&ubc->rcvd_tasklet);
741 /* tasklet still busy, drop data and resubmit URB */
742 ubc->loststatus = urb->status;
743 for (i = 0; i < BAS_NUMFRAMES; i++) {
744 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
745 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
746 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
747 ubc->loststatus = urb->iso_frame_desc[i].status;
749 urb->iso_frame_desc[i].status = 0;
750 urb->iso_frame_desc[i].actual_length = 0;
752 if (likely(atomic_read(&ubc->running))) {
753 /* urb->dev is clobbered by USB subsystem */
754 urb->dev = bcs->cs->hw.bas->udev;
755 urb->transfer_flags = URB_ISO_ASAP;
756 urb->number_of_packets = BAS_NUMFRAMES;
757 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
759 rc = usb_submit_urb(urb, SLAB_ATOMIC);
760 if (unlikely(rc != 0)) {
761 dev_err(bcs->cs->dev,
762 "could not resubmit isochronous read "
763 "URB: %s\n", get_usb_statmsg(rc));
764 dump_urb(DEBUG_ISO, "isoc read", urb);
769 spin_unlock_irqrestore(&ubc->isoinlock, flags);
772 /* write_iso_callback
773 * USB completion handler for B channel isochronous output
774 * called by the USB subsystem in interrupt context
776 * urb USB request block of completed request
777 * urb->context = isow_urbctx_t structure
779 static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
781 struct isow_urbctx_t *ucx;
782 struct bas_bc_state *ubc;
785 /* status codes not worth bothering the tasklet with */
786 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
787 urb->status == -EINPROGRESS)) {
788 gig_dbg(DEBUG_ISO, "%s: %s",
789 __func__, get_usb_statmsg(urb->status));
793 /* pass URB context to tasklet */
795 ubc = ucx->bcs->hw.bas;
797 spin_lock_irqsave(&ubc->isooutlock, flags);
798 ubc->isooutovfl = ubc->isooutdone;
799 ubc->isooutdone = ucx;
800 spin_unlock_irqrestore(&ubc->isooutlock, flags);
801 tasklet_schedule(&ubc->sent_tasklet);
805 * prepare and submit USB request blocks for isochronous input and output
807 * B channel control structure
810 * < 0 on error (no URBs submitted)
812 static int starturbs(struct bc_state *bcs)
814 struct bas_bc_state *ubc = bcs->hw.bas;
819 /* initialize L2 reception */
820 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
821 bcs->inputstate |= INS_flag_hunt;
823 /* submit all isochronous input URBs */
824 atomic_set(&ubc->running, 1);
825 for (k = 0; k < BAS_INURBS; k++) {
826 urb = ubc->isoinurbs[k];
828 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
833 urb->dev = bcs->cs->hw.bas->udev;
834 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
835 urb->transfer_flags = URB_ISO_ASAP;
836 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
837 urb->transfer_buffer_length = BAS_INBUFSIZE;
838 urb->number_of_packets = BAS_NUMFRAMES;
839 urb->interval = BAS_FRAMETIME;
840 urb->complete = read_iso_callback;
842 for (j = 0; j < BAS_NUMFRAMES; j++) {
843 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
844 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
845 urb->iso_frame_desc[j].status = 0;
846 urb->iso_frame_desc[j].actual_length = 0;
849 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
850 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
851 dev_err(bcs->cs->dev,
852 "could not submit isochronous read URB %d: %s\n",
853 k, get_usb_statmsg(rc));
858 /* initialize L2 transmission */
859 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
861 /* set up isochronous output URBs for flag idling */
862 for (k = 0; k < BAS_OUTURBS; ++k) {
863 urb = ubc->isoouturbs[k].urb;
865 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
869 urb->dev = bcs->cs->hw.bas->udev;
870 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
871 urb->transfer_flags = URB_ISO_ASAP;
872 urb->transfer_buffer = ubc->isooutbuf->data;
873 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
874 urb->number_of_packets = BAS_NUMFRAMES;
875 urb->interval = BAS_FRAMETIME;
876 urb->complete = write_iso_callback;
877 urb->context = &ubc->isoouturbs[k];
878 for (j = 0; j < BAS_NUMFRAMES; ++j) {
879 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
880 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
881 urb->iso_frame_desc[j].status = 0;
882 urb->iso_frame_desc[j].actual_length = 0;
884 ubc->isoouturbs[k].limit = -1;
887 /* submit two URBs, keep third one */
888 for (k = 0; k < 2; ++k) {
889 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
890 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
892 dev_err(bcs->cs->dev,
893 "could not submit isochronous write URB %d: %s\n",
894 k, get_usb_statmsg(rc));
898 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
899 ubc->isooutfree = &ubc->isoouturbs[2];
900 ubc->isooutdone = ubc->isooutovfl = NULL;
908 * cancel the USB request blocks for isochronous input and output
909 * errors are silently ignored
911 * B channel control structure
913 static void stopurbs(struct bas_bc_state *ubc)
917 atomic_set(&ubc->running, 0);
919 for (k = 0; k < BAS_INURBS; ++k) {
920 rc = usb_unlink_urb(ubc->isoinurbs[k]);
922 "%s: isoc input URB %d unlinked, result = %d",
926 for (k = 0; k < BAS_OUTURBS; ++k) {
927 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
929 "%s: isoc output URB %d unlinked, result = %d",
934 /* Isochronous Write - Bottom Half */
935 /* =============================== */
937 /* submit_iso_write_urb
938 * fill and submit the next isochronous write URB
940 * bcs B channel state structure
942 * number of frames submitted in URB
943 * 0 if URB not submitted because no data available (isooutbuf busy)
944 * error code < 0 on error
946 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
948 struct urb *urb = ucx->urb;
949 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
950 struct usb_iso_packet_descriptor *ifd;
951 int corrbytes, nframe, rc;
953 /* urb->dev is clobbered by USB subsystem */
954 urb->dev = ucx->bcs->cs->hw.bas->udev;
955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
959 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
960 ifd = &urb->iso_frame_desc[nframe];
962 /* compute frame length according to flow control */
963 ifd->length = BAS_NORMFRAME;
964 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
965 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
966 __func__, corrbytes);
967 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
968 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
969 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
970 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
971 ifd->length += corrbytes;
972 atomic_add(-corrbytes, &ubc->corrbytes);
975 /* retrieve block of data to send */
976 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
978 if (ifd->offset < 0) {
979 if (ifd->offset == -EBUSY) {
981 "%s: buffer busy at frame %d",
983 /* tasklet will be restarted from
984 gigaset_send_skb() */
986 dev_err(ucx->bcs->cs->dev,
987 "%s: buffer error %d at frame %d\n",
988 __func__, ifd->offset, nframe);
993 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
995 ifd->actual_length = 0;
997 if ((urb->number_of_packets = nframe) > 0) {
998 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
999 dev_err(ucx->bcs->cs->dev,
1000 "could not submit isochronous write URB: %s\n",
1001 get_usb_statmsg(rc));
1002 dump_urb(DEBUG_ISO, "isoc write", urb);
1010 /* write_iso_tasklet
1011 * tasklet scheduled when an isochronous output URB from the Gigaset device
1014 * data B channel state structure
1016 static void write_iso_tasklet(unsigned long data)
1018 struct bc_state *bcs = (struct bc_state *) data;
1019 struct bas_bc_state *ubc = bcs->hw.bas;
1020 struct cardstate *cs = bcs->cs;
1021 struct isow_urbctx_t *done, *next, *ovfl;
1023 struct usb_iso_packet_descriptor *ifd;
1025 unsigned long flags;
1027 struct sk_buff *skb;
1030 /* loop while completed URBs arrive in time */
1032 if (unlikely(!atomic_read(&cs->connected))) {
1033 warn("%s: disconnected", __func__);
1037 if (unlikely(!(atomic_read(&ubc->running)))) {
1038 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1042 /* retrieve completed URBs */
1043 spin_lock_irqsave(&ubc->isooutlock, flags);
1044 done = ubc->isooutdone;
1045 ubc->isooutdone = NULL;
1046 ovfl = ubc->isooutovfl;
1047 ubc->isooutovfl = NULL;
1048 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1050 dev_err(cs->dev, "isochronous write buffer underrun\n");
1057 /* submit free URB if available */
1058 spin_lock_irqsave(&ubc->isooutlock, flags);
1059 next = ubc->isooutfree;
1060 ubc->isooutfree = NULL;
1061 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1063 if (submit_iso_write_urb(next) <= 0) {
1064 /* could not submit URB, put it back */
1065 spin_lock_irqsave(&ubc->isooutlock, flags);
1066 if (ubc->isooutfree == NULL) {
1067 ubc->isooutfree = next;
1070 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1072 /* couldn't put it back */
1074 "losing isochronous write URB\n");
1080 /* process completed URB */
1082 switch (urb->status) {
1083 case 0: /* normal completion */
1085 case -EXDEV: /* inspect individual frames */
1086 /* assumptions (for lack of documentation):
1087 * - actual_length bytes of the frame in error are
1089 * - all following frames are not sent at all
1091 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1093 offset = done->limit; /* just in case */
1094 for (i = 0; i < BAS_NUMFRAMES; i++) {
1095 ifd = &urb->iso_frame_desc[i];
1097 ifd->actual_length != ifd->length) {
1099 "isochronous write: frame %d: %s, "
1100 "only %d of %d bytes sent\n",
1101 i, get_usb_statmsg(ifd->status),
1102 ifd->actual_length, ifd->length);
1103 offset = (ifd->offset +
1109 #ifdef CONFIG_GIGASET_DEBUG
1110 /* check assumption on remaining frames */
1111 for (; i < BAS_NUMFRAMES; i++) {
1112 ifd = &urb->iso_frame_desc[i];
1113 if (ifd->status != -EINPROGRESS
1114 || ifd->actual_length != 0) {
1116 "isochronous write: frame %d: %s, "
1117 "%d of %d bytes sent\n",
1118 i, get_usb_statmsg(ifd->status),
1119 ifd->actual_length, ifd->length);
1120 offset = (ifd->offset +
1128 case -EPIPE: //FIXME is this the code for "underrun"?
1129 dev_err(cs->dev, "isochronous write stalled\n");
1132 default: /* severe trouble */
1133 dev_warn(cs->dev, "isochronous write: %s\n",
1134 get_usb_statmsg(urb->status));
1137 /* mark the write buffer area covered by this URB as free */
1138 if (done->limit >= 0)
1139 atomic_set(&ubc->isooutbuf->read, done->limit);
1141 /* mark URB as free */
1142 spin_lock_irqsave(&ubc->isooutlock, flags);
1143 next = ubc->isooutfree;
1144 ubc->isooutfree = done;
1145 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1147 /* only one URB still active - resubmit one */
1148 if (submit_iso_write_urb(next) <= 0) {
1149 /* couldn't submit */
1155 /* process queued SKBs */
1156 while ((skb = skb_dequeue(&bcs->squeue))) {
1157 /* copy to output buffer, doing L2 encapsulation */
1159 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1160 /* insufficient buffer space, push back onto queue */
1161 skb_queue_head(&bcs->squeue, skb);
1162 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1163 __func__, skb_queue_len(&bcs->squeue));
1167 gigaset_skb_sent(bcs, skb);
1168 dev_kfree_skb_any(skb);
1172 /* Isochronous Read - Bottom Half */
1173 /* ============================== */
1176 * tasklet scheduled when an isochronous input URB from the Gigaset device
1179 * data B channel state structure
1181 static void read_iso_tasklet(unsigned long data)
1183 struct bc_state *bcs = (struct bc_state *) data;
1184 struct bas_bc_state *ubc = bcs->hw.bas;
1185 struct cardstate *cs = bcs->cs;
1188 unsigned long flags;
1189 int totleft, numbytes, offset, frame, rc;
1191 /* loop while more completed URBs arrive in the meantime */
1193 if (unlikely(!atomic_read(&cs->connected))) {
1194 warn("%s: disconnected", __func__);
1199 spin_lock_irqsave(&ubc->isoinlock, flags);
1200 if (!(urb = ubc->isoindone)) {
1201 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1204 ubc->isoindone = NULL;
1205 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1207 "isochronous read overrun, "
1208 "dropped URB with status: %s, %d bytes lost\n",
1209 get_usb_statmsg(ubc->loststatus),
1211 ubc->loststatus = -EINPROGRESS;
1213 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1215 if (unlikely(!(atomic_read(&ubc->running)))) {
1217 "%s: channel not running, "
1218 "dropped URB with status: %s",
1219 __func__, get_usb_statmsg(urb->status));
1223 switch (urb->status) {
1224 case 0: /* normal completion */
1226 case -EXDEV: /* inspect individual frames
1227 (we do that anyway) */
1228 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1233 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1234 continue; /* -> skip */
1235 case -EINPROGRESS: /* huh? */
1236 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1237 continue; /* -> skip */
1239 dev_err(cs->dev, "isochronous read stalled\n");
1241 continue; /* -> skip */
1242 default: /* severe trouble */
1243 dev_warn(cs->dev, "isochronous read: %s\n",
1244 get_usb_statmsg(urb->status));
1248 rcvbuf = urb->transfer_buffer;
1249 totleft = urb->actual_length;
1250 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1251 if (unlikely(urb->iso_frame_desc[frame].status)) {
1253 "isochronous read: frame %d: %s\n",
1256 urb->iso_frame_desc[frame].status));
1259 numbytes = urb->iso_frame_desc[frame].actual_length;
1260 if (unlikely(numbytes > BAS_MAXFRAME)) {
1262 "isochronous read: frame %d: "
1263 "numbytes (%d) > BAS_MAXFRAME\n",
1267 if (unlikely(numbytes > totleft)) {
1269 "isochronous read: frame %d: "
1270 "numbytes (%d) > totleft (%d)\n",
1271 frame, numbytes, totleft);
1274 offset = urb->iso_frame_desc[frame].offset;
1275 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1277 "isochronous read: frame %d: "
1278 "offset (%d) + numbytes (%d) "
1279 "> BAS_INBUFSIZE\n",
1280 frame, offset, numbytes);
1283 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1284 totleft -= numbytes;
1286 if (unlikely(totleft > 0))
1288 "isochronous read: %d data bytes missing\n",
1292 /* URB processed, resubmit */
1293 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1294 urb->iso_frame_desc[frame].status = 0;
1295 urb->iso_frame_desc[frame].actual_length = 0;
1297 /* urb->dev is clobbered by USB subsystem */
1298 urb->dev = bcs->cs->hw.bas->udev;
1299 urb->transfer_flags = URB_ISO_ASAP;
1300 urb->number_of_packets = BAS_NUMFRAMES;
1301 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1303 "could not resubmit isochronous read URB: %s\n",
1304 get_usb_statmsg(rc));
1305 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1311 /* Channel Operations */
1312 /* ================== */
1315 * timeout routine for control output request
1317 * B channel control structure
1319 static void req_timeout(unsigned long data)
1321 struct bc_state *bcs = (struct bc_state *) data;
1322 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1324 unsigned long flags;
1328 spin_lock_irqsave(&ucs->lock, flags);
1329 pending = ucs->pending;
1331 spin_unlock_irqrestore(&ucs->lock, flags);
1334 case 0: /* no pending request */
1335 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1338 case HD_OPEN_ATCHANNEL:
1339 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1340 error_reset(bcs->cs);
1343 case HD_OPEN_B2CHANNEL:
1344 case HD_OPEN_B1CHANNEL:
1345 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1350 case HD_CLOSE_ATCHANNEL:
1351 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1354 case HD_CLOSE_B2CHANNEL:
1355 case HD_CLOSE_B1CHANNEL:
1356 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1361 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1366 /* write_ctrl_callback
1367 * USB completion handler for control pipe output
1368 * called by the USB subsystem in interrupt context
1370 * urb USB request block of completed request
1371 * urb->context = hardware specific controller state structure
1373 static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1375 struct bas_cardstate *ucs = urb->context;
1376 unsigned long flags;
1378 spin_lock_irqsave(&ucs->lock, flags);
1379 if (urb->status && ucs->pending) {
1380 dev_err(&ucs->interface->dev,
1381 "control request 0x%02x failed: %s\n",
1382 ucs->pending, get_usb_statmsg(urb->status));
1383 del_timer(&ucs->timer_ctrl);
1386 /* individual handling of specific request types */
1387 switch (ucs->pending) {
1388 case HD_DEVICE_INIT_ACK: /* no reply expected */
1392 spin_unlock_irqrestore(&ucs->lock, flags);
1396 * submit a control output request without message buffer to the Gigaset base
1397 * and optionally start a timeout
1399 * bcs B channel control structure
1400 * req control request code (HD_*)
1401 * val control request parameter value (set to 0 if unused)
1402 * timeout timeout in seconds (0: no timeout)
1405 * -EINVAL if a NULL pointer is encountered somewhere
1406 * -EBUSY if another request is pending
1407 * any URB submission error code
1409 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1411 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1413 unsigned long flags;
1415 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1417 spin_lock_irqsave(&ucs->lock, flags);
1419 spin_unlock_irqrestore(&ucs->lock, flags);
1420 dev_err(bcs->cs->dev,
1421 "submission of request 0x%02x failed: "
1422 "request 0x%02x still pending\n",
1426 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1427 spin_unlock_irqrestore(&ucs->lock, flags);
1428 dev_err(bcs->cs->dev,
1429 "could not submit request 0x%02x: URB busy\n", req);
1433 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1434 ucs->dr_ctrl.bRequest = req;
1435 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1436 ucs->dr_ctrl.wIndex = 0;
1437 ucs->dr_ctrl.wLength = 0;
1438 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1439 usb_sndctrlpipe(ucs->udev, 0),
1440 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1441 write_ctrl_callback, ucs);
1442 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1443 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1444 req, get_usb_statmsg(ret));
1445 spin_unlock_irqrestore(&ucs->lock, flags);
1451 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1452 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1453 ucs->timer_ctrl.data = (unsigned long) bcs;
1454 ucs->timer_ctrl.function = req_timeout;
1455 add_timer(&ucs->timer_ctrl);
1458 spin_unlock_irqrestore(&ucs->lock, flags);
1462 /* gigaset_init_bchannel
1463 * called by common.c to connect a B channel
1464 * initialize isochronous I/O and tell the Gigaset base to open the channel
1466 * B channel control structure
1468 * 0 on success, error code < 0 on error
1470 static int gigaset_init_bchannel(struct bc_state *bcs)
1474 if ((ret = starturbs(bcs)) < 0) {
1475 dev_err(bcs->cs->dev,
1476 "could not start isochronous I/O for channel %d\n",
1482 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1483 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1484 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1485 bcs->channel + 1, get_usb_statmsg(ret));
1486 stopurbs(bcs->hw.bas);
1492 /* gigaset_close_bchannel
1493 * called by common.c to disconnect a B channel
1494 * tell the Gigaset base to close the channel
1495 * stopping isochronous I/O and LL notification will be done when the
1496 * acknowledgement for the close arrives
1498 * B channel control structure
1500 * 0 on success, error code < 0 on error
1502 static int gigaset_close_bchannel(struct bc_state *bcs)
1506 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1507 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1508 /* channel not running: just signal common.c */
1509 gigaset_bchannel_down(bcs);
1513 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1514 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1515 dev_err(bcs->cs->dev,
1516 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1517 get_usb_statmsg(ret));
1521 /* Device Operations */
1522 /* ================= */
1525 * unqueue first command buffer from queue, waking any sleepers
1526 * must be called with cs->cmdlock held
1528 * cs controller state structure
1530 static void complete_cb(struct cardstate *cs)
1532 struct cmdbuf_t *cb = cs->cmdbuf;
1534 /* unqueue completed buffer */
1535 cs->cmdbytes -= cs->curlen;
1536 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1537 "write_command: sent %u bytes, %u left",
1538 cs->curlen, cs->cmdbytes);
1539 if ((cs->cmdbuf = cb->next) != NULL) {
1540 cs->cmdbuf->prev = NULL;
1541 cs->curlen = cs->cmdbuf->len;
1543 cs->lastcmdbuf = NULL;
1547 if (cb->wake_tasklet)
1548 tasklet_schedule(cb->wake_tasklet);
1553 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1555 /* write_command_callback
1556 * USB completion handler for AT command transmission
1557 * called by the USB subsystem in interrupt context
1559 * urb USB request block of completed request
1560 * urb->context = controller state structure
1562 static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1564 struct cardstate *cs = urb->context;
1565 struct bas_cardstate *ucs = cs->hw.bas;
1566 unsigned long flags;
1569 switch (urb->status) {
1570 case 0: /* normal completion */
1572 case -ENOENT: /* canceled */
1573 case -ECONNRESET: /* canceled (async) */
1574 case -EINPROGRESS: /* pending */
1575 /* ignore silently */
1576 gig_dbg(DEBUG_USBREQ, "%s: %s",
1577 __func__, get_usb_statmsg(urb->status));
1579 default: /* any failure */
1580 if (++ucs->retry_cmd_out > BAS_RETRY) {
1582 "command write: %s, "
1583 "giving up after %d retries\n",
1584 get_usb_statmsg(urb->status),
1585 ucs->retry_cmd_out);
1588 if (cs->cmdbuf == NULL) {
1590 "command write: %s, "
1591 "cannot retry - cmdbuf gone\n",
1592 get_usb_statmsg(urb->status));
1595 dev_notice(cs->dev, "command write: %s, retry %d\n",
1596 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1597 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1598 /* resubmitted - bypass regular exit block */
1600 /* command send failed, assume base still waiting */
1601 update_basstate(ucs, BS_ATREADY, 0);
1604 spin_lock_irqsave(&cs->cmdlock, flags);
1605 if (cs->cmdbuf != NULL)
1607 spin_unlock_irqrestore(&cs->cmdlock, flags);
1611 * timeout routine for AT command transmission
1613 * controller state structure
1615 static void atrdy_timeout(unsigned long data)
1617 struct cardstate *cs = (struct cardstate *) data;
1618 struct bas_cardstate *ucs = cs->hw.bas;
1620 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1622 /* fake the missing signal - what else can I do? */
1623 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1628 * submit an HD_WRITE_ATMESSAGE command URB
1630 * cs controller state structure
1631 * buf buffer containing command to send
1632 * len length of command to send
1635 * -EFAULT if a NULL pointer is encountered somewhere
1636 * -EBUSY if another request is pending
1637 * any URB submission error code
1639 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1641 struct bas_cardstate *ucs = cs->hw.bas;
1644 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1646 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
1648 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1652 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1653 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1654 ucs->dr_cmd_out.wValue = 0;
1655 ucs->dr_cmd_out.wIndex = 0;
1656 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1657 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1658 usb_sndctrlpipe(ucs->udev, 0),
1659 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1660 write_command_callback, cs);
1662 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
1663 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1664 get_usb_statmsg(ret));
1668 /* submitted successfully */
1669 update_basstate(ucs, 0, BS_ATREADY);
1671 /* start timeout if necessary */
1672 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
1673 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1675 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1676 ucs->timer_atrdy.data = (unsigned long) cs;
1677 ucs->timer_atrdy.function = atrdy_timeout;
1678 add_timer(&ucs->timer_atrdy);
1679 update_basstate(ucs, BS_ATTIMER, 0);
1685 * start transmission of AT command queue if necessary
1687 * cs controller state structure
1690 * error code < 0 on error
1692 static int start_cbsend(struct cardstate *cs)
1694 struct cmdbuf_t *cb;
1695 struct bas_cardstate *ucs = cs->hw.bas;
1696 unsigned long flags;
1700 /* check if AT channel is open */
1701 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1702 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1703 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1705 dev_err(cs->dev, "could not open AT channel\n");
1706 /* flush command queue */
1707 spin_lock_irqsave(&cs->cmdlock, flags);
1708 while (cs->cmdbuf != NULL)
1710 spin_unlock_irqrestore(&cs->cmdlock, flags);
1715 /* try to send first command in queue */
1716 spin_lock_irqsave(&cs->cmdlock, flags);
1718 while ((cb = cs->cmdbuf) != NULL &&
1719 atomic_read(&ucs->basstate) & BS_ATREADY) {
1720 ucs->retry_cmd_out = 0;
1721 rc = atwrite_submit(cs, cb->buf, cb->len);
1728 spin_unlock_irqrestore(&cs->cmdlock, flags);
1732 /* gigaset_write_cmd
1733 * This function is called by the device independent part of the driver
1734 * to transmit an AT command string to the Gigaset device.
1735 * It encapsulates the device specific method for transmission over the
1736 * direct USB connection to the base.
1737 * The command string is added to the queue of commands to send, and
1738 * USB transmission is started if necessary.
1740 * cs controller state structure
1741 * buf command string to send
1742 * len number of bytes to send (max. IF_WRITEBUF)
1743 * wake_tasklet tasklet to run when transmission is completed
1746 * number of bytes queued on success
1747 * error code < 0 on error
1749 static int gigaset_write_cmd(struct cardstate *cs,
1750 const unsigned char *buf, int len,
1751 struct tasklet_struct *wake_tasklet)
1753 struct cmdbuf_t *cb;
1754 unsigned long flags;
1757 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1758 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1759 "CMD Transmit", len, buf);
1761 if (unlikely(!atomic_read(&cs->connected))) {
1762 err("%s: disconnected", __func__);
1767 return 0; /* nothing to do */
1769 if (len > IF_WRITEBUF)
1771 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1772 dev_err(cs->dev, "%s: out of memory\n", __func__);
1776 memcpy(cb->buf, buf, len);
1780 cb->wake_tasklet = wake_tasklet;
1782 spin_lock_irqsave(&cs->cmdlock, flags);
1783 cb->prev = cs->lastcmdbuf;
1785 cs->lastcmdbuf->next = cb;
1790 cs->cmdbytes += len;
1791 cs->lastcmdbuf = cb;
1792 spin_unlock_irqrestore(&cs->cmdlock, flags);
1794 status = start_cbsend(cs);
1796 return status < 0 ? status : len;
1799 /* gigaset_write_room
1800 * tty_driver.write_room interface routine
1801 * return number of characters the driver will accept to be written via
1804 * controller state structure
1806 * number of characters
1808 static int gigaset_write_room(struct cardstate *cs)
1813 /* gigaset_chars_in_buffer
1814 * tty_driver.chars_in_buffer interface routine
1815 * return number of characters waiting to be sent
1817 * controller state structure
1819 * number of characters
1821 static int gigaset_chars_in_buffer(struct cardstate *cs)
1823 unsigned long flags;
1826 spin_lock_irqsave(&cs->cmdlock, flags);
1827 bytes = cs->cmdbytes;
1828 spin_unlock_irqrestore(&cs->cmdlock, flags);
1834 * implementation of ioctl(GIGASET_BRKCHARS)
1836 * controller state structure
1838 * -EINVAL (unimplemented function)
1840 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1846 /* Device Initialization/Shutdown */
1847 /* ============================== */
1849 /* Free hardware dependent part of the B channel structure
1851 * bcs B channel structure
1855 static int gigaset_freebcshw(struct bc_state *bcs)
1860 if (bcs->hw.bas->isooutbuf)
1861 kfree(bcs->hw.bas->isooutbuf);
1867 /* Initialize hardware dependent part of the B channel structure
1869 * bcs B channel structure
1873 static int gigaset_initbcshw(struct bc_state *bcs)
1876 struct bas_bc_state *ubc;
1878 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1880 err("could not allocate bas_bc_state");
1884 atomic_set(&ubc->running, 0);
1885 atomic_set(&ubc->corrbytes, 0);
1886 spin_lock_init(&ubc->isooutlock);
1887 for (i = 0; i < BAS_OUTURBS; ++i) {
1888 ubc->isoouturbs[i].urb = NULL;
1889 ubc->isoouturbs[i].bcs = bcs;
1891 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1893 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1894 err("could not allocate isochronous output buffer");
1899 tasklet_init(&ubc->sent_tasklet,
1900 &write_iso_tasklet, (unsigned long) bcs);
1902 spin_lock_init(&ubc->isoinlock);
1903 for (i = 0; i < BAS_INURBS; ++i)
1904 ubc->isoinurbs[i] = NULL;
1905 ubc->isoindone = NULL;
1906 ubc->loststatus = -EINPROGRESS;
1920 tasklet_init(&ubc->rcvd_tasklet,
1921 &read_iso_tasklet, (unsigned long) bcs);
1925 static void gigaset_reinitbcshw(struct bc_state *bcs)
1927 struct bas_bc_state *ubc = bcs->hw.bas;
1929 atomic_set(&bcs->hw.bas->running, 0);
1930 atomic_set(&bcs->hw.bas->corrbytes, 0);
1931 bcs->hw.bas->numsub = 0;
1932 spin_lock_init(&ubc->isooutlock);
1933 spin_lock_init(&ubc->isoinlock);
1934 ubc->loststatus = -EINPROGRESS;
1937 static void gigaset_freecshw(struct cardstate *cs)
1939 struct bas_cardstate *ucs = cs->hw.bas;
1941 del_timer(&ucs->timer_ctrl);
1942 del_timer(&ucs->timer_atrdy);
1943 del_timer(&ucs->timer_cmd_in);
1948 static int gigaset_initcshw(struct cardstate *cs)
1950 struct bas_cardstate *ucs;
1952 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
1956 ucs->urb_cmd_in = NULL;
1957 ucs->urb_cmd_out = NULL;
1959 ucs->rcvbuf_size = 0;
1961 spin_lock_init(&ucs->lock);
1964 atomic_set(&ucs->basstate, 0);
1965 init_timer(&ucs->timer_ctrl);
1966 init_timer(&ucs->timer_atrdy);
1967 init_timer(&ucs->timer_cmd_in);
1973 * unlink and deallocate all URBs unconditionally
1974 * caller must make sure that no commands are still in progress
1976 * cs controller state structure
1978 static void freeurbs(struct cardstate *cs)
1980 struct bas_cardstate *ucs = cs->hw.bas;
1981 struct bas_bc_state *ubc;
1984 for (j = 0; j < 2; ++j) {
1985 ubc = cs->bcs[j].hw.bas;
1986 for (i = 0; i < BAS_OUTURBS; ++i)
1987 if (ubc->isoouturbs[i].urb) {
1988 usb_kill_urb(ubc->isoouturbs[i].urb);
1990 "%s: isoc output URB %d/%d unlinked",
1992 usb_free_urb(ubc->isoouturbs[i].urb);
1993 ubc->isoouturbs[i].urb = NULL;
1995 for (i = 0; i < BAS_INURBS; ++i)
1996 if (ubc->isoinurbs[i]) {
1997 usb_kill_urb(ubc->isoinurbs[i]);
1999 "%s: isoc input URB %d/%d unlinked",
2001 usb_free_urb(ubc->isoinurbs[i]);
2002 ubc->isoinurbs[i] = NULL;
2005 if (ucs->urb_int_in) {
2006 usb_kill_urb(ucs->urb_int_in);
2007 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2009 usb_free_urb(ucs->urb_int_in);
2010 ucs->urb_int_in = NULL;
2012 if (ucs->urb_cmd_out) {
2013 usb_kill_urb(ucs->urb_cmd_out);
2014 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2016 usb_free_urb(ucs->urb_cmd_out);
2017 ucs->urb_cmd_out = NULL;
2019 if (ucs->urb_cmd_in) {
2020 usb_kill_urb(ucs->urb_cmd_in);
2021 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2023 usb_free_urb(ucs->urb_cmd_in);
2024 ucs->urb_cmd_in = NULL;
2026 if (ucs->urb_ctrl) {
2027 usb_kill_urb(ucs->urb_ctrl);
2028 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2030 usb_free_urb(ucs->urb_ctrl);
2031 ucs->urb_ctrl = NULL;
2036 * This function is called when a new USB device is connected.
2037 * It checks whether the new device is handled by this driver.
2039 static int gigaset_probe(struct usb_interface *interface,
2040 const struct usb_device_id *id)
2042 struct usb_host_interface *hostif;
2043 struct usb_device *udev = interface_to_usbdev(interface);
2044 struct cardstate *cs = NULL;
2045 struct bas_cardstate *ucs = NULL;
2046 struct bas_bc_state *ubc;
2047 struct usb_endpoint_descriptor *endpoint;
2052 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2053 __func__, le16_to_cpu(udev->descriptor.idVendor),
2054 le16_to_cpu(udev->descriptor.idProduct));
2056 /* See if the device offered us matches what we can accept */
2057 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2058 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2059 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2060 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2061 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
2062 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
2066 /* set required alternate setting */
2067 hostif = interface->cur_altsetting;
2068 if (hostif->desc.bAlternateSetting != 3) {
2070 "%s: wrong alternate setting %d - trying to switch",
2071 __func__, hostif->desc.bAlternateSetting);
2072 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2073 dev_warn(&udev->dev, "usb_set_interface failed, "
2074 "device %d interface %d altsetting %d\n",
2075 udev->devnum, hostif->desc.bInterfaceNumber,
2076 hostif->desc.bAlternateSetting);
2079 hostif = interface->cur_altsetting;
2082 /* Reject application specific interfaces
2084 if (hostif->desc.bInterfaceClass != 255) {
2085 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2086 __func__, hostif->desc.bInterfaceClass);
2090 dev_info(&udev->dev,
2091 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2092 __func__, le16_to_cpu(udev->descriptor.idVendor),
2093 le16_to_cpu(udev->descriptor.idProduct));
2095 cs = gigaset_getunassignedcs(driver);
2097 dev_err(&udev->dev, "no free cardstate\n");
2102 /* save off device structure ptrs for later use */
2105 ucs->interface = interface;
2106 cs->dev = &interface->dev;
2109 * - one for the interrupt pipe
2110 * - three for the different uses of the default control pipe
2111 * - three for each isochronous pipe
2113 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2114 if (!ucs->urb_int_in) {
2115 dev_err(cs->dev, "no free urbs available\n");
2118 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2119 if (!ucs->urb_cmd_in) {
2120 dev_err(cs->dev, "no free urbs available\n");
2123 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2124 if (!ucs->urb_cmd_out) {
2125 dev_err(cs->dev, "no free urbs available\n");
2128 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2129 if (!ucs->urb_ctrl) {
2130 dev_err(cs->dev, "no free urbs available\n");
2134 for (j = 0; j < 2; ++j) {
2135 ubc = cs->bcs[j].hw.bas;
2136 for (i = 0; i < BAS_OUTURBS; ++i) {
2137 ubc->isoouturbs[i].urb =
2138 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2139 if (!ubc->isoouturbs[i].urb) {
2140 dev_err(cs->dev, "no free urbs available\n");
2144 for (i = 0; i < BAS_INURBS; ++i) {
2146 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2147 if (!ubc->isoinurbs[i]) {
2148 dev_err(cs->dev, "no free urbs available\n");
2155 ucs->rcvbuf_size = 0;
2157 /* Fill the interrupt urb and send it to the core */
2158 endpoint = &hostif->endpoint[0].desc;
2159 usb_fill_int_urb(ucs->urb_int_in, udev,
2160 usb_rcvintpipe(udev,
2161 (endpoint->bEndpointAddress) & 0x0f),
2162 ucs->int_in_buf, 3, read_int_callback, cs,
2163 endpoint->bInterval);
2164 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2166 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2167 get_usb_statmsg(ret));
2171 /* tell the device that the driver is ready */
2172 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2175 /* tell common part that the device is ready */
2176 if (startmode == SM_LOCKED)
2177 atomic_set(&cs->mstate, MS_LOCKED);
2179 /* save address of controller structure */
2180 usb_set_intfdata(interface, cs);
2182 if (!gigaset_start(cs))
2189 gigaset_unassign(cs);
2193 /* gigaset_disconnect
2194 * This function is called when the Gigaset base is unplugged.
2196 static void gigaset_disconnect(struct usb_interface *interface)
2198 struct cardstate *cs;
2199 struct bas_cardstate *ucs;
2201 cs = usb_get_intfdata(interface);
2205 dev_info(cs->dev, "disconnecting Gigaset base\n");
2208 usb_set_intfdata(interface, NULL);
2211 ucs->rcvbuf_size = 0;
2212 atomic_set(&ucs->basstate, 0);
2213 usb_put_dev(ucs->udev);
2214 ucs->interface = NULL;
2217 gigaset_unassign(cs);
2220 static struct gigaset_ops gigops = {
2223 gigaset_chars_in_buffer,
2225 gigaset_init_bchannel,
2226 gigaset_close_bchannel,
2229 gigaset_reinitbcshw,
2232 gigaset_set_modem_ctrl,
2234 gigaset_set_line_ctrl,
2235 gigaset_isoc_send_skb,
2240 * This function is called after the kernel module is loaded.
2242 static int __init bas_gigaset_init(void)
2246 /* allocate memory for our driver state and intialize it */
2247 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2248 GIGASET_MODULENAME, GIGASET_DEVNAME,
2249 GIGASET_DEVFSNAME, &gigops,
2250 THIS_MODULE)) == NULL)
2253 /* allocate memory for our device state and intialize it */
2254 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2255 GIGASET_MODULENAME);
2259 /* register this driver with the USB subsystem */
2260 result = usb_register(&gigaset_usb_driver);
2262 err("usb_register failed (error %d)", -result);
2266 info(DRIVER_AUTHOR);
2270 error: if (cardstate)
2271 gigaset_freecs(cardstate);
2274 gigaset_freedriver(driver);
2280 * This function is called before the kernel module is unloaded.
2282 static void __exit bas_gigaset_exit(void)
2284 gigaset_blockdriver(driver); /* => probe will fail
2285 * => no gigaset_start any more
2288 gigaset_shutdown(cardstate);
2289 /* from now on, no isdn callback should be possible */
2291 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
2292 gig_dbg(DEBUG_ANY, "closing AT channel");
2293 if (req_submit(cardstate->bcs,
2294 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2295 /* successfully submitted */
2296 //FIXME wait for completion?
2300 /* deregister this driver with the USB subsystem */
2301 usb_deregister(&gigaset_usb_driver);
2302 /* this will call the disconnect-callback */
2303 /* from now on, no disconnect/probe callback should be running */
2305 gigaset_freecs(cardstate);
2307 gigaset_freedriver(driver);
2312 module_init(bas_gigaset_init);
2313 module_exit(bas_gigaset_exit);
2315 MODULE_AUTHOR(DRIVER_AUTHOR);
2316 MODULE_DESCRIPTION(DRIVER_DESC);
2317 MODULE_LICENSE("GPL");