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