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