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 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/usb.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
26 /* Version Information */
27 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
28 #define DRIVER_DESC "USB Driver for Gigaset 307x"
31 /* Module parameters */
33 static int startmode = SM_ISDN;
34 static int cidmode = 1;
36 module_param(startmode, int, S_IRUGO);
37 module_param(cidmode, int, S_IRUGO);
38 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39 MODULE_PARM_DESC(cidmode, "Call-ID mode");
41 #define GIGASET_MINORS 1
42 #define GIGASET_MINOR 16
43 #define GIGASET_MODULENAME "bas_gigaset"
44 #define GIGASET_DEVFSNAME "gig/bas/"
45 #define GIGASET_DEVNAME "ttyGB"
47 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
48 #define IF_WRITEBUF 264
50 /* Values for the Gigaset 307x */
51 #define USB_GIGA_VENDOR_ID 0x0681
52 #define USB_3070_PRODUCT_ID 0x0001
53 #define USB_3075_PRODUCT_ID 0x0002
54 #define USB_SX303_PRODUCT_ID 0x0021
55 #define USB_SX353_PRODUCT_ID 0x0022
57 /* table of devices that work with this driver */
58 static struct usb_device_id gigaset_table [] = {
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
60 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
63 { } /* Terminating entry */
66 MODULE_DEVICE_TABLE(usb, gigaset_table);
68 /*======================= local function prototypes =============================*/
70 /* This function is called if a new device is connected to the USB port. It
71 * checks whether this new device belongs to this driver.
73 static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
76 /* Function will be called if the device is unplugged */
77 static void gigaset_disconnect(struct usb_interface *interface);
79 static void read_ctrl_callback(struct urb *, struct pt_regs *);
80 static void stopurbs(struct bas_bc_state *);
81 static int atwrite_submit(struct cardstate *, unsigned char *, int);
82 static int start_cbsend(struct cardstate *);
84 /*==============================================================================*/
86 struct bas_cardstate {
87 struct usb_device *udev; /* USB device pointer */
88 struct usb_interface *interface; /* interface for this device */
89 unsigned char minor; /* starting minor number */
91 struct urb *urb_ctrl; /* control pipe default URB */
92 struct usb_ctrlrequest dr_ctrl;
93 struct timer_list timer_ctrl; /* control request timeout */
95 struct timer_list timer_atrdy; /* AT command ready timeout */
96 struct urb *urb_cmd_out; /* for sending AT commands */
97 struct usb_ctrlrequest dr_cmd_out;
100 struct urb *urb_cmd_in; /* for receiving AT replies */
101 struct usb_ctrlrequest dr_cmd_in;
102 struct timer_list timer_cmd_in; /* receive request timeout */
103 unsigned char *rcvbuf; /* AT reply receive buffer */
105 struct urb *urb_int_in; /* URB for interrupt pipe */
106 unsigned char int_in_buf[3];
108 spinlock_t lock; /* locks all following */
109 atomic_t basstate; /* bitmap (BS_*) */
110 int pending; /* uncompleted base request */
111 int rcvbuf_size; /* size of AT receive buffer */
112 /* 0: no receive in progress */
113 int retry_cmd_in; /* receive req retry count */
116 /* status of direct USB connection to 307x base (bits in basstate) */
117 #define BS_ATOPEN 0x001 /* AT channel open */
118 #define BS_B1OPEN 0x002 /* B channel 1 open */
119 #define BS_B2OPEN 0x004 /* B channel 2 open */
120 #define BS_ATREADY 0x008 /* base ready for AT command */
121 #define BS_INIT 0x010 /* base has signalled INIT_OK */
122 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
123 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
124 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
127 static struct gigaset_driver *driver = NULL;
128 static struct cardstate *cardstate = NULL;
130 /* usb specific object needed to register this driver with the usb subsystem */
131 static struct usb_driver gigaset_usb_driver = {
132 .name = GIGASET_MODULENAME,
133 .probe = gigaset_probe,
134 .disconnect = gigaset_disconnect,
135 .id_table = gigaset_table,
138 /* get message text for usb_submit_urb return code
140 static char *get_usb_rcmsg(int rc)
142 static char unkmsg[28];
148 return "out of memory";
150 return "device not present";
152 return "endpoint not present";
154 return "URB type not supported";
156 return "invalid argument";
158 return "start frame too early or too much scheduled";
160 return "too many isochronous frames requested";
162 return "endpoint stalled";
164 return "invalid packet size";
166 return "would overcommit USB bandwidth";
168 return "device shut down";
170 return "reject flag set";
172 return "device suspended";
174 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
179 /* get message text for USB status code
181 static char *get_usb_statmsg(int status)
183 static char unkmsg[28];
189 return "unlinked (sync)";
193 return "bit stuffing error, timeout, or unknown USB error";
195 return "CRC mismatch, timeout, or unknown USB error";
199 return "endpoint stalled";
201 return "IN buffer overrun";
203 return "OUT buffer underrun";
205 return "too much data";
207 return "short packet detected";
209 return "device removed";
211 return "partial isochronous transfer";
213 return "invalid argument";
215 return "unlinked (async)";
217 return "device shut down";
219 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
225 * retrieve string representation of USB pipe type
227 static inline char *usb_pipetype_str(int pipe)
229 if (usb_pipeisoc(pipe))
231 if (usb_pipeint(pipe))
233 if (usb_pipecontrol(pipe))
235 if (usb_pipebulk(pipe))
241 * write content of URB to syslog for debugging
243 static inline void dump_urb(enum debuglevel level, const char *tag,
246 #ifdef CONFIG_GIGASET_DEBUG
248 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
251 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
252 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
253 (unsigned long) urb->dev,
254 usb_pipetype_str(urb->pipe),
255 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
256 usb_pipein(urb->pipe) ? "in" : "out",
257 urb->status, (unsigned long) urb->hcpriv,
258 urb->transfer_flags);
260 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
261 "bandwidth=%d, setup_packet=0x%08lx,",
262 (unsigned long) urb->transfer_buffer,
263 urb->transfer_buffer_length, urb->actual_length,
264 urb->bandwidth, (unsigned long) urb->setup_packet);
266 " start_frame=%d, number_of_packets=%d, interval=%d, "
268 urb->start_frame, urb->number_of_packets, urb->interval,
271 " context=0x%08lx, complete=0x%08lx, "
272 "iso_frame_desc[]={",
273 (unsigned long) urb->context,
274 (unsigned long) urb->complete);
275 for (i = 0; i < urb->number_of_packets; i++) {
276 struct usb_iso_packet_descriptor *pifd
277 = &urb->iso_frame_desc[i];
279 " {offset=%u, length=%u, actual_length=%u, "
281 pifd->offset, pifd->length, pifd->actual_length,
285 gig_dbg(level, "}}");
289 /* read/set modem control bits etc. (m10x only) */
290 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
296 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
301 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
307 * hang up any existing connection because of an unrecoverable error
308 * This function may be called from any context and takes care of scheduling
309 * the necessary actions for execution outside of interrupt context.
311 * B channel control structure
313 static inline void error_hangup(struct bc_state *bcs)
315 struct cardstate *cs = bcs->cs;
317 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
318 __func__, bcs->channel);
320 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
321 dev_err(cs->dev, "event queue full\n");
323 gigaset_schedule_event(cs);
327 * reset Gigaset device because of an unrecoverable error
328 * This function may be called from any context, and should take care of
329 * scheduling the necessary actions for execution outside of interrupt context.
330 * Right now, it just generates a kernel message calling for help.
332 * controller state structure
334 static inline void error_reset(struct cardstate *cs)
336 //FIXME try to recover without bothering the user
338 "unrecoverable error - please disconnect Gigaset base to reset\n");
342 * check for completion of pending control request
344 * ucs hardware specific controller state structure
346 static void check_pending(struct bas_cardstate *ucs)
350 spin_lock_irqsave(&ucs->lock, flags);
351 switch (ucs->pending) {
354 case HD_OPEN_ATCHANNEL:
355 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
358 case HD_OPEN_B1CHANNEL:
359 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
362 case HD_OPEN_B2CHANNEL:
363 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
366 case HD_CLOSE_ATCHANNEL:
367 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
370 case HD_CLOSE_B1CHANNEL:
371 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
374 case HD_CLOSE_B2CHANNEL:
375 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
378 case HD_DEVICE_INIT_ACK: /* no reply expected */
381 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
382 * are handled separately and should never end up here
385 dev_warn(&ucs->interface->dev,
386 "unknown pending request 0x%02x cleared\n",
392 del_timer(&ucs->timer_ctrl);
394 spin_unlock_irqrestore(&ucs->lock, flags);
398 * timeout routine for command input request
400 * controller state structure
402 static void cmd_in_timeout(unsigned long data)
404 struct cardstate *cs = (struct cardstate *) data;
405 struct bas_cardstate *ucs = cs->hw.bas;
407 if (!ucs->rcvbuf_size) {
408 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
412 dev_err(cs->dev, "timeout reading AT response\n");
413 error_reset(cs); //FIXME retry?
416 /* set/clear bits in base connection state, return previous state
418 inline static int update_basstate(struct bas_cardstate *ucs,
424 spin_lock_irqsave(&ucs->lock, flags);
425 state = atomic_read(&ucs->basstate);
426 atomic_set(&ucs->basstate, (state & ~clear) | set);
427 spin_unlock_irqrestore(&ucs->lock, flags);
432 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
434 * cs controller state structure
435 * timeout timeout in 1/10 sec., 0: none
438 * -EBUSY if another request is pending
439 * any URB submission error code
441 static int atread_submit(struct cardstate *cs, int timeout)
443 struct bas_cardstate *ucs = cs->hw.bas;
446 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
449 if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
451 "could not submit HD_READ_ATMESSAGE: URB busy\n");
455 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
456 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
457 ucs->dr_cmd_in.wValue = 0;
458 ucs->dr_cmd_in.wIndex = 0;
459 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
460 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
461 usb_rcvctrlpipe(ucs->udev, 0),
462 (unsigned char*) & ucs->dr_cmd_in,
463 ucs->rcvbuf, ucs->rcvbuf_size,
464 read_ctrl_callback, cs->inbuf);
466 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
467 update_basstate(ucs, 0, BS_ATRDPEND);
468 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
469 get_usb_statmsg(ret));
474 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
475 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
476 ucs->timer_cmd_in.data = (unsigned long) cs;
477 ucs->timer_cmd_in.function = cmd_in_timeout;
478 add_timer(&ucs->timer_cmd_in);
484 * USB completion handler for interrupt pipe input
485 * called by the USB subsystem in interrupt context
487 * urb USB request block
488 * urb->context = controller state structure
490 static void read_int_callback(struct urb *urb, struct pt_regs *regs)
492 struct cardstate *cs = urb->context;
493 struct bas_cardstate *ucs = cs->hw.bas;
494 struct bc_state *bcs;
500 switch (urb->status) {
501 case 0: /* success */
503 case -ENOENT: /* cancelled */
504 case -ECONNRESET: /* cancelled (async) */
505 case -EINPROGRESS: /* pending */
506 /* ignore silently */
507 gig_dbg(DEBUG_USBREQ, "%s: %s",
508 __func__, get_usb_statmsg(urb->status));
510 case -ENODEV: /* device removed */
511 case -ESHUTDOWN: /* device shut down */
512 //FIXME use this as disconnect indicator?
513 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
515 default: /* severe trouble */
516 dev_warn(cs->dev, "interrupt read: %s\n",
517 get_usb_statmsg(urb->status));
518 //FIXME corrective action? resubmission always ok?
522 /* drop incomplete packets even if the missing bytes wouldn't matter */
523 if (unlikely(urb->actual_length < 3)) {
524 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
529 l = (unsigned) ucs->int_in_buf[1] +
530 (((unsigned) ucs->int_in_buf[2]) << 8);
532 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
533 urb->actual_length, (int)ucs->int_in_buf[0], l,
534 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
538 switch (ucs->int_in_buf[0]) {
539 case HD_DEVICE_INIT_OK:
540 update_basstate(ucs, BS_INIT, 0);
543 case HD_READY_SEND_ATDATA:
544 del_timer(&ucs->timer_atrdy);
545 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
549 case HD_OPEN_B2CHANNEL_ACK:
551 case HD_OPEN_B1CHANNEL_ACK:
552 bcs = cs->bcs + channel;
553 update_basstate(ucs, BS_B1OPEN << channel, 0);
554 gigaset_bchannel_up(bcs);
557 case HD_OPEN_ATCHANNEL_ACK:
558 update_basstate(ucs, BS_ATOPEN, 0);
562 case HD_CLOSE_B2CHANNEL_ACK:
564 case HD_CLOSE_B1CHANNEL_ACK:
565 bcs = cs->bcs + channel;
566 update_basstate(ucs, 0, BS_B1OPEN << channel);
567 stopurbs(bcs->hw.bas);
568 gigaset_bchannel_down(bcs);
571 case HD_CLOSE_ATCHANNEL_ACK:
572 update_basstate(ucs, 0, BS_ATOPEN);
575 case HD_B2_FLOW_CONTROL:
577 case HD_B1_FLOW_CONTROL:
578 bcs = cs->bcs + channel;
579 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
580 &bcs->hw.bas->corrbytes);
582 "Flow control (channel %d, sub %d): 0x%02x => %d",
583 channel, bcs->hw.bas->numsub, l,
584 atomic_read(&bcs->hw.bas->corrbytes));
587 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
590 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
593 spin_lock_irqsave(&cs->lock, flags);
594 if (ucs->rcvbuf_size) {
595 /* throw away previous buffer - we have no queue */
597 "receive AT data overrun, %d bytes lost\n",
600 ucs->rcvbuf_size = 0;
602 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
603 spin_unlock_irqrestore(&cs->lock, flags);
604 dev_err(cs->dev, "out of memory receiving AT data\n");
608 ucs->rcvbuf_size = l;
609 ucs->retry_cmd_in = 0;
610 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
613 ucs->rcvbuf_size = 0;
615 //FIXME corrective action?
618 spin_unlock_irqrestore(&cs->lock, flags);
621 case HD_RESET_INTERRUPT_PIPE_ACK:
622 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
626 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
631 "unknown Gigaset signal 0x%02x (%u) ignored\n",
632 (int) ucs->int_in_buf[0], l);
638 rc = usb_submit_urb(urb, SLAB_ATOMIC);
639 if (unlikely(rc != 0 && rc != -ENODEV)) {
640 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
646 /* read_ctrl_callback
647 * USB completion handler for control pipe input
648 * called by the USB subsystem in interrupt context
650 * urb USB request block
651 * urb->context = inbuf structure for controller state
653 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
655 struct inbuf_t *inbuf = urb->context;
656 struct cardstate *cs = inbuf->cs;
657 struct bas_cardstate *ucs = cs->hw.bas;
662 update_basstate(ucs, 0, BS_ATRDPEND);
664 if (!ucs->rcvbuf_size) {
665 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
669 del_timer(&ucs->timer_cmd_in);
671 switch (urb->status) {
672 case 0: /* normal completion */
673 numbytes = urb->actual_length;
674 if (unlikely(numbytes == 0)) {
676 "control read: empty block received\n");
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
681 "control read: received %d chars, expected %d\n",
682 numbytes, ucs->rcvbuf_size);
683 if (numbytes > ucs->rcvbuf_size)
684 numbytes = ucs->rcvbuf_size;
687 /* copy received bytes to inbuf */
688 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
690 if (unlikely(numbytes < ucs->rcvbuf_size)) {
691 /* incomplete - resubmit for remaining bytes */
692 ucs->rcvbuf_size -= numbytes;
693 ucs->retry_cmd_in = 0;
698 case -ENOENT: /* cancelled */
699 case -ECONNRESET: /* cancelled (async) */
700 case -EINPROGRESS: /* pending */
701 case -ENODEV: /* device removed */
702 case -ESHUTDOWN: /* device shut down */
703 /* no action necessary */
704 gig_dbg(DEBUG_USBREQ, "%s: %s",
705 __func__, get_usb_statmsg(urb->status));
708 default: /* severe trouble */
709 dev_warn(cs->dev, "control read: %s\n",
710 get_usb_statmsg(urb->status));
712 if (ucs->retry_cmd_in++ < BAS_RETRY) {
713 dev_notice(cs->dev, "control read: retry %d\n",
715 rc = atread_submit(cs, BAS_TIMEOUT);
716 if (rc >= 0 || rc == -ENODEV)
717 /* resubmitted or disconnected */
718 /* - bypass regular exit block */
722 "control read: giving up after %d tries\n",
730 ucs->rcvbuf_size = 0;
732 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
733 gigaset_schedule_event(cs);
738 * USB completion handler for B channel isochronous input
739 * called by the USB subsystem in interrupt context
741 * urb USB request block of completed request
742 * urb->context = bc_state structure
744 static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
746 struct bc_state *bcs;
747 struct bas_bc_state *ubc;
751 /* status codes not worth bothering the tasklet with */
752 if (unlikely(urb->status == -ENOENT ||
753 urb->status == -ECONNRESET ||
754 urb->status == -EINPROGRESS ||
755 urb->status == -ENODEV ||
756 urb->status == -ESHUTDOWN)) {
757 gig_dbg(DEBUG_ISO, "%s: %s",
758 __func__, get_usb_statmsg(urb->status));
765 spin_lock_irqsave(&ubc->isoinlock, flags);
766 if (likely(ubc->isoindone == NULL)) {
767 /* pass URB to tasklet */
768 ubc->isoindone = urb;
769 tasklet_schedule(&ubc->rcvd_tasklet);
771 /* tasklet still busy, drop data and resubmit URB */
772 ubc->loststatus = urb->status;
773 for (i = 0; i < BAS_NUMFRAMES; i++) {
774 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
775 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
776 urb->iso_frame_desc[i].status !=
778 ubc->loststatus = urb->iso_frame_desc[i].status;
779 urb->iso_frame_desc[i].status = 0;
780 urb->iso_frame_desc[i].actual_length = 0;
782 if (likely(atomic_read(&ubc->running))) {
783 /* urb->dev is clobbered by USB subsystem */
784 urb->dev = bcs->cs->hw.bas->udev;
785 urb->transfer_flags = URB_ISO_ASAP;
786 urb->number_of_packets = BAS_NUMFRAMES;
787 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
789 rc = usb_submit_urb(urb, SLAB_ATOMIC);
790 if (unlikely(rc != 0 && rc != -ENODEV)) {
791 dev_err(bcs->cs->dev,
792 "could not resubmit isochronous read "
793 "URB: %s\n", get_usb_rcmsg(rc));
794 dump_urb(DEBUG_ISO, "isoc read", urb);
799 spin_unlock_irqrestore(&ubc->isoinlock, flags);
802 /* write_iso_callback
803 * USB completion handler for B channel isochronous output
804 * called by the USB subsystem in interrupt context
806 * urb USB request block of completed request
807 * urb->context = isow_urbctx_t structure
809 static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
811 struct isow_urbctx_t *ucx;
812 struct bas_bc_state *ubc;
815 /* status codes not worth bothering the tasklet with */
816 if (unlikely(urb->status == -ENOENT ||
817 urb->status == -ECONNRESET ||
818 urb->status == -EINPROGRESS ||
819 urb->status == -ENODEV ||
820 urb->status == -ESHUTDOWN)) {
821 gig_dbg(DEBUG_ISO, "%s: %s",
822 __func__, get_usb_statmsg(urb->status));
826 /* pass URB context to tasklet */
828 ubc = ucx->bcs->hw.bas;
830 spin_lock_irqsave(&ubc->isooutlock, flags);
831 ubc->isooutovfl = ubc->isooutdone;
832 ubc->isooutdone = ucx;
833 spin_unlock_irqrestore(&ubc->isooutlock, flags);
834 tasklet_schedule(&ubc->sent_tasklet);
838 * prepare and submit USB request blocks for isochronous input and output
840 * B channel control structure
843 * < 0 on error (no URBs submitted)
845 static int starturbs(struct bc_state *bcs)
847 struct bas_bc_state *ubc = bcs->hw.bas;
852 /* initialize L2 reception */
853 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
854 bcs->inputstate |= INS_flag_hunt;
856 /* submit all isochronous input URBs */
857 atomic_set(&ubc->running, 1);
858 for (k = 0; k < BAS_INURBS; k++) {
859 urb = ubc->isoinurbs[k];
865 urb->dev = bcs->cs->hw.bas->udev;
866 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
867 urb->transfer_flags = URB_ISO_ASAP;
868 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
869 urb->transfer_buffer_length = BAS_INBUFSIZE;
870 urb->number_of_packets = BAS_NUMFRAMES;
871 urb->interval = BAS_FRAMETIME;
872 urb->complete = read_iso_callback;
874 for (j = 0; j < BAS_NUMFRAMES; j++) {
875 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
876 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
877 urb->iso_frame_desc[j].status = 0;
878 urb->iso_frame_desc[j].actual_length = 0;
881 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
882 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0)
886 /* initialize L2 transmission */
887 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
889 /* set up isochronous output URBs for flag idling */
890 for (k = 0; k < BAS_OUTURBS; ++k) {
891 urb = ubc->isoouturbs[k].urb;
896 urb->dev = bcs->cs->hw.bas->udev;
897 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
898 urb->transfer_flags = URB_ISO_ASAP;
899 urb->transfer_buffer = ubc->isooutbuf->data;
900 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
901 urb->number_of_packets = BAS_NUMFRAMES;
902 urb->interval = BAS_FRAMETIME;
903 urb->complete = write_iso_callback;
904 urb->context = &ubc->isoouturbs[k];
905 for (j = 0; j < BAS_NUMFRAMES; ++j) {
906 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
907 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
908 urb->iso_frame_desc[j].status = 0;
909 urb->iso_frame_desc[j].actual_length = 0;
911 ubc->isoouturbs[k].limit = -1;
914 /* submit two URBs, keep third one */
915 for (k = 0; k < 2; ++k) {
916 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
917 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
921 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
922 ubc->isooutfree = &ubc->isoouturbs[2];
923 ubc->isooutdone = ubc->isooutovfl = NULL;
931 * cancel the USB request blocks for isochronous input and output
932 * errors are silently ignored
934 * B channel control structure
936 static void stopurbs(struct bas_bc_state *ubc)
940 atomic_set(&ubc->running, 0);
942 for (k = 0; k < BAS_INURBS; ++k) {
943 rc = usb_unlink_urb(ubc->isoinurbs[k]);
945 "%s: isoc input URB %d unlinked, result = %s",
946 __func__, k, get_usb_rcmsg(rc));
949 for (k = 0; k < BAS_OUTURBS; ++k) {
950 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
952 "%s: isoc output URB %d unlinked, result = %s",
953 __func__, k, get_usb_rcmsg(rc));
957 /* Isochronous Write - Bottom Half */
958 /* =============================== */
960 /* submit_iso_write_urb
961 * fill and submit the next isochronous write URB
963 * ucx context structure containing URB
965 * number of frames submitted in URB
966 * 0 if URB not submitted because no data available (isooutbuf busy)
967 * error code < 0 on error
969 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
971 struct urb *urb = ucx->urb;
972 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
973 struct usb_iso_packet_descriptor *ifd;
974 int corrbytes, nframe, rc;
976 /* urb->dev is clobbered by USB subsystem */
977 urb->dev = ucx->bcs->cs->hw.bas->udev;
978 urb->transfer_flags = URB_ISO_ASAP;
979 urb->transfer_buffer = ubc->isooutbuf->data;
980 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
982 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
983 ifd = &urb->iso_frame_desc[nframe];
985 /* compute frame length according to flow control */
986 ifd->length = BAS_NORMFRAME;
987 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
988 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
989 __func__, corrbytes);
990 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
991 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
992 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
993 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
994 ifd->length += corrbytes;
995 atomic_add(-corrbytes, &ubc->corrbytes);
998 /* retrieve block of data to send */
999 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1001 if (ifd->offset < 0) {
1002 if (ifd->offset == -EBUSY) {
1004 "%s: buffer busy at frame %d",
1006 /* tasklet will be restarted from
1007 gigaset_send_skb() */
1009 dev_err(ucx->bcs->cs->dev,
1010 "%s: buffer error %d at frame %d\n",
1011 __func__, ifd->offset, nframe);
1016 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1018 ifd->actual_length = 0;
1020 if (unlikely(nframe == 0))
1021 return 0; /* no data to send */
1022 urb->number_of_packets = nframe;
1024 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1027 /* device removed - give up silently */
1028 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1030 dev_err(ucx->bcs->cs->dev,
1031 "could not submit isochronous write URB: %s\n",
1039 /* write_iso_tasklet
1040 * tasklet scheduled when an isochronous output URB from the Gigaset device
1043 * data B channel state structure
1045 static void write_iso_tasklet(unsigned long data)
1047 struct bc_state *bcs = (struct bc_state *) data;
1048 struct bas_bc_state *ubc = bcs->hw.bas;
1049 struct cardstate *cs = bcs->cs;
1050 struct isow_urbctx_t *done, *next, *ovfl;
1052 struct usb_iso_packet_descriptor *ifd;
1054 unsigned long flags;
1056 struct sk_buff *skb;
1060 /* loop while completed URBs arrive in time */
1062 if (unlikely(!(atomic_read(&ubc->running)))) {
1063 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1067 /* retrieve completed URBs */
1068 spin_lock_irqsave(&ubc->isooutlock, flags);
1069 done = ubc->isooutdone;
1070 ubc->isooutdone = NULL;
1071 ovfl = ubc->isooutovfl;
1072 ubc->isooutovfl = NULL;
1073 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1075 dev_err(cs->dev, "isochronous write buffer underrun\n");
1082 /* submit free URB if available */
1083 spin_lock_irqsave(&ubc->isooutlock, flags);
1084 next = ubc->isooutfree;
1085 ubc->isooutfree = NULL;
1086 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1088 rc = submit_iso_write_urb(next);
1089 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1090 /* could not submit URB, put it back */
1091 spin_lock_irqsave(&ubc->isooutlock, flags);
1092 if (ubc->isooutfree == NULL) {
1093 ubc->isooutfree = next;
1096 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1098 /* couldn't put it back */
1100 "losing isochronous write URB\n");
1106 /* process completed URB */
1108 switch (urb->status) {
1109 case -EXDEV: /* partial completion */
1110 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1112 /* fall through - what's the difference anyway? */
1113 case 0: /* normal completion */
1114 /* inspect individual frames
1115 * assumptions (for lack of documentation):
1116 * - actual_length bytes of first frame in error are
1118 * - all following frames are not sent at all
1120 offset = done->limit; /* default (no error) */
1121 for (i = 0; i < BAS_NUMFRAMES; i++) {
1122 ifd = &urb->iso_frame_desc[i];
1124 ifd->actual_length != ifd->length) {
1126 "isochronous write: frame %d: %s, "
1127 "only %d of %d bytes sent\n",
1128 i, get_usb_statmsg(ifd->status),
1129 ifd->actual_length, ifd->length);
1130 offset = (ifd->offset +
1136 #ifdef CONFIG_GIGASET_DEBUG
1137 /* check assumption on remaining frames */
1138 for (; i < BAS_NUMFRAMES; i++) {
1139 ifd = &urb->iso_frame_desc[i];
1140 if (ifd->status != -EINPROGRESS
1141 || ifd->actual_length != 0) {
1143 "isochronous write: frame %d: %s, "
1144 "%d of %d bytes sent\n",
1145 i, get_usb_statmsg(ifd->status),
1146 ifd->actual_length, ifd->length);
1147 offset = (ifd->offset +
1155 case -EPIPE: /* stall - probably underrun */
1156 dev_err(cs->dev, "isochronous write stalled\n");
1159 default: /* severe trouble */
1160 dev_warn(cs->dev, "isochronous write: %s\n",
1161 get_usb_statmsg(urb->status));
1164 /* mark the write buffer area covered by this URB as free */
1165 if (done->limit >= 0)
1166 atomic_set(&ubc->isooutbuf->read, done->limit);
1168 /* mark URB as free */
1169 spin_lock_irqsave(&ubc->isooutlock, flags);
1170 next = ubc->isooutfree;
1171 ubc->isooutfree = done;
1172 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1174 /* only one URB still active - resubmit one */
1175 rc = submit_iso_write_urb(next);
1176 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1177 /* couldn't submit */
1183 /* process queued SKBs */
1184 while ((skb = skb_dequeue(&bcs->squeue))) {
1185 /* copy to output buffer, doing L2 encapsulation */
1187 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1188 /* insufficient buffer space, push back onto queue */
1189 skb_queue_head(&bcs->squeue, skb);
1190 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1191 __func__, skb_queue_len(&bcs->squeue));
1195 gigaset_skb_sent(bcs, skb);
1196 dev_kfree_skb_any(skb);
1200 /* Isochronous Read - Bottom Half */
1201 /* ============================== */
1204 * tasklet scheduled when an isochronous input URB from the Gigaset device
1207 * data B channel state structure
1209 static void read_iso_tasklet(unsigned long data)
1211 struct bc_state *bcs = (struct bc_state *) data;
1212 struct bas_bc_state *ubc = bcs->hw.bas;
1213 struct cardstate *cs = bcs->cs;
1216 unsigned long flags;
1217 int totleft, numbytes, offset, frame, rc;
1219 /* loop while more completed URBs arrive in the meantime */
1222 spin_lock_irqsave(&ubc->isoinlock, flags);
1223 if (!(urb = ubc->isoindone)) {
1224 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1227 ubc->isoindone = NULL;
1228 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1230 "isochronous read overrun, "
1231 "dropped URB with status: %s, %d bytes lost\n",
1232 get_usb_statmsg(ubc->loststatus),
1234 ubc->loststatus = -EINPROGRESS;
1236 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1238 if (unlikely(!(atomic_read(&ubc->running)))) {
1240 "%s: channel not running, "
1241 "dropped URB with status: %s",
1242 __func__, get_usb_statmsg(urb->status));
1246 switch (urb->status) {
1247 case 0: /* normal completion */
1249 case -EXDEV: /* inspect individual frames
1250 (we do that anyway) */
1251 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1257 gig_dbg(DEBUG_ISO, "%s: %s",
1258 __func__, get_usb_statmsg(urb->status));
1259 continue; /* -> skip */
1261 dev_err(cs->dev, "isochronous read stalled\n");
1263 continue; /* -> skip */
1264 default: /* severe trouble */
1265 dev_warn(cs->dev, "isochronous read: %s\n",
1266 get_usb_statmsg(urb->status));
1270 rcvbuf = urb->transfer_buffer;
1271 totleft = urb->actual_length;
1272 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1273 if (unlikely(urb->iso_frame_desc[frame].status)) {
1275 "isochronous read: frame %d: %s\n",
1278 urb->iso_frame_desc[frame].status));
1281 numbytes = urb->iso_frame_desc[frame].actual_length;
1282 if (unlikely(numbytes > BAS_MAXFRAME)) {
1284 "isochronous read: frame %d: "
1285 "numbytes (%d) > BAS_MAXFRAME\n",
1289 if (unlikely(numbytes > totleft)) {
1291 "isochronous read: frame %d: "
1292 "numbytes (%d) > totleft (%d)\n",
1293 frame, numbytes, totleft);
1296 offset = urb->iso_frame_desc[frame].offset;
1297 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1299 "isochronous read: frame %d: "
1300 "offset (%d) + numbytes (%d) "
1301 "> BAS_INBUFSIZE\n",
1302 frame, offset, numbytes);
1305 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1306 totleft -= numbytes;
1308 if (unlikely(totleft > 0))
1310 "isochronous read: %d data bytes missing\n",
1314 /* URB processed, resubmit */
1315 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1316 urb->iso_frame_desc[frame].status = 0;
1317 urb->iso_frame_desc[frame].actual_length = 0;
1319 /* urb->dev is clobbered by USB subsystem */
1320 urb->dev = bcs->cs->hw.bas->udev;
1321 urb->transfer_flags = URB_ISO_ASAP;
1322 urb->number_of_packets = BAS_NUMFRAMES;
1323 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1324 if (unlikely(rc != 0 && rc != -ENODEV)) {
1326 "could not resubmit isochronous read URB: %s\n",
1328 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1334 /* Channel Operations */
1335 /* ================== */
1338 * timeout routine for control output request
1340 * B channel control structure
1342 static void req_timeout(unsigned long data)
1344 struct bc_state *bcs = (struct bc_state *) data;
1345 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1347 unsigned long flags;
1351 spin_lock_irqsave(&ucs->lock, flags);
1352 pending = ucs->pending;
1354 spin_unlock_irqrestore(&ucs->lock, flags);
1357 case 0: /* no pending request */
1358 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1361 case HD_OPEN_ATCHANNEL:
1362 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1363 error_reset(bcs->cs);
1366 case HD_OPEN_B2CHANNEL:
1367 case HD_OPEN_B1CHANNEL:
1368 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1373 case HD_CLOSE_ATCHANNEL:
1374 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1377 case HD_CLOSE_B2CHANNEL:
1378 case HD_CLOSE_B1CHANNEL:
1379 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1384 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1389 /* write_ctrl_callback
1390 * USB completion handler for control pipe output
1391 * called by the USB subsystem in interrupt context
1393 * urb USB request block of completed request
1394 * urb->context = hardware specific controller state structure
1396 static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1398 struct bas_cardstate *ucs = urb->context;
1399 unsigned long flags;
1401 spin_lock_irqsave(&ucs->lock, flags);
1402 if (urb->status && ucs->pending) {
1403 dev_err(&ucs->interface->dev,
1404 "control request 0x%02x failed: %s\n",
1405 ucs->pending, get_usb_statmsg(urb->status));
1406 del_timer(&ucs->timer_ctrl);
1409 /* individual handling of specific request types */
1410 switch (ucs->pending) {
1411 case HD_DEVICE_INIT_ACK: /* no reply expected */
1415 spin_unlock_irqrestore(&ucs->lock, flags);
1419 * submit a control output request without message buffer to the Gigaset base
1420 * and optionally start a timeout
1422 * bcs B channel control structure
1423 * req control request code (HD_*)
1424 * val control request parameter value (set to 0 if unused)
1425 * timeout timeout in seconds (0: no timeout)
1428 * -EBUSY if another request is pending
1429 * any URB submission error code
1431 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1433 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1435 unsigned long flags;
1437 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1439 spin_lock_irqsave(&ucs->lock, flags);
1441 spin_unlock_irqrestore(&ucs->lock, flags);
1442 dev_err(bcs->cs->dev,
1443 "submission of request 0x%02x failed: "
1444 "request 0x%02x still pending\n",
1449 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1450 ucs->dr_ctrl.bRequest = req;
1451 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1452 ucs->dr_ctrl.wIndex = 0;
1453 ucs->dr_ctrl.wLength = 0;
1454 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1455 usb_sndctrlpipe(ucs->udev, 0),
1456 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1457 write_ctrl_callback, ucs);
1458 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1459 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1460 req, get_usb_statmsg(ret));
1461 spin_unlock_irqrestore(&ucs->lock, flags);
1467 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1468 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1469 ucs->timer_ctrl.data = (unsigned long) bcs;
1470 ucs->timer_ctrl.function = req_timeout;
1471 add_timer(&ucs->timer_ctrl);
1474 spin_unlock_irqrestore(&ucs->lock, flags);
1478 /* gigaset_init_bchannel
1479 * called by common.c to connect a B channel
1480 * initialize isochronous I/O and tell the Gigaset base to open the channel
1482 * B channel control structure
1484 * 0 on success, error code < 0 on error
1486 static int gigaset_init_bchannel(struct bc_state *bcs)
1489 unsigned long flags;
1491 spin_lock_irqsave(&bcs->cs->lock, flags);
1492 if (unlikely(!bcs->cs->connected)) {
1493 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1494 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1498 if ((ret = starturbs(bcs)) < 0) {
1499 dev_err(bcs->cs->dev,
1500 "could not start isochronous I/O for channel B%d: %s\n",
1502 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1505 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1509 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1510 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1511 dev_err(bcs->cs->dev, "could not open channel B%d\n",
1513 stopurbs(bcs->hw.bas);
1518 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1522 /* gigaset_close_bchannel
1523 * called by common.c to disconnect a B channel
1524 * tell the Gigaset base to close the channel
1525 * stopping isochronous I/O and LL notification will be done when the
1526 * acknowledgement for the close arrives
1528 * B channel control structure
1530 * 0 on success, error code < 0 on error
1532 static int gigaset_close_bchannel(struct bc_state *bcs)
1535 unsigned long flags;
1537 spin_lock_irqsave(&bcs->cs->lock, flags);
1538 if (unlikely(!bcs->cs->connected)) {
1539 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1540 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1544 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1545 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1546 /* channel not running: just signal common.c */
1547 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1548 gigaset_bchannel_down(bcs);
1552 /* channel running: tell device to close it */
1553 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1554 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1555 dev_err(bcs->cs->dev, "closing channel B%d failed\n",
1558 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1562 /* Device Operations */
1563 /* ================= */
1566 * unqueue first command buffer from queue, waking any sleepers
1567 * must be called with cs->cmdlock held
1569 * cs controller state structure
1571 static void complete_cb(struct cardstate *cs)
1573 struct cmdbuf_t *cb = cs->cmdbuf;
1575 /* unqueue completed buffer */
1576 cs->cmdbytes -= cs->curlen;
1577 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1578 "write_command: sent %u bytes, %u left",
1579 cs->curlen, cs->cmdbytes);
1580 if ((cs->cmdbuf = cb->next) != NULL) {
1581 cs->cmdbuf->prev = NULL;
1582 cs->curlen = cs->cmdbuf->len;
1584 cs->lastcmdbuf = NULL;
1588 if (cb->wake_tasklet)
1589 tasklet_schedule(cb->wake_tasklet);
1594 /* write_command_callback
1595 * USB completion handler for AT command transmission
1596 * called by the USB subsystem in interrupt context
1598 * urb USB request block of completed request
1599 * urb->context = controller state structure
1601 static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1603 struct cardstate *cs = urb->context;
1604 struct bas_cardstate *ucs = cs->hw.bas;
1605 unsigned long flags;
1607 update_basstate(ucs, 0, BS_ATWRPEND);
1610 switch (urb->status) {
1611 case 0: /* normal completion */
1613 case -ENOENT: /* cancelled */
1614 case -ECONNRESET: /* cancelled (async) */
1615 case -EINPROGRESS: /* pending */
1616 case -ENODEV: /* device removed */
1617 case -ESHUTDOWN: /* device shut down */
1618 /* ignore silently */
1619 gig_dbg(DEBUG_USBREQ, "%s: %s",
1620 __func__, get_usb_statmsg(urb->status));
1622 default: /* any failure */
1623 if (++ucs->retry_cmd_out > BAS_RETRY) {
1625 "command write: %s, "
1626 "giving up after %d retries\n",
1627 get_usb_statmsg(urb->status),
1628 ucs->retry_cmd_out);
1631 if (cs->cmdbuf == NULL) {
1633 "command write: %s, "
1634 "cannot retry - cmdbuf gone\n",
1635 get_usb_statmsg(urb->status));
1638 dev_notice(cs->dev, "command write: %s, retry %d\n",
1639 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1640 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1641 /* resubmitted - bypass regular exit block */
1643 /* command send failed, assume base still waiting */
1644 update_basstate(ucs, BS_ATREADY, 0);
1647 spin_lock_irqsave(&cs->cmdlock, flags);
1648 if (cs->cmdbuf != NULL)
1650 spin_unlock_irqrestore(&cs->cmdlock, flags);
1654 * timeout routine for AT command transmission
1656 * controller state structure
1658 static void atrdy_timeout(unsigned long data)
1660 struct cardstate *cs = (struct cardstate *) data;
1661 struct bas_cardstate *ucs = cs->hw.bas;
1663 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1665 /* fake the missing signal - what else can I do? */
1666 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1671 * submit an HD_WRITE_ATMESSAGE command URB
1673 * cs controller state structure
1674 * buf buffer containing command to send
1675 * len length of command to send
1678 * -EBUSY if another request is pending
1679 * any URB submission error code
1681 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1683 struct bas_cardstate *ucs = cs->hw.bas;
1686 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1688 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1690 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1694 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1695 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1696 ucs->dr_cmd_out.wValue = 0;
1697 ucs->dr_cmd_out.wIndex = 0;
1698 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1699 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1700 usb_sndctrlpipe(ucs->udev, 0),
1701 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1702 write_command_callback, cs);
1703 rc = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC);
1705 update_basstate(ucs, 0, BS_ATWRPEND);
1706 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1711 /* submitted successfully, start timeout if necessary */
1712 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1713 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1715 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1716 ucs->timer_atrdy.data = (unsigned long) cs;
1717 ucs->timer_atrdy.function = atrdy_timeout;
1718 add_timer(&ucs->timer_atrdy);
1724 * start transmission of AT command queue if necessary
1726 * cs controller state structure
1729 * error code < 0 on error
1731 static int start_cbsend(struct cardstate *cs)
1733 struct cmdbuf_t *cb;
1734 struct bas_cardstate *ucs = cs->hw.bas;
1735 unsigned long flags;
1739 /* check if AT channel is open */
1740 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1741 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1742 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1744 /* flush command queue */
1745 spin_lock_irqsave(&cs->cmdlock, flags);
1746 while (cs->cmdbuf != NULL)
1748 spin_unlock_irqrestore(&cs->cmdlock, flags);
1753 /* try to send first command in queue */
1754 spin_lock_irqsave(&cs->cmdlock, flags);
1756 while ((cb = cs->cmdbuf) != NULL &&
1757 atomic_read(&ucs->basstate) & BS_ATREADY) {
1758 ucs->retry_cmd_out = 0;
1759 rc = atwrite_submit(cs, cb->buf, cb->len);
1766 spin_unlock_irqrestore(&cs->cmdlock, flags);
1770 /* gigaset_write_cmd
1771 * This function is called by the device independent part of the driver
1772 * to transmit an AT command string to the Gigaset device.
1773 * It encapsulates the device specific method for transmission over the
1774 * direct USB connection to the base.
1775 * The command string is added to the queue of commands to send, and
1776 * USB transmission is started if necessary.
1778 * cs controller state structure
1779 * buf command string to send
1780 * len number of bytes to send (max. IF_WRITEBUF)
1781 * wake_tasklet tasklet to run when transmission is completed
1784 * number of bytes queued on success
1785 * error code < 0 on error
1787 static int gigaset_write_cmd(struct cardstate *cs,
1788 const unsigned char *buf, int len,
1789 struct tasklet_struct *wake_tasklet)
1791 struct cmdbuf_t *cb;
1792 unsigned long flags;
1795 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1796 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1797 "CMD Transmit", len, buf);
1800 return 0; /* nothing to do */
1802 if (len > IF_WRITEBUF)
1804 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1805 dev_err(cs->dev, "%s: out of memory\n", __func__);
1809 memcpy(cb->buf, buf, len);
1813 cb->wake_tasklet = wake_tasklet;
1815 spin_lock_irqsave(&cs->cmdlock, flags);
1816 cb->prev = cs->lastcmdbuf;
1818 cs->lastcmdbuf->next = cb;
1823 cs->cmdbytes += len;
1824 cs->lastcmdbuf = cb;
1825 spin_unlock_irqrestore(&cs->cmdlock, flags);
1827 spin_lock_irqsave(&cs->lock, flags);
1828 if (unlikely(!cs->connected)) {
1829 spin_unlock_irqrestore(&cs->lock, flags);
1830 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1833 status = start_cbsend(cs);
1834 spin_unlock_irqrestore(&cs->lock, flags);
1835 return status < 0 ? status : len;
1838 /* gigaset_write_room
1839 * tty_driver.write_room interface routine
1840 * return number of characters the driver will accept to be written via
1843 * controller state structure
1845 * number of characters
1847 static int gigaset_write_room(struct cardstate *cs)
1852 /* gigaset_chars_in_buffer
1853 * tty_driver.chars_in_buffer interface routine
1854 * return number of characters waiting to be sent
1856 * controller state structure
1858 * number of characters
1860 static int gigaset_chars_in_buffer(struct cardstate *cs)
1862 unsigned long flags;
1865 spin_lock_irqsave(&cs->cmdlock, flags);
1866 bytes = cs->cmdbytes;
1867 spin_unlock_irqrestore(&cs->cmdlock, flags);
1873 * implementation of ioctl(GIGASET_BRKCHARS)
1875 * controller state structure
1877 * -EINVAL (unimplemented function)
1879 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1885 /* Device Initialization/Shutdown */
1886 /* ============================== */
1888 /* Free hardware dependent part of the B channel structure
1890 * bcs B channel structure
1894 static int gigaset_freebcshw(struct bc_state *bcs)
1896 struct bas_bc_state *ubc = bcs->hw.bas;
1902 /* kill URBs and tasklets before freeing - better safe than sorry */
1903 atomic_set(&ubc->running, 0);
1904 for (i = 0; i < BAS_OUTURBS; ++i)
1905 if (ubc->isoouturbs[i].urb) {
1906 gig_dbg(DEBUG_INIT, "%s: killing iso out URB %d",
1908 usb_kill_urb(ubc->isoouturbs[i].urb);
1909 usb_free_urb(ubc->isoouturbs[i].urb);
1911 for (i = 0; i < BAS_INURBS; ++i)
1912 if (ubc->isoinurbs[i]) {
1913 gig_dbg(DEBUG_INIT, "%s: killing iso in URB %d",
1915 usb_kill_urb(ubc->isoinurbs[i]);
1916 usb_free_urb(ubc->isoinurbs[i]);
1918 tasklet_kill(&ubc->sent_tasklet);
1919 tasklet_kill(&ubc->rcvd_tasklet);
1920 kfree(ubc->isooutbuf);
1926 /* Initialize hardware dependent part of the B channel structure
1928 * bcs B channel structure
1932 static int gigaset_initbcshw(struct bc_state *bcs)
1935 struct bas_bc_state *ubc;
1937 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1939 err("could not allocate bas_bc_state");
1943 atomic_set(&ubc->running, 0);
1944 atomic_set(&ubc->corrbytes, 0);
1945 spin_lock_init(&ubc->isooutlock);
1946 for (i = 0; i < BAS_OUTURBS; ++i) {
1947 ubc->isoouturbs[i].urb = NULL;
1948 ubc->isoouturbs[i].bcs = bcs;
1950 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1952 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1953 err("could not allocate isochronous output buffer");
1958 tasklet_init(&ubc->sent_tasklet,
1959 &write_iso_tasklet, (unsigned long) bcs);
1961 spin_lock_init(&ubc->isoinlock);
1962 for (i = 0; i < BAS_INURBS; ++i)
1963 ubc->isoinurbs[i] = NULL;
1964 ubc->isoindone = NULL;
1965 ubc->loststatus = -EINPROGRESS;
1979 tasklet_init(&ubc->rcvd_tasklet,
1980 &read_iso_tasklet, (unsigned long) bcs);
1984 static void gigaset_reinitbcshw(struct bc_state *bcs)
1986 struct bas_bc_state *ubc = bcs->hw.bas;
1988 atomic_set(&bcs->hw.bas->running, 0);
1989 atomic_set(&bcs->hw.bas->corrbytes, 0);
1990 bcs->hw.bas->numsub = 0;
1991 spin_lock_init(&ubc->isooutlock);
1992 spin_lock_init(&ubc->isoinlock);
1993 ubc->loststatus = -EINPROGRESS;
1996 static void gigaset_freecshw(struct cardstate *cs)
1998 /* timers, URBs and rcvbuf are disposed of in disconnect */
2003 static int gigaset_initcshw(struct cardstate *cs)
2005 struct bas_cardstate *ucs;
2007 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2011 ucs->urb_cmd_in = NULL;
2012 ucs->urb_cmd_out = NULL;
2014 ucs->rcvbuf_size = 0;
2016 spin_lock_init(&ucs->lock);
2019 atomic_set(&ucs->basstate, 0);
2020 init_timer(&ucs->timer_ctrl);
2021 init_timer(&ucs->timer_atrdy);
2022 init_timer(&ucs->timer_cmd_in);
2028 * unlink and deallocate all URBs unconditionally
2029 * caller must make sure that no commands are still in progress
2031 * cs controller state structure
2033 static void freeurbs(struct cardstate *cs)
2035 struct bas_cardstate *ucs = cs->hw.bas;
2036 struct bas_bc_state *ubc;
2039 for (j = 0; j < 2; ++j) {
2040 ubc = cs->bcs[j].hw.bas;
2041 for (i = 0; i < BAS_OUTURBS; ++i)
2042 if (ubc->isoouturbs[i].urb) {
2043 usb_kill_urb(ubc->isoouturbs[i].urb);
2045 "%s: isoc output URB %d/%d unlinked",
2047 usb_free_urb(ubc->isoouturbs[i].urb);
2048 ubc->isoouturbs[i].urb = NULL;
2050 for (i = 0; i < BAS_INURBS; ++i)
2051 if (ubc->isoinurbs[i]) {
2052 usb_kill_urb(ubc->isoinurbs[i]);
2054 "%s: isoc input URB %d/%d unlinked",
2056 usb_free_urb(ubc->isoinurbs[i]);
2057 ubc->isoinurbs[i] = NULL;
2060 if (ucs->urb_int_in) {
2061 usb_kill_urb(ucs->urb_int_in);
2062 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2064 usb_free_urb(ucs->urb_int_in);
2065 ucs->urb_int_in = NULL;
2067 if (ucs->urb_cmd_out) {
2068 usb_kill_urb(ucs->urb_cmd_out);
2069 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2071 usb_free_urb(ucs->urb_cmd_out);
2072 ucs->urb_cmd_out = NULL;
2074 if (ucs->urb_cmd_in) {
2075 usb_kill_urb(ucs->urb_cmd_in);
2076 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2078 usb_free_urb(ucs->urb_cmd_in);
2079 ucs->urb_cmd_in = NULL;
2081 if (ucs->urb_ctrl) {
2082 usb_kill_urb(ucs->urb_ctrl);
2083 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2085 usb_free_urb(ucs->urb_ctrl);
2086 ucs->urb_ctrl = NULL;
2091 * This function is called when a new USB device is connected.
2092 * It checks whether the new device is handled by this driver.
2094 static int gigaset_probe(struct usb_interface *interface,
2095 const struct usb_device_id *id)
2097 struct usb_host_interface *hostif;
2098 struct usb_device *udev = interface_to_usbdev(interface);
2099 struct cardstate *cs = NULL;
2100 struct bas_cardstate *ucs = NULL;
2101 struct bas_bc_state *ubc;
2102 struct usb_endpoint_descriptor *endpoint;
2107 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2108 __func__, le16_to_cpu(udev->descriptor.idVendor),
2109 le16_to_cpu(udev->descriptor.idProduct));
2111 /* set required alternate setting */
2112 hostif = interface->cur_altsetting;
2113 if (hostif->desc.bAlternateSetting != 3) {
2115 "%s: wrong alternate setting %d - trying to switch",
2116 __func__, hostif->desc.bAlternateSetting);
2117 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2118 dev_warn(&udev->dev, "usb_set_interface failed, "
2119 "device %d interface %d altsetting %d\n",
2120 udev->devnum, hostif->desc.bInterfaceNumber,
2121 hostif->desc.bAlternateSetting);
2124 hostif = interface->cur_altsetting;
2127 /* Reject application specific interfaces
2129 if (hostif->desc.bInterfaceClass != 255) {
2130 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2131 __func__, hostif->desc.bInterfaceClass);
2135 dev_info(&udev->dev,
2136 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2137 __func__, le16_to_cpu(udev->descriptor.idVendor),
2138 le16_to_cpu(udev->descriptor.idProduct));
2140 cs = gigaset_getunassignedcs(driver);
2142 dev_err(&udev->dev, "no free cardstate\n");
2147 /* save off device structure ptrs for later use */
2150 ucs->interface = interface;
2151 cs->dev = &interface->dev;
2154 * - one for the interrupt pipe
2155 * - three for the different uses of the default control pipe
2156 * - three for each isochronous pipe
2158 if (!(ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2159 !(ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2160 !(ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL)) ||
2161 !(ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL)))
2164 for (j = 0; j < 2; ++j) {
2165 ubc = cs->bcs[j].hw.bas;
2166 for (i = 0; i < BAS_OUTURBS; ++i)
2167 if (!(ubc->isoouturbs[i].urb =
2168 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2170 for (i = 0; i < BAS_INURBS; ++i)
2171 if (!(ubc->isoinurbs[i] =
2172 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2177 ucs->rcvbuf_size = 0;
2179 /* Fill the interrupt urb and send it to the core */
2180 endpoint = &hostif->endpoint[0].desc;
2181 usb_fill_int_urb(ucs->urb_int_in, udev,
2182 usb_rcvintpipe(udev,
2183 (endpoint->bEndpointAddress) & 0x0f),
2184 ucs->int_in_buf, 3, read_int_callback, cs,
2185 endpoint->bInterval);
2186 if ((rc = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL)) != 0) {
2187 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2192 /* tell the device that the driver is ready */
2193 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2196 /* tell common part that the device is ready */
2197 if (startmode == SM_LOCKED)
2198 atomic_set(&cs->mstate, MS_LOCKED);
2200 /* save address of controller structure */
2201 usb_set_intfdata(interface, cs);
2203 if (!gigaset_start(cs))
2209 dev_err(cs->dev, "could not allocate URBs\n");
2212 usb_set_intfdata(interface, NULL);
2213 gigaset_unassign(cs);
2217 /* gigaset_disconnect
2218 * This function is called when the Gigaset base is unplugged.
2220 static void gigaset_disconnect(struct usb_interface *interface)
2222 struct cardstate *cs;
2223 struct bas_cardstate *ucs;
2226 cs = usb_get_intfdata(interface);
2230 dev_info(cs->dev, "disconnecting Gigaset base\n");
2232 /* mark base as not ready, all channels disconnected */
2233 atomic_set(&ucs->basstate, 0);
2235 /* tell LL all channels are down */
2236 //FIXME shouldn't gigaset_stop() do this?
2237 for (j = 0; j < 2; ++j)
2238 gigaset_bchannel_down(cs->bcs + j);
2240 /* stop driver (common part) */
2243 /* stop timers and URBs, free ressources */
2244 del_timer_sync(&ucs->timer_ctrl);
2245 del_timer_sync(&ucs->timer_atrdy);
2246 del_timer_sync(&ucs->timer_cmd_in);
2248 usb_set_intfdata(interface, NULL);
2251 ucs->rcvbuf_size = 0;
2252 usb_put_dev(ucs->udev);
2253 ucs->interface = NULL;
2256 gigaset_unassign(cs);
2259 static struct gigaset_ops gigops = {
2262 gigaset_chars_in_buffer,
2264 gigaset_init_bchannel,
2265 gigaset_close_bchannel,
2268 gigaset_reinitbcshw,
2271 gigaset_set_modem_ctrl,
2273 gigaset_set_line_ctrl,
2274 gigaset_isoc_send_skb,
2279 * This function is called after the kernel module is loaded.
2281 static int __init bas_gigaset_init(void)
2285 /* allocate memory for our driver state and intialize it */
2286 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2287 GIGASET_MODULENAME, GIGASET_DEVNAME,
2288 GIGASET_DEVFSNAME, &gigops,
2289 THIS_MODULE)) == NULL)
2292 /* allocate memory for our device state and intialize it */
2293 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2294 GIGASET_MODULENAME);
2298 /* register this driver with the USB subsystem */
2299 result = usb_register(&gigaset_usb_driver);
2301 err("usb_register failed (error %d)", -result);
2305 info(DRIVER_AUTHOR);
2309 error: if (cardstate)
2310 gigaset_freecs(cardstate);
2313 gigaset_freedriver(driver);
2319 * This function is called before the kernel module is unloaded.
2321 static void __exit bas_gigaset_exit(void)
2323 struct bas_cardstate *ucs = cardstate->hw.bas;
2325 gigaset_blockdriver(driver); /* => probe will fail
2326 * => no gigaset_start any more
2329 gigaset_shutdown(cardstate);
2330 /* from now on, no isdn callback should be possible */
2332 /* close all still open channels */
2333 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2334 gig_dbg(DEBUG_INIT, "closing B1 channel");
2335 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2336 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2337 NULL, 0, BAS_TIMEOUT);
2339 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2340 gig_dbg(DEBUG_INIT, "closing B2 channel");
2341 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2342 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2343 NULL, 0, BAS_TIMEOUT);
2345 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2346 gig_dbg(DEBUG_INIT, "closing AT channel");
2347 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2348 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2349 NULL, 0, BAS_TIMEOUT);
2351 atomic_set(&ucs->basstate, 0);
2353 /* deregister this driver with the USB subsystem */
2354 usb_deregister(&gigaset_usb_driver);
2355 /* this will call the disconnect-callback */
2356 /* from now on, no disconnect/probe callback should be running */
2358 gigaset_freecs(cardstate);
2360 gigaset_freedriver(driver);
2365 module_init(bas_gigaset_init);
2366 module_exit(bas_gigaset_exit);
2368 MODULE_AUTHOR(DRIVER_AUTHOR);
2369 MODULE_DESCRIPTION(DRIVER_DESC);
2370 MODULE_LICENSE("GPL");