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(!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 switch (urb->status) {
479 case 0: /* success */
481 case -ENOENT: /* canceled */
482 case -ECONNRESET: /* canceled (async) */
483 case -EINPROGRESS: /* pending */
484 /* ignore silently */
485 gig_dbg(DEBUG_USBREQ, "%s: %s",
486 __func__, get_usb_statmsg(urb->status));
488 default: /* severe trouble */
489 dev_warn(cs->dev, "interrupt read: %s\n",
490 get_usb_statmsg(urb->status));
491 //FIXME corrective action? resubmission always ok?
495 l = (unsigned) ucs->int_in_buf[1] +
496 (((unsigned) ucs->int_in_buf[2]) << 8);
498 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
499 urb->actual_length, (int)ucs->int_in_buf[0], l,
500 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
504 switch (ucs->int_in_buf[0]) {
505 case HD_DEVICE_INIT_OK:
506 update_basstate(ucs, BS_INIT, 0);
509 case HD_READY_SEND_ATDATA:
510 del_timer(&ucs->timer_atrdy);
511 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
515 case HD_OPEN_B2CHANNEL_ACK:
517 case HD_OPEN_B1CHANNEL_ACK:
518 bcs = cs->bcs + channel;
519 update_basstate(ucs, BS_B1OPEN << channel, 0);
520 gigaset_bchannel_up(bcs);
523 case HD_OPEN_ATCHANNEL_ACK:
524 update_basstate(ucs, BS_ATOPEN, 0);
528 case HD_CLOSE_B2CHANNEL_ACK:
530 case HD_CLOSE_B1CHANNEL_ACK:
531 bcs = cs->bcs + channel;
532 update_basstate(ucs, 0, BS_B1OPEN << channel);
533 stopurbs(bcs->hw.bas);
534 gigaset_bchannel_down(bcs);
537 case HD_CLOSE_ATCHANNEL_ACK:
538 update_basstate(ucs, 0, BS_ATOPEN);
541 case HD_B2_FLOW_CONTROL:
543 case HD_B1_FLOW_CONTROL:
544 bcs = cs->bcs + channel;
545 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
546 &bcs->hw.bas->corrbytes);
548 "Flow control (channel %d, sub %d): 0x%02x => %d",
549 channel, bcs->hw.bas->numsub, l,
550 atomic_read(&bcs->hw.bas->corrbytes));
553 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
556 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
559 spin_lock_irqsave(&cs->lock, flags);
560 if (ucs->rcvbuf_size) {
561 spin_unlock_irqrestore(&cs->lock, flags);
563 "receive AT data overrun, %d bytes lost\n", l);
564 error_reset(cs); //FIXME reschedule
567 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
568 spin_unlock_irqrestore(&cs->lock, flags);
569 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
570 error_reset(cs); //FIXME reschedule
573 ucs->rcvbuf_size = l;
574 ucs->retry_cmd_in = 0;
575 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
578 ucs->rcvbuf_size = 0;
579 error_reset(cs); //FIXME reschedule
581 spin_unlock_irqrestore(&cs->lock, flags);
584 case HD_RESET_INTERRUPT_PIPE_ACK:
585 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
589 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
594 "unknown Gigaset signal 0x%02x (%u) ignored\n",
595 (int) ucs->int_in_buf[0], l);
601 spin_lock_irqsave(&cs->lock, flags);
602 status = cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
603 spin_unlock_irqrestore(&cs->lock, flags);
604 if (unlikely(status)) {
605 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
606 get_usb_statmsg(status));
611 /* read_ctrl_callback
612 * USB completion handler for control pipe input
613 * called by the USB subsystem in interrupt context
615 * urb USB request block
616 * urb->context = inbuf structure for controller state
618 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
620 struct inbuf_t *inbuf = urb->context;
621 struct cardstate *cs = inbuf->cs;
622 struct bas_cardstate *ucs = cs->hw.bas;
627 spin_lock_irqsave(&cs->lock, flags);
628 if (unlikely(!cs->connected)) {
629 warn("%s: disconnected", __func__);
630 spin_unlock_irqrestore(&cs->lock, flags);
634 if (!ucs->rcvbuf_size) {
635 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
636 spin_unlock_irqrestore(&cs->lock, flags);
640 del_timer(&ucs->timer_cmd_in);
642 switch (urb->status) {
643 case 0: /* normal completion */
644 numbytes = urb->actual_length;
645 if (unlikely(numbytes == 0)) {
647 "control read: empty block received\n");
650 if (unlikely(numbytes != ucs->rcvbuf_size)) {
652 "control read: received %d chars, expected %d\n",
653 numbytes, ucs->rcvbuf_size);
654 if (numbytes > ucs->rcvbuf_size)
655 numbytes = ucs->rcvbuf_size;
658 /* copy received bytes to inbuf */
659 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
661 if (unlikely(numbytes < ucs->rcvbuf_size)) {
662 /* incomplete - resubmit for remaining bytes */
663 ucs->rcvbuf_size -= numbytes;
664 ucs->retry_cmd_in = 0;
669 case -ENOENT: /* canceled */
670 case -ECONNRESET: /* canceled (async) */
671 case -EINPROGRESS: /* pending */
672 /* no action necessary */
673 gig_dbg(DEBUG_USBREQ, "%s: %s",
674 __func__, get_usb_statmsg(urb->status));
677 default: /* severe trouble */
678 dev_warn(cs->dev, "control read: %s\n",
679 get_usb_statmsg(urb->status));
681 if (ucs->retry_cmd_in++ < BAS_RETRY) {
682 dev_notice(cs->dev, "control read: retry %d\n",
684 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
685 /* resubmitted - bypass regular exit block */
686 spin_unlock_irqrestore(&cs->lock, flags);
691 "control read: giving up after %d tries\n",
699 ucs->rcvbuf_size = 0;
700 spin_unlock_irqrestore(&cs->lock, flags);
702 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
703 gigaset_schedule_event(cs);
708 * USB completion handler for B channel isochronous input
709 * called by the USB subsystem in interrupt context
711 * urb USB request block of completed request
712 * urb->context = bc_state structure
714 static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
716 struct bc_state *bcs;
717 struct bas_bc_state *ubc;
721 /* status codes not worth bothering the tasklet with */
722 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
723 urb->status == -EINPROGRESS)) {
724 gig_dbg(DEBUG_ISO, "%s: %s",
725 __func__, get_usb_statmsg(urb->status));
732 spin_lock_irqsave(&ubc->isoinlock, flags);
733 if (likely(ubc->isoindone == NULL)) {
734 /* pass URB to tasklet */
735 ubc->isoindone = urb;
736 tasklet_schedule(&ubc->rcvd_tasklet);
738 /* tasklet still busy, drop data and resubmit URB */
739 ubc->loststatus = urb->status;
740 for (i = 0; i < BAS_NUMFRAMES; i++) {
741 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
742 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
743 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
744 ubc->loststatus = urb->iso_frame_desc[i].status;
746 urb->iso_frame_desc[i].status = 0;
747 urb->iso_frame_desc[i].actual_length = 0;
749 if (likely(atomic_read(&ubc->running))) {
750 /* urb->dev is clobbered by USB subsystem */
751 urb->dev = bcs->cs->hw.bas->udev;
752 urb->transfer_flags = URB_ISO_ASAP;
753 urb->number_of_packets = BAS_NUMFRAMES;
754 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
756 rc = usb_submit_urb(urb, SLAB_ATOMIC);
757 if (unlikely(rc != 0)) {
758 dev_err(bcs->cs->dev,
759 "could not resubmit isochronous read "
760 "URB: %s\n", get_usb_statmsg(rc));
761 dump_urb(DEBUG_ISO, "isoc read", urb);
766 spin_unlock_irqrestore(&ubc->isoinlock, flags);
769 /* write_iso_callback
770 * USB completion handler for B channel isochronous output
771 * called by the USB subsystem in interrupt context
773 * urb USB request block of completed request
774 * urb->context = isow_urbctx_t structure
776 static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
778 struct isow_urbctx_t *ucx;
779 struct bas_bc_state *ubc;
782 /* status codes not worth bothering the tasklet with */
783 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
784 urb->status == -EINPROGRESS)) {
785 gig_dbg(DEBUG_ISO, "%s: %s",
786 __func__, get_usb_statmsg(urb->status));
790 /* pass URB context to tasklet */
792 ubc = ucx->bcs->hw.bas;
794 spin_lock_irqsave(&ubc->isooutlock, flags);
795 ubc->isooutovfl = ubc->isooutdone;
796 ubc->isooutdone = ucx;
797 spin_unlock_irqrestore(&ubc->isooutlock, flags);
798 tasklet_schedule(&ubc->sent_tasklet);
802 * prepare and submit USB request blocks for isochronous input and output
804 * B channel control structure
807 * < 0 on error (no URBs submitted)
809 static int starturbs(struct bc_state *bcs)
811 struct bas_bc_state *ubc = bcs->hw.bas;
816 /* initialize L2 reception */
817 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
818 bcs->inputstate |= INS_flag_hunt;
820 /* submit all isochronous input URBs */
821 atomic_set(&ubc->running, 1);
822 for (k = 0; k < BAS_INURBS; k++) {
823 urb = ubc->isoinurbs[k];
825 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
830 urb->dev = bcs->cs->hw.bas->udev;
831 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
832 urb->transfer_flags = URB_ISO_ASAP;
833 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
834 urb->transfer_buffer_length = BAS_INBUFSIZE;
835 urb->number_of_packets = BAS_NUMFRAMES;
836 urb->interval = BAS_FRAMETIME;
837 urb->complete = read_iso_callback;
839 for (j = 0; j < BAS_NUMFRAMES; j++) {
840 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
841 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
842 urb->iso_frame_desc[j].status = 0;
843 urb->iso_frame_desc[j].actual_length = 0;
846 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
847 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
848 dev_err(bcs->cs->dev,
849 "could not submit isochronous read URB %d: %s\n",
850 k, get_usb_statmsg(rc));
855 /* initialize L2 transmission */
856 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
858 /* set up isochronous output URBs for flag idling */
859 for (k = 0; k < BAS_OUTURBS; ++k) {
860 urb = ubc->isoouturbs[k].urb;
862 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
866 urb->dev = bcs->cs->hw.bas->udev;
867 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
868 urb->transfer_flags = URB_ISO_ASAP;
869 urb->transfer_buffer = ubc->isooutbuf->data;
870 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
871 urb->number_of_packets = BAS_NUMFRAMES;
872 urb->interval = BAS_FRAMETIME;
873 urb->complete = write_iso_callback;
874 urb->context = &ubc->isoouturbs[k];
875 for (j = 0; j < BAS_NUMFRAMES; ++j) {
876 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
877 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
878 urb->iso_frame_desc[j].status = 0;
879 urb->iso_frame_desc[j].actual_length = 0;
881 ubc->isoouturbs[k].limit = -1;
884 /* submit two URBs, keep third one */
885 for (k = 0; k < 2; ++k) {
886 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
887 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
889 dev_err(bcs->cs->dev,
890 "could not submit isochronous write URB %d: %s\n",
891 k, get_usb_statmsg(rc));
895 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
896 ubc->isooutfree = &ubc->isoouturbs[2];
897 ubc->isooutdone = ubc->isooutovfl = NULL;
905 * cancel the USB request blocks for isochronous input and output
906 * errors are silently ignored
908 * B channel control structure
910 static void stopurbs(struct bas_bc_state *ubc)
914 atomic_set(&ubc->running, 0);
916 for (k = 0; k < BAS_INURBS; ++k) {
917 rc = usb_unlink_urb(ubc->isoinurbs[k]);
919 "%s: isoc input URB %d unlinked, result = %d",
923 for (k = 0; k < BAS_OUTURBS; ++k) {
924 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
926 "%s: isoc output URB %d unlinked, result = %d",
931 /* Isochronous Write - Bottom Half */
932 /* =============================== */
934 /* submit_iso_write_urb
935 * fill and submit the next isochronous write URB
937 * bcs B channel state structure
939 * number of frames submitted in URB
940 * 0 if URB not submitted because no data available (isooutbuf busy)
941 * error code < 0 on error
943 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
945 struct urb *urb = ucx->urb;
946 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
947 struct usb_iso_packet_descriptor *ifd;
948 int corrbytes, nframe, rc;
951 /* urb->dev is clobbered by USB subsystem */
952 urb->dev = ucx->bcs->cs->hw.bas->udev;
953 urb->transfer_flags = URB_ISO_ASAP;
954 urb->transfer_buffer = ubc->isooutbuf->data;
955 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
957 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
958 ifd = &urb->iso_frame_desc[nframe];
960 /* compute frame length according to flow control */
961 ifd->length = BAS_NORMFRAME;
962 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
963 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
964 __func__, corrbytes);
965 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
966 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
967 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
968 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
969 ifd->length += corrbytes;
970 atomic_add(-corrbytes, &ubc->corrbytes);
973 /* retrieve block of data to send */
974 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
976 if (ifd->offset < 0) {
977 if (ifd->offset == -EBUSY) {
979 "%s: buffer busy at frame %d",
981 /* tasklet will be restarted from
982 gigaset_send_skb() */
984 dev_err(ucx->bcs->cs->dev,
985 "%s: buffer error %d at frame %d\n",
986 __func__, ifd->offset, nframe);
991 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
993 ifd->actual_length = 0;
995 if ((urb->number_of_packets = nframe) > 0) {
996 spin_lock_irqsave(&ucx->bcs->cs->lock, flags);
997 rc = ucx->bcs->cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
998 spin_unlock_irqrestore(&ucx->bcs->cs->lock, flags);
1001 dev_err(ucx->bcs->cs->dev,
1002 "could not submit isochronous write URB: %s\n",
1003 get_usb_statmsg(rc));
1004 dump_urb(DEBUG_ISO, "isoc write", urb);
1012 /* write_iso_tasklet
1013 * tasklet scheduled when an isochronous output URB from the Gigaset device
1016 * data B channel state structure
1018 static void write_iso_tasklet(unsigned long data)
1020 struct bc_state *bcs = (struct bc_state *) data;
1021 struct bas_bc_state *ubc = bcs->hw.bas;
1022 struct cardstate *cs = bcs->cs;
1023 struct isow_urbctx_t *done, *next, *ovfl;
1025 struct usb_iso_packet_descriptor *ifd;
1027 unsigned long flags;
1029 struct sk_buff *skb;
1032 /* loop while completed URBs arrive in time */
1034 if (unlikely(!(atomic_read(&ubc->running)))) {
1035 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1039 /* retrieve completed URBs */
1040 spin_lock_irqsave(&ubc->isooutlock, flags);
1041 done = ubc->isooutdone;
1042 ubc->isooutdone = NULL;
1043 ovfl = ubc->isooutovfl;
1044 ubc->isooutovfl = NULL;
1045 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1047 dev_err(cs->dev, "isochronous write buffer underrun\n");
1054 /* submit free URB if available */
1055 spin_lock_irqsave(&ubc->isooutlock, flags);
1056 next = ubc->isooutfree;
1057 ubc->isooutfree = NULL;
1058 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1060 if (submit_iso_write_urb(next) <= 0) {
1061 /* could not submit URB, put it back */
1062 spin_lock_irqsave(&ubc->isooutlock, flags);
1063 if (ubc->isooutfree == NULL) {
1064 ubc->isooutfree = next;
1067 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1069 /* couldn't put it back */
1071 "losing isochronous write URB\n");
1077 /* process completed URB */
1079 switch (urb->status) {
1080 case 0: /* normal completion */
1082 case -EXDEV: /* inspect individual frames */
1083 /* assumptions (for lack of documentation):
1084 * - actual_length bytes of the frame in error are
1086 * - all following frames are not sent at all
1088 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1090 offset = done->limit; /* just in case */
1091 for (i = 0; i < BAS_NUMFRAMES; i++) {
1092 ifd = &urb->iso_frame_desc[i];
1094 ifd->actual_length != ifd->length) {
1096 "isochronous write: frame %d: %s, "
1097 "only %d of %d bytes sent\n",
1098 i, get_usb_statmsg(ifd->status),
1099 ifd->actual_length, ifd->length);
1100 offset = (ifd->offset +
1106 #ifdef CONFIG_GIGASET_DEBUG
1107 /* check assumption on remaining frames */
1108 for (; i < BAS_NUMFRAMES; i++) {
1109 ifd = &urb->iso_frame_desc[i];
1110 if (ifd->status != -EINPROGRESS
1111 || ifd->actual_length != 0) {
1113 "isochronous write: frame %d: %s, "
1114 "%d of %d bytes sent\n",
1115 i, get_usb_statmsg(ifd->status),
1116 ifd->actual_length, ifd->length);
1117 offset = (ifd->offset +
1125 case -EPIPE: //FIXME is this the code for "underrun"?
1126 dev_err(cs->dev, "isochronous write stalled\n");
1129 default: /* severe trouble */
1130 dev_warn(cs->dev, "isochronous write: %s\n",
1131 get_usb_statmsg(urb->status));
1134 /* mark the write buffer area covered by this URB as free */
1135 if (done->limit >= 0)
1136 atomic_set(&ubc->isooutbuf->read, done->limit);
1138 /* mark URB as free */
1139 spin_lock_irqsave(&ubc->isooutlock, flags);
1140 next = ubc->isooutfree;
1141 ubc->isooutfree = done;
1142 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1144 /* only one URB still active - resubmit one */
1145 if (submit_iso_write_urb(next) <= 0) {
1146 /* couldn't submit */
1152 /* process queued SKBs */
1153 while ((skb = skb_dequeue(&bcs->squeue))) {
1154 /* copy to output buffer, doing L2 encapsulation */
1156 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1157 /* insufficient buffer space, push back onto queue */
1158 skb_queue_head(&bcs->squeue, skb);
1159 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1160 __func__, skb_queue_len(&bcs->squeue));
1164 gigaset_skb_sent(bcs, skb);
1165 dev_kfree_skb_any(skb);
1169 /* Isochronous Read - Bottom Half */
1170 /* ============================== */
1173 * tasklet scheduled when an isochronous input URB from the Gigaset device
1176 * data B channel state structure
1178 static void read_iso_tasklet(unsigned long data)
1180 struct bc_state *bcs = (struct bc_state *) data;
1181 struct bas_bc_state *ubc = bcs->hw.bas;
1182 struct cardstate *cs = bcs->cs;
1185 unsigned long flags;
1186 int totleft, numbytes, offset, frame, rc;
1188 /* loop while more completed URBs arrive in the meantime */
1191 spin_lock_irqsave(&ubc->isoinlock, flags);
1192 if (!(urb = ubc->isoindone)) {
1193 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1196 ubc->isoindone = NULL;
1197 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1199 "isochronous read overrun, "
1200 "dropped URB with status: %s, %d bytes lost\n",
1201 get_usb_statmsg(ubc->loststatus),
1203 ubc->loststatus = -EINPROGRESS;
1205 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1207 if (unlikely(!(atomic_read(&ubc->running)))) {
1209 "%s: channel not running, "
1210 "dropped URB with status: %s",
1211 __func__, get_usb_statmsg(urb->status));
1215 switch (urb->status) {
1216 case 0: /* normal completion */
1218 case -EXDEV: /* inspect individual frames
1219 (we do that anyway) */
1220 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1225 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1226 continue; /* -> skip */
1227 case -EINPROGRESS: /* huh? */
1228 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1229 continue; /* -> skip */
1231 dev_err(cs->dev, "isochronous read stalled\n");
1233 continue; /* -> skip */
1234 default: /* severe trouble */
1235 dev_warn(cs->dev, "isochronous read: %s\n",
1236 get_usb_statmsg(urb->status));
1240 rcvbuf = urb->transfer_buffer;
1241 totleft = urb->actual_length;
1242 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1243 if (unlikely(urb->iso_frame_desc[frame].status)) {
1245 "isochronous read: frame %d: %s\n",
1248 urb->iso_frame_desc[frame].status));
1251 numbytes = urb->iso_frame_desc[frame].actual_length;
1252 if (unlikely(numbytes > BAS_MAXFRAME)) {
1254 "isochronous read: frame %d: "
1255 "numbytes (%d) > BAS_MAXFRAME\n",
1259 if (unlikely(numbytes > totleft)) {
1261 "isochronous read: frame %d: "
1262 "numbytes (%d) > totleft (%d)\n",
1263 frame, numbytes, totleft);
1266 offset = urb->iso_frame_desc[frame].offset;
1267 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1269 "isochronous read: frame %d: "
1270 "offset (%d) + numbytes (%d) "
1271 "> BAS_INBUFSIZE\n",
1272 frame, offset, numbytes);
1275 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1276 totleft -= numbytes;
1278 if (unlikely(totleft > 0))
1280 "isochronous read: %d data bytes missing\n",
1284 /* URB processed, resubmit */
1285 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1286 urb->iso_frame_desc[frame].status = 0;
1287 urb->iso_frame_desc[frame].actual_length = 0;
1289 /* urb->dev is clobbered by USB subsystem */
1290 urb->dev = bcs->cs->hw.bas->udev;
1291 urb->transfer_flags = URB_ISO_ASAP;
1292 urb->number_of_packets = BAS_NUMFRAMES;
1293 spin_lock_irqsave(&cs->lock, flags);
1294 rc = cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
1295 spin_unlock_irqrestore(&cs->lock, flags);
1298 "could not resubmit isochronous read URB: %s\n",
1299 get_usb_statmsg(rc));
1300 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1306 /* Channel Operations */
1307 /* ================== */
1310 * timeout routine for control output request
1312 * B channel control structure
1314 static void req_timeout(unsigned long data)
1316 struct bc_state *bcs = (struct bc_state *) data;
1317 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1319 unsigned long flags;
1323 spin_lock_irqsave(&ucs->lock, flags);
1324 pending = ucs->pending;
1326 spin_unlock_irqrestore(&ucs->lock, flags);
1329 case 0: /* no pending request */
1330 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1333 case HD_OPEN_ATCHANNEL:
1334 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1335 error_reset(bcs->cs);
1338 case HD_OPEN_B2CHANNEL:
1339 case HD_OPEN_B1CHANNEL:
1340 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1345 case HD_CLOSE_ATCHANNEL:
1346 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1349 case HD_CLOSE_B2CHANNEL:
1350 case HD_CLOSE_B1CHANNEL:
1351 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1356 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1361 /* write_ctrl_callback
1362 * USB completion handler for control pipe output
1363 * called by the USB subsystem in interrupt context
1365 * urb USB request block of completed request
1366 * urb->context = hardware specific controller state structure
1368 static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1370 struct bas_cardstate *ucs = urb->context;
1371 unsigned long flags;
1373 spin_lock_irqsave(&ucs->lock, flags);
1374 if (urb->status && ucs->pending) {
1375 dev_err(&ucs->interface->dev,
1376 "control request 0x%02x failed: %s\n",
1377 ucs->pending, get_usb_statmsg(urb->status));
1378 del_timer(&ucs->timer_ctrl);
1381 /* individual handling of specific request types */
1382 switch (ucs->pending) {
1383 case HD_DEVICE_INIT_ACK: /* no reply expected */
1387 spin_unlock_irqrestore(&ucs->lock, flags);
1391 * submit a control output request without message buffer to the Gigaset base
1392 * and optionally start a timeout
1394 * bcs B channel control structure
1395 * req control request code (HD_*)
1396 * val control request parameter value (set to 0 if unused)
1397 * timeout timeout in seconds (0: no timeout)
1400 * -EINVAL if a NULL pointer is encountered somewhere
1401 * -EBUSY if another request is pending
1402 * any URB submission error code
1404 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1406 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1408 unsigned long flags;
1410 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1412 spin_lock_irqsave(&ucs->lock, flags);
1414 spin_unlock_irqrestore(&ucs->lock, flags);
1415 dev_err(bcs->cs->dev,
1416 "submission of request 0x%02x failed: "
1417 "request 0x%02x still pending\n",
1421 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1422 spin_unlock_irqrestore(&ucs->lock, flags);
1423 dev_err(bcs->cs->dev,
1424 "could not submit request 0x%02x: URB busy\n", req);
1428 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1429 ucs->dr_ctrl.bRequest = req;
1430 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1431 ucs->dr_ctrl.wIndex = 0;
1432 ucs->dr_ctrl.wLength = 0;
1433 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1434 usb_sndctrlpipe(ucs->udev, 0),
1435 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1436 write_ctrl_callback, ucs);
1437 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1438 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1439 req, get_usb_statmsg(ret));
1440 spin_unlock_irqrestore(&ucs->lock, flags);
1446 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1447 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1448 ucs->timer_ctrl.data = (unsigned long) bcs;
1449 ucs->timer_ctrl.function = req_timeout;
1450 add_timer(&ucs->timer_ctrl);
1453 spin_unlock_irqrestore(&ucs->lock, flags);
1457 /* gigaset_init_bchannel
1458 * called by common.c to connect a B channel
1459 * initialize isochronous I/O and tell the Gigaset base to open the channel
1461 * B channel control structure
1463 * 0 on success, error code < 0 on error
1465 static int gigaset_init_bchannel(struct bc_state *bcs)
1469 if ((ret = starturbs(bcs)) < 0) {
1470 dev_err(bcs->cs->dev,
1471 "could not start isochronous I/O for channel %d\n",
1477 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1478 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1479 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1480 bcs->channel + 1, get_usb_statmsg(ret));
1481 stopurbs(bcs->hw.bas);
1487 /* gigaset_close_bchannel
1488 * called by common.c to disconnect a B channel
1489 * tell the Gigaset base to close the channel
1490 * stopping isochronous I/O and LL notification will be done when the
1491 * acknowledgement for the close arrives
1493 * B channel control structure
1495 * 0 on success, error code < 0 on error
1497 static int gigaset_close_bchannel(struct bc_state *bcs)
1501 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1502 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1503 /* channel not running: just signal common.c */
1504 gigaset_bchannel_down(bcs);
1508 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1509 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1510 dev_err(bcs->cs->dev,
1511 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1512 get_usb_statmsg(ret));
1516 /* Device Operations */
1517 /* ================= */
1520 * unqueue first command buffer from queue, waking any sleepers
1521 * must be called with cs->cmdlock held
1523 * cs controller state structure
1525 static void complete_cb(struct cardstate *cs)
1527 struct cmdbuf_t *cb = cs->cmdbuf;
1529 /* unqueue completed buffer */
1530 cs->cmdbytes -= cs->curlen;
1531 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1532 "write_command: sent %u bytes, %u left",
1533 cs->curlen, cs->cmdbytes);
1534 if ((cs->cmdbuf = cb->next) != NULL) {
1535 cs->cmdbuf->prev = NULL;
1536 cs->curlen = cs->cmdbuf->len;
1538 cs->lastcmdbuf = NULL;
1542 if (cb->wake_tasklet)
1543 tasklet_schedule(cb->wake_tasklet);
1548 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1550 /* write_command_callback
1551 * USB completion handler for AT command transmission
1552 * called by the USB subsystem in interrupt context
1554 * urb USB request block of completed request
1555 * urb->context = controller state structure
1557 static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1559 struct cardstate *cs = urb->context;
1560 struct bas_cardstate *ucs = cs->hw.bas;
1561 unsigned long flags;
1564 switch (urb->status) {
1565 case 0: /* normal completion */
1567 case -ENOENT: /* canceled */
1568 case -ECONNRESET: /* canceled (async) */
1569 case -EINPROGRESS: /* pending */
1570 /* ignore silently */
1571 gig_dbg(DEBUG_USBREQ, "%s: %s",
1572 __func__, get_usb_statmsg(urb->status));
1574 default: /* any failure */
1575 if (++ucs->retry_cmd_out > BAS_RETRY) {
1577 "command write: %s, "
1578 "giving up after %d retries\n",
1579 get_usb_statmsg(urb->status),
1580 ucs->retry_cmd_out);
1583 if (cs->cmdbuf == NULL) {
1585 "command write: %s, "
1586 "cannot retry - cmdbuf gone\n",
1587 get_usb_statmsg(urb->status));
1590 dev_notice(cs->dev, "command write: %s, retry %d\n",
1591 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1592 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1593 /* resubmitted - bypass regular exit block */
1595 /* command send failed, assume base still waiting */
1596 update_basstate(ucs, BS_ATREADY, 0);
1599 spin_lock_irqsave(&cs->cmdlock, flags);
1600 if (cs->cmdbuf != NULL)
1602 spin_unlock_irqrestore(&cs->cmdlock, flags);
1606 * timeout routine for AT command transmission
1608 * controller state structure
1610 static void atrdy_timeout(unsigned long data)
1612 struct cardstate *cs = (struct cardstate *) data;
1613 struct bas_cardstate *ucs = cs->hw.bas;
1615 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1617 /* fake the missing signal - what else can I do? */
1618 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1623 * submit an HD_WRITE_ATMESSAGE command URB
1625 * cs controller state structure
1626 * buf buffer containing command to send
1627 * len length of command to send
1630 * -EFAULT if a NULL pointer is encountered somewhere
1631 * -EBUSY if another request is pending
1632 * any URB submission error code
1634 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1636 struct bas_cardstate *ucs = cs->hw.bas;
1637 unsigned long flags;
1640 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1642 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
1644 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1648 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1649 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1650 ucs->dr_cmd_out.wValue = 0;
1651 ucs->dr_cmd_out.wIndex = 0;
1652 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1653 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1654 usb_sndctrlpipe(ucs->udev, 0),
1655 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1656 write_command_callback, cs);
1658 spin_lock_irqsave(&cs->lock, flags);
1659 ret = cs->connected ? usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC) : -ENODEV;
1660 spin_unlock_irqrestore(&cs->lock, flags);
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);
1762 return 0; /* nothing to do */
1764 if (len > IF_WRITEBUF)
1766 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1767 dev_err(cs->dev, "%s: out of memory\n", __func__);
1771 memcpy(cb->buf, buf, len);
1775 cb->wake_tasklet = wake_tasklet;
1777 spin_lock_irqsave(&cs->cmdlock, flags);
1778 cb->prev = cs->lastcmdbuf;
1780 cs->lastcmdbuf->next = cb;
1785 cs->cmdbytes += len;
1786 cs->lastcmdbuf = cb;
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1789 status = start_cbsend(cs);
1791 return status < 0 ? status : len;
1794 /* gigaset_write_room
1795 * tty_driver.write_room interface routine
1796 * return number of characters the driver will accept to be written via
1799 * controller state structure
1801 * number of characters
1803 static int gigaset_write_room(struct cardstate *cs)
1808 /* gigaset_chars_in_buffer
1809 * tty_driver.chars_in_buffer interface routine
1810 * return number of characters waiting to be sent
1812 * controller state structure
1814 * number of characters
1816 static int gigaset_chars_in_buffer(struct cardstate *cs)
1818 unsigned long flags;
1821 spin_lock_irqsave(&cs->cmdlock, flags);
1822 bytes = cs->cmdbytes;
1823 spin_unlock_irqrestore(&cs->cmdlock, flags);
1829 * implementation of ioctl(GIGASET_BRKCHARS)
1831 * controller state structure
1833 * -EINVAL (unimplemented function)
1835 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1841 /* Device Initialization/Shutdown */
1842 /* ============================== */
1844 /* Free hardware dependent part of the B channel structure
1846 * bcs B channel structure
1850 static int gigaset_freebcshw(struct bc_state *bcs)
1855 if (bcs->hw.bas->isooutbuf)
1856 kfree(bcs->hw.bas->isooutbuf);
1862 /* Initialize hardware dependent part of the B channel structure
1864 * bcs B channel structure
1868 static int gigaset_initbcshw(struct bc_state *bcs)
1871 struct bas_bc_state *ubc;
1873 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1875 err("could not allocate bas_bc_state");
1879 atomic_set(&ubc->running, 0);
1880 atomic_set(&ubc->corrbytes, 0);
1881 spin_lock_init(&ubc->isooutlock);
1882 for (i = 0; i < BAS_OUTURBS; ++i) {
1883 ubc->isoouturbs[i].urb = NULL;
1884 ubc->isoouturbs[i].bcs = bcs;
1886 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1888 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1889 err("could not allocate isochronous output buffer");
1894 tasklet_init(&ubc->sent_tasklet,
1895 &write_iso_tasklet, (unsigned long) bcs);
1897 spin_lock_init(&ubc->isoinlock);
1898 for (i = 0; i < BAS_INURBS; ++i)
1899 ubc->isoinurbs[i] = NULL;
1900 ubc->isoindone = NULL;
1901 ubc->loststatus = -EINPROGRESS;
1915 tasklet_init(&ubc->rcvd_tasklet,
1916 &read_iso_tasklet, (unsigned long) bcs);
1920 static void gigaset_reinitbcshw(struct bc_state *bcs)
1922 struct bas_bc_state *ubc = bcs->hw.bas;
1924 atomic_set(&bcs->hw.bas->running, 0);
1925 atomic_set(&bcs->hw.bas->corrbytes, 0);
1926 bcs->hw.bas->numsub = 0;
1927 spin_lock_init(&ubc->isooutlock);
1928 spin_lock_init(&ubc->isoinlock);
1929 ubc->loststatus = -EINPROGRESS;
1932 static void gigaset_freecshw(struct cardstate *cs)
1934 struct bas_cardstate *ucs = cs->hw.bas;
1936 del_timer(&ucs->timer_ctrl);
1937 del_timer(&ucs->timer_atrdy);
1938 del_timer(&ucs->timer_cmd_in);
1943 static int gigaset_initcshw(struct cardstate *cs)
1945 struct bas_cardstate *ucs;
1947 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
1951 ucs->urb_cmd_in = NULL;
1952 ucs->urb_cmd_out = NULL;
1954 ucs->rcvbuf_size = 0;
1956 spin_lock_init(&ucs->lock);
1959 atomic_set(&ucs->basstate, 0);
1960 init_timer(&ucs->timer_ctrl);
1961 init_timer(&ucs->timer_atrdy);
1962 init_timer(&ucs->timer_cmd_in);
1968 * unlink and deallocate all URBs unconditionally
1969 * caller must make sure that no commands are still in progress
1971 * cs controller state structure
1973 static void freeurbs(struct cardstate *cs)
1975 struct bas_cardstate *ucs = cs->hw.bas;
1976 struct bas_bc_state *ubc;
1979 for (j = 0; j < 2; ++j) {
1980 ubc = cs->bcs[j].hw.bas;
1981 for (i = 0; i < BAS_OUTURBS; ++i)
1982 if (ubc->isoouturbs[i].urb) {
1983 usb_kill_urb(ubc->isoouturbs[i].urb);
1985 "%s: isoc output URB %d/%d unlinked",
1987 usb_free_urb(ubc->isoouturbs[i].urb);
1988 ubc->isoouturbs[i].urb = NULL;
1990 for (i = 0; i < BAS_INURBS; ++i)
1991 if (ubc->isoinurbs[i]) {
1992 usb_kill_urb(ubc->isoinurbs[i]);
1994 "%s: isoc input URB %d/%d unlinked",
1996 usb_free_urb(ubc->isoinurbs[i]);
1997 ubc->isoinurbs[i] = NULL;
2000 if (ucs->urb_int_in) {
2001 usb_kill_urb(ucs->urb_int_in);
2002 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2004 usb_free_urb(ucs->urb_int_in);
2005 ucs->urb_int_in = NULL;
2007 if (ucs->urb_cmd_out) {
2008 usb_kill_urb(ucs->urb_cmd_out);
2009 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2011 usb_free_urb(ucs->urb_cmd_out);
2012 ucs->urb_cmd_out = NULL;
2014 if (ucs->urb_cmd_in) {
2015 usb_kill_urb(ucs->urb_cmd_in);
2016 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2018 usb_free_urb(ucs->urb_cmd_in);
2019 ucs->urb_cmd_in = NULL;
2021 if (ucs->urb_ctrl) {
2022 usb_kill_urb(ucs->urb_ctrl);
2023 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2025 usb_free_urb(ucs->urb_ctrl);
2026 ucs->urb_ctrl = NULL;
2031 * This function is called when a new USB device is connected.
2032 * It checks whether the new device is handled by this driver.
2034 static int gigaset_probe(struct usb_interface *interface,
2035 const struct usb_device_id *id)
2037 struct usb_host_interface *hostif;
2038 struct usb_device *udev = interface_to_usbdev(interface);
2039 struct cardstate *cs = NULL;
2040 struct bas_cardstate *ucs = NULL;
2041 struct bas_bc_state *ubc;
2042 struct usb_endpoint_descriptor *endpoint;
2047 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2048 __func__, le16_to_cpu(udev->descriptor.idVendor),
2049 le16_to_cpu(udev->descriptor.idProduct));
2051 /* See if the device offered us matches what we can accept */
2052 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2053 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2054 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2055 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2056 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
2057 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
2061 /* set required alternate setting */
2062 hostif = interface->cur_altsetting;
2063 if (hostif->desc.bAlternateSetting != 3) {
2065 "%s: wrong alternate setting %d - trying to switch",
2066 __func__, hostif->desc.bAlternateSetting);
2067 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2068 dev_warn(&udev->dev, "usb_set_interface failed, "
2069 "device %d interface %d altsetting %d\n",
2070 udev->devnum, hostif->desc.bInterfaceNumber,
2071 hostif->desc.bAlternateSetting);
2074 hostif = interface->cur_altsetting;
2077 /* Reject application specific interfaces
2079 if (hostif->desc.bInterfaceClass != 255) {
2080 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2081 __func__, hostif->desc.bInterfaceClass);
2085 dev_info(&udev->dev,
2086 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2087 __func__, le16_to_cpu(udev->descriptor.idVendor),
2088 le16_to_cpu(udev->descriptor.idProduct));
2090 cs = gigaset_getunassignedcs(driver);
2092 dev_err(&udev->dev, "no free cardstate\n");
2097 /* save off device structure ptrs for later use */
2100 ucs->interface = interface;
2101 cs->dev = &interface->dev;
2104 * - one for the interrupt pipe
2105 * - three for the different uses of the default control pipe
2106 * - three for each isochronous pipe
2108 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2109 if (!ucs->urb_int_in) {
2110 dev_err(cs->dev, "no free urbs available\n");
2113 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2114 if (!ucs->urb_cmd_in) {
2115 dev_err(cs->dev, "no free urbs available\n");
2118 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2119 if (!ucs->urb_cmd_out) {
2120 dev_err(cs->dev, "no free urbs available\n");
2123 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2124 if (!ucs->urb_ctrl) {
2125 dev_err(cs->dev, "no free urbs available\n");
2129 for (j = 0; j < 2; ++j) {
2130 ubc = cs->bcs[j].hw.bas;
2131 for (i = 0; i < BAS_OUTURBS; ++i) {
2132 ubc->isoouturbs[i].urb =
2133 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2134 if (!ubc->isoouturbs[i].urb) {
2135 dev_err(cs->dev, "no free urbs available\n");
2139 for (i = 0; i < BAS_INURBS; ++i) {
2141 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2142 if (!ubc->isoinurbs[i]) {
2143 dev_err(cs->dev, "no free urbs available\n");
2150 ucs->rcvbuf_size = 0;
2152 /* Fill the interrupt urb and send it to the core */
2153 endpoint = &hostif->endpoint[0].desc;
2154 usb_fill_int_urb(ucs->urb_int_in, udev,
2155 usb_rcvintpipe(udev,
2156 (endpoint->bEndpointAddress) & 0x0f),
2157 ucs->int_in_buf, 3, read_int_callback, cs,
2158 endpoint->bInterval);
2159 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2161 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2162 get_usb_statmsg(ret));
2166 /* tell the device that the driver is ready */
2167 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2170 /* tell common part that the device is ready */
2171 if (startmode == SM_LOCKED)
2172 atomic_set(&cs->mstate, MS_LOCKED);
2174 /* save address of controller structure */
2175 usb_set_intfdata(interface, cs);
2177 if (!gigaset_start(cs))
2184 usb_set_intfdata(interface, NULL);
2185 gigaset_unassign(cs);
2189 /* gigaset_disconnect
2190 * This function is called when the Gigaset base is unplugged.
2192 static void gigaset_disconnect(struct usb_interface *interface)
2194 struct cardstate *cs;
2195 struct bas_cardstate *ucs;
2197 cs = usb_get_intfdata(interface);
2201 dev_info(cs->dev, "disconnecting Gigaset base\n");
2204 usb_set_intfdata(interface, NULL);
2207 ucs->rcvbuf_size = 0;
2208 atomic_set(&ucs->basstate, 0);
2209 usb_put_dev(ucs->udev);
2210 ucs->interface = NULL;
2213 gigaset_unassign(cs);
2216 static struct gigaset_ops gigops = {
2219 gigaset_chars_in_buffer,
2221 gigaset_init_bchannel,
2222 gigaset_close_bchannel,
2225 gigaset_reinitbcshw,
2228 gigaset_set_modem_ctrl,
2230 gigaset_set_line_ctrl,
2231 gigaset_isoc_send_skb,
2236 * This function is called after the kernel module is loaded.
2238 static int __init bas_gigaset_init(void)
2242 /* allocate memory for our driver state and intialize it */
2243 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2244 GIGASET_MODULENAME, GIGASET_DEVNAME,
2245 GIGASET_DEVFSNAME, &gigops,
2246 THIS_MODULE)) == NULL)
2249 /* allocate memory for our device state and intialize it */
2250 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2251 GIGASET_MODULENAME);
2255 /* register this driver with the USB subsystem */
2256 result = usb_register(&gigaset_usb_driver);
2258 err("usb_register failed (error %d)", -result);
2262 info(DRIVER_AUTHOR);
2266 error: if (cardstate)
2267 gigaset_freecs(cardstate);
2270 gigaset_freedriver(driver);
2276 * This function is called before the kernel module is unloaded.
2278 static void __exit bas_gigaset_exit(void)
2280 gigaset_blockdriver(driver); /* => probe will fail
2281 * => no gigaset_start any more
2284 gigaset_shutdown(cardstate);
2285 /* from now on, no isdn callback should be possible */
2287 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
2288 gig_dbg(DEBUG_ANY, "closing AT channel");
2289 if (req_submit(cardstate->bcs,
2290 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2291 /* successfully submitted */
2292 //FIXME wait for completion?
2296 /* deregister this driver with the USB subsystem */
2297 usb_deregister(&gigaset_usb_driver);
2298 /* this will call the disconnect-callback */
2299 /* from now on, no disconnect/probe callback should be running */
2301 gigaset_freecs(cardstate);
2303 gigaset_freedriver(driver);
2308 module_init(bas_gigaset_init);
2309 module_exit(bas_gigaset_exit);
2311 MODULE_AUTHOR(DRIVER_AUTHOR);
2312 MODULE_DESCRIPTION(DRIVER_DESC);
2313 MODULE_LICENSE("GPL");