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