blob: 20d0a080a2b0d521ace124c8b1206b0298b7d55e [file] [log] [blame]
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001/*
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>,
Tilman Schmidt70440cf2006-04-10 22:55:14 -07006 * Stefan Eilers.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08007 *
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08008 * =====================================================================
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 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080014 */
15
16#include "gigaset.h"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080017#include <linux/usb.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20
21/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070022#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080023#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"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080039#define GIGASET_DEVNAME "ttyGB"
40
Tilman Schmidt73a88812006-04-22 02:35:30 -070041/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
42#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080043
Tilman Schmidt170ebf82009-03-18 23:44:23 -070044/* interrupt pipe message size according to ibid. ch. 2.2 */
45#define IP_MSGSIZE 3
46
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080047/* Values for the Gigaset 307x */
48#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070049#define USB_3070_PRODUCT_ID 0x0001
50#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080051#define USB_SX303_PRODUCT_ID 0x0021
52#define USB_SX353_PRODUCT_ID 0x0022
53
54/* table of devices that work with this driver */
Tilman Schmidt7891adf2009-10-25 09:30:37 +000055static const struct usb_device_id gigaset_table[] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070056 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
57 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080058 { 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
Tilman Schmidt06163f82006-06-26 00:25:33 -070065/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080066
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068static 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
Tilman Schmidt024fd292008-02-06 01:38:26 -080074/* 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
Tilman Schmidt06163f82006-06-26 00:25:33 -070082static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070083static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070084static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070085static int atwrite_submit(struct cardstate *, unsigned char *, int);
86static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080087
Tilman Schmidt06163f82006-06-26 00:25:33 -070088/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089
90struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070091 struct usb_device *udev; /* USB device pointer */
Kees Cook33ad61d2017-10-20 13:47:08 -070092 struct cardstate *cs;
Tilman Schmidt784d5852006-04-10 22:55:04 -070093 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080094 unsigned char minor; /* starting minor number */
95
Tilman Schmidt784d5852006-04-10 22:55:04 -070096 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080097 struct usb_ctrlrequest dr_ctrl;
98 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070099 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800100
101 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700102 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800103 struct usb_ctrlrequest dr_cmd_out;
104 int retry_cmd_out;
105
Tilman Schmidt784d5852006-04-10 22:55:04 -0700106 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800107 struct usb_ctrlrequest dr_cmd_in;
108 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700109 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800110
Tilman Schmidt784d5852006-04-10 22:55:04 -0700111 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700112 unsigned char *int_in_buf;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000113 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;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800116
117 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800118 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800119 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800120 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800121 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) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700127#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 */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800135#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000136#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800137
138
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000139static struct gigaset_driver *driver;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800140
141/* usb specific object needed to register this driver with the usb subsystem */
142static struct usb_driver gigaset_usb_driver = {
143 .name = GIGASET_MODULENAME,
144 .probe = gigaset_probe,
145 .disconnect = gigaset_disconnect,
146 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800147 .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,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700152 .disable_hub_initiated_lpm = 1,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800153};
154
Tilman Schmidt73a88812006-04-22 02:35:30 -0700155/* get message text for usb_submit_urb return code
156 */
157static 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:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000177 return "too many isoc frames requested";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700178 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800196/* get message text for USB status code
197 */
198static 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:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700206 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800207 case -EINPROGRESS:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000208 return "URB still pending";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 case -EPROTO:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000210 return "bitstuff error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800211 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700212 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700213 case -ETIME:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000214 return "USB response timeout";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700215 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:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000222 return "endpoint babble";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800223 case -EREMOTEIO:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000224 return "short packet";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700225 case -ENODEV:
226 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800227 case -EXDEV:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000228 return "partial isoc transfer";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800229 case -EINVAL:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000230 return "ISO madness";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 case -ECONNRESET:
232 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800233 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700234 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800235 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800237 return unkmsg;
238 }
239}
240
241/* usb_pipetype_str
242 * retrieve string representation of USB pipe type
243 */
244static 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 */
260static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700261 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800262{
263#ifdef CONFIG_GIGASET_DEBUG
264 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800266 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700267 gig_dbg(level,
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700270 (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",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800274 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700275 urb->transfer_flags);
276 gig_dbg(level,
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800278 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800281 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700282 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);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800292 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700295 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);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800300 }
301 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700302 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800303#endif
304}
305
306/* read/set modem control bits etc. (m10x only) */
307static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700308 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800309{
310 return -EINVAL;
311}
312
313static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
318static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
319{
320 return -EINVAL;
321}
322
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000323/* set/clear bits in base connection state, return previous state
324 */
325static 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800338/* 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.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700342 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800343 * argument:
344 * B channel control structure
345 */
346static inline void error_hangup(struct bc_state *bcs)
347{
348 struct cardstate *cs = bcs->cs;
349
Tilman Schmidt1528b182010-02-22 13:09:52 +0000350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800351 gigaset_schedule_event(cs);
352}
353
354/* error_reset
355 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700356 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700357 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt60798c682010-09-30 13:35:21 +0000358 * cs->hw.bas->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800359 * argument:
360 * controller state structure
361 */
362static inline void error_reset(struct cardstate *cs)
363{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000364 /* reset interrupt pipe to recover (ignore errors) */
365 update_basstate(cs->hw.bas, BS_RESETTING, 0);
Tilman Schmidt60798c682010-09-30 13:35:21 +0000366 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);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800369}
370
371/* check_pending
372 * check for completion of pending control request
373 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700374 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800375 */
376static void check_pending(struct bas_cardstate *ucs)
377{
378 unsigned long flags;
379
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800380 spin_lock_irqsave(&ucs->lock, flags);
381 switch (ucs->pending) {
382 case 0:
383 break;
384 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800385 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800386 ucs->pending = 0;
387 break;
388 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800389 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800390 ucs->pending = 0;
391 break;
392 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800393 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800394 ucs->pending = 0;
395 break;
396 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800397 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800398 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800399 break;
400 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800401 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800402 ucs->pending = 0;
403 break;
404 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800405 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800406 ucs->pending = 0;
407 break;
408 case HD_DEVICE_INIT_ACK: /* no reply expected */
409 ucs->pending = 0;
410 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000411 case HD_RESET_INTERRUPT_PIPE:
412 if (!(ucs->basstate & BS_RESETTING))
413 ucs->pending = 0;
414 break;
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000415 /*
416 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
417 * and should never end up here
418 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800419 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700420 dev_warn(&ucs->interface->dev,
421 "unknown pending request 0x%02x cleared\n",
422 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800423 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 */
Kees Cook4cfea082017-10-16 17:29:16 -0700437static void cmd_in_timeout(struct timer_list *t)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800438{
Kees Cook4cfea082017-10-16 17:29:16 -0700439 struct bas_cardstate *ucs = from_timer(ucs, t, timer_cmd_in);
Kees Cook33ad61d2017-10-20 13:47:08 -0700440 struct cardstate *cs = ucs->cs;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700441 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800442
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800443 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700444 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800445 return;
446 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800447
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000448 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700449 dev_err(cs->dev,
450 "control read: timeout, giving up after %d tries\n",
451 ucs->retry_cmd_in);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000452 kfree(ucs->rcvbuf);
453 ucs->rcvbuf = NULL;
454 ucs->rcvbuf_size = 0;
455 error_reset(cs);
456 return;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700457 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000458
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 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800469}
470
Tilman Schmidt06163f82006-06-26 00:25:33 -0700471/* 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 */
David Howells7d12e782006-10-05 14:55:46 +0100478static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700479{
480 struct inbuf_t *inbuf = urb->context;
481 struct cardstate *cs = inbuf->cs;
482 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800483 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700484 unsigned numbytes;
485 int rc;
486
487 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800488 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700489 del_timer(&ucs->timer_cmd_in);
490
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800491 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700492 case 0: /* normal completion */
493 numbytes = urb->actual_length;
494 if (unlikely(numbytes != ucs->rcvbuf_size)) {
495 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800496 "control read: received %d chars, expected %d\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700497 numbytes, ucs->rcvbuf_size);
498 if (numbytes > ucs->rcvbuf_size)
499 numbytes = ucs->rcvbuf_size;
500 }
501
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000502 /* 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);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700506 }
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 */
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000514 /* no further action necessary */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700515 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800516 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700517 break;
518
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000519 default: /* other errors: retry */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700520 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000521 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
522 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700523 rc = atread_submit(cs, BAS_TIMEOUT);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000524 if (rc >= 0)
525 /* successfully resubmitted, skip freeing */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700526 return;
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000527 if (rc == -ENODEV)
528 /* disconnect, no further action necessary */
529 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700530 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000531 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
532 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700533 error_reset(cs);
534 }
535
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000536 /* read finished, free buffer */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700537 kfree(ucs->rcvbuf);
538 ucs->rcvbuf = NULL;
539 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700540}
541
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800542/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700543 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800544 * parameters:
545 * cs controller state structure
546 * timeout timeout in 1/10 sec., 0: none
547 * return value:
548 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800549 * -EBUSY if another request is pending
550 * any URB submission error code
551 */
552static int atread_submit(struct cardstate *cs, int timeout)
553{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700554 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800555 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800556 int ret;
557
Tilman Schmidt784d5852006-04-10 22:55:04 -0700558 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
559 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800560
Tilman Schmidt024fd292008-02-06 01:38:26 -0800561 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
562 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700563 dev_err(cs->dev,
564 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800565 return -EBUSY;
566 }
567
Tilman Schmidt024fd292008-02-06 01:38:26 -0800568 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800577 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,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700583 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000584 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700585 ucs->rcvbuf, ucs->rcvbuf_size,
586 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800587
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000588 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
589 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700590 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700591 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700592 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800593 return ret;
594 }
595
596 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700597 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +0000598 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800599 }
600 return 0;
601}
602
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000603/* int_in_work
604 * workqueue routine to clear halt on interrupt in endpoint
605 */
606
607static 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);
Tilman Schmidtc6fdd8e2012-10-24 08:44:32 +0000621
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 */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000628 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 */
Kees Cook4cfea082017-10-16 17:29:16 -0700643static void int_in_resubmit(struct timer_list *t)
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000644{
Kees Cook4cfea082017-10-16 17:29:16 -0700645 struct bas_cardstate *ucs = from_timer(ucs, t, timer_int_in);
Kees Cook33ad61d2017-10-20 13:47:08 -0700646 struct cardstate *cs = ucs->cs;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000647 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800665/* 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 */
David Howells7d12e782006-10-05 14:55:46 +0100672static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800673{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700674 struct cardstate *cs = urb->context;
675 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800676 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800677 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800678 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700679 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800680 unsigned l;
681 int channel;
682
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800683 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800684 case 0: /* success */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000685 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800686 break;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000687 case -EPIPE: /* endpoint stalled */
688 schedule_work(&ucs->int_in_wq);
689 /* fall through */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700690 case -ENOENT: /* cancelled */
691 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800692 case -EINPROGRESS: /* pending */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000693 case -ENODEV: /* device removed */
694 case -ESHUTDOWN: /* device shut down */
695 /* no further action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700696 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800697 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800698 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000699 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);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700706 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000707 default: /* other errors: just resubmit */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700708 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800709 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800710 goto resubmit;
711 }
712
Tilman Schmidt73a88812006-04-22 02:35:30 -0700713 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700714 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700715 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
716 urb->actual_length);
717 goto resubmit;
718 }
719
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800720 l = (unsigned) ucs->int_in_buf[1] +
Joe Perches475be4d2012-02-19 19:52:38 -0800721 (((unsigned) ucs->int_in_buf[2]) << 8);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800722
Tilman Schmidt784d5852006-04-10 22:55:04 -0700723 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]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800726
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 case HD_OPEN_B1CHANNEL_ACK:
743 bcs = cs->bcs + channel;
744 update_basstate(ucs, BS_B1OPEN << channel, 0);
745 gigaset_bchannel_up(bcs);
746 break;
747
748 case HD_OPEN_ATCHANNEL_ACK:
749 update_basstate(ucs, BS_ATOPEN, 0);
750 start_cbsend(cs);
751 break;
752
753 case HD_CLOSE_B2CHANNEL_ACK:
754 ++channel;
755 case HD_CLOSE_B1CHANNEL_ACK:
756 bcs = cs->bcs + channel;
757 update_basstate(ucs, 0, BS_B1OPEN << channel);
758 stopurbs(bcs->hw.bas);
759 gigaset_bchannel_down(bcs);
760 break;
761
762 case HD_CLOSE_ATCHANNEL_ACK:
763 update_basstate(ucs, 0, BS_ATOPEN);
764 break;
765
766 case HD_B2_FLOW_CONTROL:
767 ++channel;
768 case HD_B1_FLOW_CONTROL:
769 bcs = cs->bcs + channel;
770 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700771 &bcs->hw.bas->corrbytes);
772 gig_dbg(DEBUG_ISO,
773 "Flow control (channel %d, sub %d): 0x%02x => %d",
774 channel, bcs->hw.bas->numsub, l,
775 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800776 break;
777
778 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
779 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700780 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800781 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800782 break;
783 }
784 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000785 if (ucs->basstate & BS_ATRDPEND) {
786 spin_unlock_irqrestore(&cs->lock, flags);
787 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800788 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000789 l, ucs->rcvbuf_size);
790 break;
791 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800792 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700793 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700794 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700795 "receive AT data overrun, %d bytes lost\n",
796 ucs->rcvbuf_size);
797 kfree(ucs->rcvbuf);
798 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800799 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000800 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
801 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700803 dev_err(cs->dev, "out of memory receiving AT data\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800804 break;
805 }
806 ucs->rcvbuf_size = l;
807 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000808 rc = atread_submit(cs, BAS_TIMEOUT);
809 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800810 kfree(ucs->rcvbuf);
811 ucs->rcvbuf = NULL;
812 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800813 }
814 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000815 if (rc < 0 && rc != -ENODEV)
816 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800817 break;
818
819 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000820 update_basstate(ucs, 0, BS_RESETTING);
821 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800822 break;
823
824 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700825 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800826 break;
827
828 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700829 dev_warn(cs->dev,
830 "unknown Gigaset signal 0x%02x (%u) ignored\n",
831 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800832 }
833
834 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800835 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800836
837resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800838 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700839 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700840 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700841 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800842 error_reset(cs);
843 }
844}
845
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800846/* read_iso_callback
847 * USB completion handler for B channel isochronous input
848 * called by the USB subsystem in interrupt context
849 * parameter:
850 * urb USB request block of completed request
851 * urb->context = bc_state structure
852 */
David Howells7d12e782006-10-05 14:55:46 +0100853static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800854{
855 struct bc_state *bcs;
856 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800857 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800858 unsigned long flags;
859 int i, rc;
860
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800861 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800862 if (unlikely(status == -ENOENT ||
863 status == -ECONNRESET ||
864 status == -EINPROGRESS ||
865 status == -ENODEV ||
866 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700867 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800868 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869 return;
870 }
871
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700872 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800873 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800874
875 spin_lock_irqsave(&ubc->isoinlock, flags);
876 if (likely(ubc->isoindone == NULL)) {
877 /* pass URB to tasklet */
878 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800879 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000880 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800881 } else {
882 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000883 gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800884 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800885 for (i = 0; i < BAS_NUMFRAMES; i++) {
886 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
887 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000888 urb->iso_frame_desc[i].status != -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800889 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800890 urb->iso_frame_desc[i].status = 0;
891 urb->iso_frame_desc[i].actual_length = 0;
892 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800893 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700894 /* urb->dev is clobbered by USB subsystem */
895 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800896 urb->transfer_flags = URB_ISO_ASAP;
897 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800898 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700899 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700900 dev_err(bcs->cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800901 "could not resubmit isoc read URB: %s\n",
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000902 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800903 dump_urb(DEBUG_ISO, "isoc read", urb);
904 error_hangup(bcs);
905 }
906 }
907 }
908 spin_unlock_irqrestore(&ubc->isoinlock, flags);
909}
910
911/* write_iso_callback
912 * USB completion handler for B channel isochronous output
913 * called by the USB subsystem in interrupt context
914 * parameter:
915 * urb USB request block of completed request
916 * urb->context = isow_urbctx_t structure
917 */
David Howells7d12e782006-10-05 14:55:46 +0100918static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800919{
920 struct isow_urbctx_t *ucx;
921 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800922 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800923 unsigned long flags;
924
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800925 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800926 if (unlikely(status == -ENOENT ||
927 status == -ECONNRESET ||
928 status == -EINPROGRESS ||
929 status == -ENODEV ||
930 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700931 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800932 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800933 return;
934 }
935
936 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700937 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800938 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800939 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940
941 spin_lock_irqsave(&ubc->isooutlock, flags);
942 ubc->isooutovfl = ubc->isooutdone;
943 ubc->isooutdone = ucx;
944 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000945 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800946}
947
948/* starturbs
949 * prepare and submit USB request blocks for isochronous input and output
950 * argument:
951 * B channel control structure
952 * return value:
953 * 0 on success
954 * < 0 on error (no URBs submitted)
955 */
956static int starturbs(struct bc_state *bcs)
957{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700958 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800959 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800960 int j, k;
961 int rc;
962
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800963 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000964 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800965 bcs->inputstate |= INS_flag_hunt;
966
967 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800968 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800969 for (k = 0; k < BAS_INURBS; k++) {
970 urb = ubc->isoinurbs[k];
971 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800972 rc = -EFAULT;
973 goto error;
974 }
975
976 urb->dev = bcs->cs->hw.bas->udev;
977 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
978 urb->transfer_flags = URB_ISO_ASAP;
979 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
980 urb->transfer_buffer_length = BAS_INBUFSIZE;
981 urb->number_of_packets = BAS_NUMFRAMES;
982 urb->interval = BAS_FRAMETIME;
983 urb->complete = read_iso_callback;
984 urb->context = bcs;
985 for (j = 0; j < BAS_NUMFRAMES; j++) {
986 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
987 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
988 urb->iso_frame_desc[j].status = 0;
989 urb->iso_frame_desc[j].actual_length = 0;
990 }
991
992 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000993 rc = usb_submit_urb(urb, GFP_ATOMIC);
994 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800995 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800996 }
997
998 /* initialize L2 transmission */
999 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
1000
1001 /* set up isochronous output URBs for flag idling */
1002 for (k = 0; k < BAS_OUTURBS; ++k) {
1003 urb = ubc->isoouturbs[k].urb;
1004 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001005 rc = -EFAULT;
1006 goto error;
1007 }
1008 urb->dev = bcs->cs->hw.bas->udev;
1009 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
1010 urb->transfer_flags = URB_ISO_ASAP;
1011 urb->transfer_buffer = ubc->isooutbuf->data;
1012 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1013 urb->number_of_packets = BAS_NUMFRAMES;
1014 urb->interval = BAS_FRAMETIME;
1015 urb->complete = write_iso_callback;
1016 urb->context = &ubc->isoouturbs[k];
1017 for (j = 0; j < BAS_NUMFRAMES; ++j) {
1018 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
1019 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
1020 urb->iso_frame_desc[j].status = 0;
1021 urb->iso_frame_desc[j].actual_length = 0;
1022 }
1023 ubc->isoouturbs[k].limit = -1;
1024 }
1025
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001026 /* keep one URB free, submit the others */
Joe Perches475be4d2012-02-19 19:52:38 -08001027 for (k = 0; k < BAS_OUTURBS - 1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001028 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001029 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001030 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001031 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001032 }
1033 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Joe Perches475be4d2012-02-19 19:52:38 -08001034 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS - 1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001035 ubc->isooutdone = ubc->isooutovfl = NULL;
1036 return 0;
Joe Perches475be4d2012-02-19 19:52:38 -08001037error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001038 stopurbs(ubc);
1039 return rc;
1040}
1041
1042/* stopurbs
1043 * cancel the USB request blocks for isochronous input and output
1044 * errors are silently ignored
1045 * argument:
1046 * B channel control structure
1047 */
1048static void stopurbs(struct bas_bc_state *ubc)
1049{
1050 int k, rc;
1051
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001052 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001053
1054 for (k = 0; k < BAS_INURBS; ++k) {
1055 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001056 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001057 "%s: isoc input URB %d unlinked, result = %s",
1058 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001059 }
1060
1061 for (k = 0; k < BAS_OUTURBS; ++k) {
1062 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001063 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001064 "%s: isoc output URB %d unlinked, result = %s",
1065 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001066 }
1067}
1068
1069/* Isochronous Write - Bottom Half */
1070/* =============================== */
1071
1072/* submit_iso_write_urb
1073 * fill and submit the next isochronous write URB
1074 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001075 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001076 * return value:
1077 * number of frames submitted in URB
1078 * 0 if URB not submitted because no data available (isooutbuf busy)
1079 * error code < 0 on error
1080 */
1081static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1082{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001083 struct urb *urb = ucx->urb;
1084 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001085 struct usb_iso_packet_descriptor *ifd;
1086 int corrbytes, nframe, rc;
1087
Tilman Schmidt784d5852006-04-10 22:55:04 -07001088 /* urb->dev is clobbered by USB subsystem */
1089 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001090 urb->transfer_flags = URB_ISO_ASAP;
1091 urb->transfer_buffer = ubc->isooutbuf->data;
1092 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1093
1094 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1095 ifd = &urb->iso_frame_desc[nframe];
1096
1097 /* compute frame length according to flow control */
1098 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001099 corrbytes = atomic_read(&ubc->corrbytes);
1100 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001101 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1102 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001103 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1104 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1105 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1106 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1107 ifd->length += corrbytes;
1108 atomic_add(-corrbytes, &ubc->corrbytes);
1109 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001110
1111 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001112 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1113 if (rc < 0) {
1114 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001115 gig_dbg(DEBUG_ISO,
1116 "%s: buffer busy at frame %d",
1117 __func__, nframe);
1118 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001119 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001120 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001121 dev_err(ucx->bcs->cs->dev,
1122 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001123 __func__, rc, nframe);
1124 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001125 }
1126 break;
1127 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001128 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001129 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001130 ifd->status = 0;
1131 ifd->actual_length = 0;
1132 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001133 if (unlikely(nframe == 0))
1134 return 0; /* no data to send */
1135 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001136
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001137 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001138 if (unlikely(rc)) {
1139 if (rc == -ENODEV)
1140 /* device removed - give up silently */
1141 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1142 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001143 dev_err(ucx->bcs->cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001144 "could not submit isoc write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001145 get_usb_rcmsg(rc));
1146 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001147 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001148 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001149 return nframe;
1150}
1151
1152/* write_iso_tasklet
1153 * tasklet scheduled when an isochronous output URB from the Gigaset device
1154 * has completed
1155 * parameter:
1156 * data B channel state structure
1157 */
1158static void write_iso_tasklet(unsigned long data)
1159{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001160 struct bc_state *bcs = (struct bc_state *) data;
1161 struct bas_bc_state *ubc = bcs->hw.bas;
1162 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001163 struct isow_urbctx_t *done, *next, *ovfl;
1164 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001165 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001166 struct usb_iso_packet_descriptor *ifd;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001167 unsigned long flags;
1168 int i;
1169 struct sk_buff *skb;
1170 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001171 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001172
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001173 /* loop while completed URBs arrive in time */
1174 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001175 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001176 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001177 return;
1178 }
1179
1180 /* retrieve completed URBs */
1181 spin_lock_irqsave(&ubc->isooutlock, flags);
1182 done = ubc->isooutdone;
1183 ubc->isooutdone = NULL;
1184 ovfl = ubc->isooutovfl;
1185 ubc->isooutovfl = NULL;
1186 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1187 if (ovfl) {
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001188 dev_err(cs->dev, "isoc write underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001189 error_hangup(bcs);
1190 break;
1191 }
1192 if (!done)
1193 break;
1194
1195 /* submit free URB if available */
1196 spin_lock_irqsave(&ubc->isooutlock, flags);
1197 next = ubc->isooutfree;
1198 ubc->isooutfree = NULL;
1199 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1200 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001201 rc = submit_iso_write_urb(next);
1202 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001203 /* could not submit URB, put it back */
1204 spin_lock_irqsave(&ubc->isooutlock, flags);
1205 if (ubc->isooutfree == NULL) {
1206 ubc->isooutfree = next;
1207 next = NULL;
1208 }
1209 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1210 if (next) {
1211 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001212 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001213 "losing isoc write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 error_hangup(bcs);
1215 }
1216 }
1217 }
1218
1219 /* process completed URB */
1220 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001221 status = done->status;
1222 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001223 case -EXDEV: /* partial completion */
1224 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1225 __func__);
1226 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001227 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001228 /* inspect individual frames
1229 * assumptions (for lack of documentation):
1230 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001231 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001232 * - all following frames are not sent at all
1233 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001234 for (i = 0; i < BAS_NUMFRAMES; i++) {
1235 ifd = &urb->iso_frame_desc[i];
1236 if (ifd->status ||
1237 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001238 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001239 "isoc write: frame %d[%d/%d]: %s\n",
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001240 i, ifd->actual_length,
1241 ifd->length,
1242 get_usb_statmsg(ifd->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 break;
1244 }
1245 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001246 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001247 case -EPIPE: /* stall - probably underrun */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001248 dev_err(cs->dev, "isoc write: stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 error_hangup(bcs);
1250 break;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001251 default: /* other errors */
1252 dev_warn(cs->dev, "isoc write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001253 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001254 }
1255
1256 /* mark the write buffer area covered by this URB as free */
1257 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001258 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001259
1260 /* mark URB as free */
1261 spin_lock_irqsave(&ubc->isooutlock, flags);
1262 next = ubc->isooutfree;
1263 ubc->isooutfree = done;
1264 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1265 if (next) {
1266 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001267 rc = submit_iso_write_urb(next);
1268 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001269 /* couldn't submit */
1270 error_hangup(bcs);
1271 }
1272 }
1273 }
1274
1275 /* process queued SKBs */
1276 while ((skb = skb_dequeue(&bcs->squeue))) {
1277 /* copy to output buffer, doing L2 encapsulation */
1278 len = skb->len;
1279 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1280 /* insufficient buffer space, push back onto queue */
1281 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001282 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1283 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001284 break;
1285 }
1286 skb_pull(skb, len);
1287 gigaset_skb_sent(bcs, skb);
1288 dev_kfree_skb_any(skb);
1289 }
1290}
1291
1292/* Isochronous Read - Bottom Half */
1293/* ============================== */
1294
1295/* read_iso_tasklet
1296 * tasklet scheduled when an isochronous input URB from the Gigaset device
1297 * has completed
1298 * parameter:
1299 * data B channel state structure
1300 */
1301static void read_iso_tasklet(unsigned long data)
1302{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001303 struct bc_state *bcs = (struct bc_state *) data;
1304 struct bas_bc_state *ubc = bcs->hw.bas;
1305 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001306 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001307 int status;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001308 struct usb_iso_packet_descriptor *ifd;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001309 char *rcvbuf;
1310 unsigned long flags;
1311 int totleft, numbytes, offset, frame, rc;
1312
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001313 /* loop while more completed URBs arrive in the meantime */
1314 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001315 /* retrieve URB */
1316 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001317 urb = ubc->isoindone;
1318 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001319 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1320 return;
1321 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001322 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001323 ubc->isoindone = NULL;
1324 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001325 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001326 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001327 get_usb_statmsg(ubc->loststatus),
1328 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001329 ubc->loststatus = -EINPROGRESS;
1330 }
1331 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1332
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001333 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001334 gig_dbg(DEBUG_ISO,
1335 "%s: channel not running, "
1336 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001337 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001338 return;
1339 }
1340
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001341 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001342 case 0: /* normal completion */
1343 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001344 case -EXDEV: /* inspect individual frames
1345 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001346 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1347 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001348 break;
1349 case -ENOENT:
1350 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001351 case -EINPROGRESS:
1352 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001353 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001354 continue; /* -> skip */
1355 case -EPIPE:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001356 dev_err(cs->dev, "isoc read: stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001357 error_hangup(bcs);
1358 continue; /* -> skip */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001359 default: /* other error */
1360 dev_warn(cs->dev, "isoc read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001361 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001362 goto error;
1363 }
1364
1365 rcvbuf = urb->transfer_buffer;
1366 totleft = urb->actual_length;
1367 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001368 ifd = &urb->iso_frame_desc[frame];
1369 numbytes = ifd->actual_length;
1370 switch (ifd->status) {
1371 case 0: /* success */
1372 break;
1373 case -EPROTO: /* protocol error or unplug */
1374 case -EILSEQ:
1375 case -ETIME:
1376 /* probably just disconnected, ignore */
1377 gig_dbg(DEBUG_ISO,
1378 "isoc read: frame %d[%d]: %s\n",
1379 frame, numbytes,
1380 get_usb_statmsg(ifd->status));
1381 break;
1382 default: /* other error */
1383 /* report, assume transferred bytes are ok */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001385 "isoc read: frame %d[%d]: %s\n",
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001386 frame, numbytes,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001387 get_usb_statmsg(ifd->status));
1388 }
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001389 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001390 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001391 "isoc read: frame %d[%d]: %s\n",
1392 frame, numbytes,
1393 "exceeds max frame size");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001394 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001395 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001396 "isoc read: frame %d[%d]: %s\n",
1397 frame, numbytes,
1398 "exceeds total transfer length");
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001399 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001400 }
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001401 offset = ifd->offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001402 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001403 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001404 "isoc read: frame %d[%d]: %s\n",
1405 frame, numbytes,
1406 "exceeds end of buffer");
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001407 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001408 }
1409 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1410 totleft -= numbytes;
1411 }
1412 if (unlikely(totleft > 0))
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001413 dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001414 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001415
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001416error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001417 /* URB processed, resubmit */
1418 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1419 urb->iso_frame_desc[frame].status = 0;
1420 urb->iso_frame_desc[frame].actual_length = 0;
1421 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001422 /* urb->dev is clobbered by USB subsystem */
1423 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001424 urb->transfer_flags = URB_ISO_ASAP;
1425 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001426 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001427 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001428 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001429 "could not resubmit isoc read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001430 get_usb_rcmsg(rc));
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001431 dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001432 error_hangup(bcs);
1433 }
1434 }
1435}
1436
1437/* Channel Operations */
1438/* ================== */
1439
1440/* req_timeout
1441 * timeout routine for control output request
1442 * argument:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001443 * controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001444 */
Kees Cook4cfea082017-10-16 17:29:16 -07001445static void req_timeout(struct timer_list *t)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446{
Kees Cook4cfea082017-10-16 17:29:16 -07001447 struct bas_cardstate *ucs = from_timer(ucs, t, timer_ctrl);
Kees Cook33ad61d2017-10-20 13:47:08 -07001448 struct cardstate *cs = ucs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001449 int pending;
1450 unsigned long flags;
1451
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001452 check_pending(ucs);
1453
1454 spin_lock_irqsave(&ucs->lock, flags);
1455 pending = ucs->pending;
1456 ucs->pending = 0;
1457 spin_unlock_irqrestore(&ucs->lock, flags);
1458
1459 switch (pending) {
1460 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001461 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001462 break;
1463
1464 case HD_OPEN_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001465 dev_err(cs->dev, "timeout opening AT channel\n");
1466 error_reset(cs);
1467 break;
1468
1469 case HD_OPEN_B1CHANNEL:
1470 dev_err(cs->dev, "timeout opening channel 1\n");
1471 error_hangup(&cs->bcs[0]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001472 break;
1473
1474 case HD_OPEN_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001475 dev_err(cs->dev, "timeout opening channel 2\n");
1476 error_hangup(&cs->bcs[1]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001477 break;
1478
1479 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001480 dev_err(cs->dev, "timeout closing AT channel\n");
1481 error_reset(cs);
1482 break;
1483
1484 case HD_CLOSE_B1CHANNEL:
1485 dev_err(cs->dev, "timeout closing channel 1\n");
1486 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001487 break;
1488
1489 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001490 dev_err(cs->dev, "timeout closing channel 2\n");
1491 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001492 break;
1493
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001494 case HD_RESET_INTERRUPT_PIPE:
1495 /* error recovery escalation */
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001496 dev_err(cs->dev,
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001497 "reset interrupt pipe timeout, attempting USB reset\n");
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001498 usb_queue_reset_device(ucs->interface);
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001499 break;
1500
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001501 default:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001502 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001503 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001504 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001505
1506 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001507}
1508
1509/* write_ctrl_callback
1510 * USB completion handler for control pipe output
1511 * called by the USB subsystem in interrupt context
1512 * parameter:
1513 * urb USB request block of completed request
1514 * urb->context = hardware specific controller state structure
1515 */
David Howells7d12e782006-10-05 14:55:46 +01001516static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001517{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001518 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001519 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001520 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001521 unsigned long flags;
1522
Tilman Schmidt06163f82006-06-26 00:25:33 -07001523 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001524 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001525 case 0: /* normal completion */
1526 spin_lock_irqsave(&ucs->lock, flags);
1527 switch (ucs->pending) {
1528 case HD_DEVICE_INIT_ACK: /* no reply expected */
1529 del_timer(&ucs->timer_ctrl);
1530 ucs->pending = 0;
1531 break;
1532 }
1533 spin_unlock_irqrestore(&ucs->lock, flags);
1534 return;
1535
1536 case -ENOENT: /* cancelled */
1537 case -ECONNRESET: /* cancelled (async) */
1538 case -EINPROGRESS: /* pending */
1539 case -ENODEV: /* device removed */
1540 case -ESHUTDOWN: /* device shut down */
1541 /* ignore silently */
1542 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001543 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001544 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001545
1546 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001547 /* don't retry if suspend requested */
1548 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001549 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001550 dev_err(&ucs->interface->dev,
1551 "control request 0x%02x failed: %s\n",
1552 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001553 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001554 break; /* give up */
1555 }
1556 dev_notice(&ucs->interface->dev,
1557 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001558 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001559 ucs->retry_ctrl);
1560 /* urb->dev is clobbered by USB subsystem */
1561 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001562 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001563 if (unlikely(rc)) {
1564 dev_err(&ucs->interface->dev,
1565 "could not resubmit request 0x%02x: %s\n",
1566 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1567 break;
1568 }
1569 /* resubmitted */
1570 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001571 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001572
1573 /* failed, clear pending request */
1574 spin_lock_irqsave(&ucs->lock, flags);
1575 del_timer(&ucs->timer_ctrl);
1576 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001577 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001578 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001579}
1580
1581/* req_submit
1582 * submit a control output request without message buffer to the Gigaset base
1583 * and optionally start a timeout
1584 * parameters:
1585 * bcs B channel control structure
1586 * req control request code (HD_*)
1587 * val control request parameter value (set to 0 if unused)
1588 * timeout timeout in seconds (0: no timeout)
1589 * return value:
1590 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001591 * -EBUSY if another request is pending
1592 * any URB submission error code
1593 */
1594static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1595{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001596 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001597 int ret;
1598 unsigned long flags;
1599
Tilman Schmidt784d5852006-04-10 22:55:04 -07001600 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001601
1602 spin_lock_irqsave(&ucs->lock, flags);
1603 if (ucs->pending) {
1604 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001605 dev_err(bcs->cs->dev,
1606 "submission of request 0x%02x failed: "
1607 "request 0x%02x still pending\n",
1608 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001609 return -EBUSY;
1610 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001611
1612 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1613 ucs->dr_ctrl.bRequest = req;
1614 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1615 ucs->dr_ctrl.wIndex = 0;
1616 ucs->dr_ctrl.wLength = 0;
1617 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001618 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001619 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001620 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001621 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001622 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001623 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001624 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001625 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001626 spin_unlock_irqrestore(&ucs->lock, flags);
1627 return ret;
1628 }
1629 ucs->pending = req;
1630
1631 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001632 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001633 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001634 }
1635
1636 spin_unlock_irqrestore(&ucs->lock, flags);
1637 return 0;
1638}
1639
1640/* gigaset_init_bchannel
1641 * called by common.c to connect a B channel
1642 * initialize isochronous I/O and tell the Gigaset base to open the channel
1643 * argument:
1644 * B channel control structure
1645 * return value:
1646 * 0 on success, error code < 0 on error
1647 */
1648static int gigaset_init_bchannel(struct bc_state *bcs)
1649{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001650 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001651 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001652 unsigned long flags;
1653
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001654 spin_lock_irqsave(&cs->lock, flags);
1655 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001656 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001657 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001658 return -ENODEV;
1659 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001660
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001661 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001662 dev_notice(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001663 "not starting isoc I/O, suspend in progress\n");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001664 spin_unlock_irqrestore(&cs->lock, flags);
1665 return -EHOSTUNREACH;
1666 }
1667
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001668 ret = starturbs(bcs);
1669 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001670 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001671 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001672 "could not start isoc I/O for channel B%d: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001673 bcs->channel + 1,
1674 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1675 if (ret != -ENODEV)
1676 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001677 return ret;
1678 }
1679
1680 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001681 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1682 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001683 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001684 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001686 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001687
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001688 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001689 if (ret < 0 && ret != -ENODEV)
1690 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001691 return ret;
1692}
1693
1694/* gigaset_close_bchannel
1695 * called by common.c to disconnect a B channel
1696 * tell the Gigaset base to close the channel
1697 * stopping isochronous I/O and LL notification will be done when the
1698 * acknowledgement for the close arrives
1699 * argument:
1700 * B channel control structure
1701 * return value:
1702 * 0 on success, error code < 0 on error
1703 */
1704static int gigaset_close_bchannel(struct bc_state *bcs)
1705{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001706 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001707 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001708 unsigned long flags;
1709
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001710 spin_lock_irqsave(&cs->lock, flags);
1711 if (unlikely(!cs->connected)) {
1712 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001713 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1714 return -ENODEV;
1715 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001716
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001717 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001718 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001719 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001720 gigaset_bchannel_down(bcs);
1721 return 0;
1722 }
1723
Tilman Schmidt73a88812006-04-22 02:35:30 -07001724 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001725 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001726 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1727 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001728 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001729 bcs->channel + 1);
1730
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001731 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001732 return ret;
1733}
1734
1735/* Device Operations */
1736/* ================= */
1737
1738/* complete_cb
1739 * unqueue first command buffer from queue, waking any sleepers
1740 * must be called with cs->cmdlock held
1741 * parameter:
1742 * cs controller state structure
1743 */
1744static void complete_cb(struct cardstate *cs)
1745{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001746 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001747
1748 /* unqueue completed buffer */
1749 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001750 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001751 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001752 if (cb->next != NULL) {
1753 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001754 cs->cmdbuf->prev = NULL;
1755 cs->curlen = cs->cmdbuf->len;
1756 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001757 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001758 cs->lastcmdbuf = NULL;
1759 cs->curlen = 0;
1760 }
1761
1762 if (cb->wake_tasklet)
1763 tasklet_schedule(cb->wake_tasklet);
1764
1765 kfree(cb);
1766}
1767
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001768/* write_command_callback
1769 * USB completion handler for AT command transmission
1770 * called by the USB subsystem in interrupt context
1771 * parameter:
1772 * urb USB request block of completed request
1773 * urb->context = controller state structure
1774 */
David Howells7d12e782006-10-05 14:55:46 +01001775static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001776{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001777 struct cardstate *cs = urb->context;
1778 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001779 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001780 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001781
Tilman Schmidt73a88812006-04-22 02:35:30 -07001782 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001783 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001784
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001785 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001786 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001787 case 0: /* normal completion */
1788 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001789 case -ENOENT: /* cancelled */
1790 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001791 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001792 case -ENODEV: /* device removed */
1793 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001794 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001795 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001796 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001797 return;
1798 default: /* any failure */
1799 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001800 dev_warn(cs->dev,
1801 "command write: %s, "
1802 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001803 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001804 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001805 break;
1806 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001807 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001808 dev_warn(cs->dev,
1809 "command write: %s, "
1810 "won't retry - suspend requested\n",
1811 get_usb_statmsg(status));
1812 break;
1813 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001814 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001815 dev_warn(cs->dev,
1816 "command write: %s, "
1817 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001818 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001819 break;
1820 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001821 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001822 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001823 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1824 /* resubmitted - bypass regular exit block */
1825 return;
1826 /* command send failed, assume base still waiting */
1827 update_basstate(ucs, BS_ATREADY, 0);
1828 }
1829
1830 spin_lock_irqsave(&cs->cmdlock, flags);
1831 if (cs->cmdbuf != NULL)
1832 complete_cb(cs);
1833 spin_unlock_irqrestore(&cs->cmdlock, flags);
1834}
1835
1836/* atrdy_timeout
1837 * timeout routine for AT command transmission
1838 * argument:
1839 * controller state structure
1840 */
Kees Cook4cfea082017-10-16 17:29:16 -07001841static void atrdy_timeout(struct timer_list *t)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001842{
Kees Cook4cfea082017-10-16 17:29:16 -07001843 struct bas_cardstate *ucs = from_timer(ucs, t, timer_atrdy);
Kees Cook33ad61d2017-10-20 13:47:08 -07001844 struct cardstate *cs = ucs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001845
Tilman Schmidt784d5852006-04-10 22:55:04 -07001846 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001847
1848 /* fake the missing signal - what else can I do? */
1849 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1850 start_cbsend(cs);
1851}
1852
1853/* atwrite_submit
1854 * submit an HD_WRITE_ATMESSAGE command URB
1855 * parameters:
1856 * cs controller state structure
1857 * buf buffer containing command to send
1858 * len length of command to send
1859 * return value:
1860 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001861 * -EBUSY if another request is pending
1862 * any URB submission error code
1863 */
1864static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1865{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001866 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001867 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001868
Tilman Schmidt784d5852006-04-10 22:55:04 -07001869 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001870
Tilman Schmidt73a88812006-04-22 02:35:30 -07001871 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001872 dev_err(cs->dev,
1873 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001874 return -EBUSY;
1875 }
1876
1877 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1878 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1879 ucs->dr_cmd_out.wValue = 0;
1880 ucs->dr_cmd_out.wIndex = 0;
1881 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1882 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1883 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001884 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001885 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001886 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001887 if (unlikely(rc)) {
1888 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001889 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001890 get_usb_rcmsg(rc));
1891 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001892 }
1893
Tilman Schmidt73a88812006-04-22 02:35:30 -07001894 /* submitted successfully, start timeout if necessary */
1895 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001896 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1897 ATRDY_TIMEOUT);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001898 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001899 }
1900 return 0;
1901}
1902
1903/* start_cbsend
1904 * start transmission of AT command queue if necessary
1905 * parameter:
1906 * cs controller state structure
1907 * return value:
1908 * 0 on success
1909 * error code < 0 on error
1910 */
1911static int start_cbsend(struct cardstate *cs)
1912{
1913 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001914 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001915 unsigned long flags;
1916 int rc;
1917 int retval = 0;
1918
Tilman Schmidt024fd292008-02-06 01:38:26 -08001919 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001920 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001921 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001922 return -EHOSTUNREACH;
1923 }
1924
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001925 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001926 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001927 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001928 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1929 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001930 /* flush command queue */
1931 spin_lock_irqsave(&cs->cmdlock, flags);
1932 while (cs->cmdbuf != NULL)
1933 complete_cb(cs);
1934 spin_unlock_irqrestore(&cs->cmdlock, flags);
1935 }
1936 return rc;
1937 }
1938
1939 /* try to send first command in queue */
1940 spin_lock_irqsave(&cs->cmdlock, flags);
1941
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001942 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001943 ucs->retry_cmd_out = 0;
1944 rc = atwrite_submit(cs, cb->buf, cb->len);
1945 if (unlikely(rc)) {
1946 retval = rc;
1947 complete_cb(cs);
1948 }
1949 }
1950
1951 spin_unlock_irqrestore(&cs->cmdlock, flags);
1952 return retval;
1953}
1954
1955/* gigaset_write_cmd
1956 * This function is called by the device independent part of the driver
1957 * to transmit an AT command string to the Gigaset device.
1958 * It encapsulates the device specific method for transmission over the
1959 * direct USB connection to the base.
1960 * The command string is added to the queue of commands to send, and
1961 * USB transmission is started if necessary.
1962 * parameters:
1963 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001964 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001965 * return value:
1966 * number of bytes queued on success
1967 * error code < 0 on error
1968 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001969static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001970{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001971 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001972 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001973
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001974 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Joe Perches475be4d2012-02-19 19:52:38 -08001975 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001976 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001977
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001978 /* translate "+++" escape sequence sent as a single separate command
1979 * into "close AT channel" command for error recovery
1980 * The next command will reopen the AT channel automatically.
1981 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001982 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidt4cb5e422010-09-30 13:35:31 +00001983 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1984 * because of an error, the base never sends another one.
1985 * The response channel is thus effectively blocked.
1986 * Closing and reopening the AT channel does *not* clear
1987 * this condition.
1988 * As a stopgap measure, submit a zero-length AT read
1989 * before closing the AT channel. This has the undocumented
1990 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1991 * from the base if necessary.
1992 * The subsequent AT channel close then discards any pending
1993 * messages.
1994 */
1995 spin_lock_irqsave(&cs->lock, flags);
1996 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1997 kfree(cs->hw.bas->rcvbuf);
1998 cs->hw.bas->rcvbuf = NULL;
1999 cs->hw.bas->rcvbuf_size = 0;
2000 cs->hw.bas->retry_cmd_in = 0;
2001 atread_submit(cs, 0);
2002 }
2003 spin_unlock_irqrestore(&cs->lock, flags);
2004
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00002005 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002006 if (cb->wake_tasklet)
2007 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00002008 if (!rc)
2009 rc = cb->len;
2010 kfree(cb);
2011 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00002012 }
2013
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002014 spin_lock_irqsave(&cs->cmdlock, flags);
2015 cb->prev = cs->lastcmdbuf;
2016 if (cs->lastcmdbuf)
2017 cs->lastcmdbuf->next = cb;
2018 else {
2019 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002020 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002021 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002022 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002023 cs->lastcmdbuf = cb;
2024 spin_unlock_irqrestore(&cs->cmdlock, flags);
2025
Tilman Schmidt73a88812006-04-22 02:35:30 -07002026 spin_lock_irqsave(&cs->lock, flags);
2027 if (unlikely(!cs->connected)) {
2028 spin_unlock_irqrestore(&cs->lock, flags);
2029 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002030 /* flush command queue */
2031 spin_lock_irqsave(&cs->cmdlock, flags);
2032 while (cs->cmdbuf != NULL)
2033 complete_cb(cs);
2034 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002035 return -ENODEV;
2036 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002037 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002038 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002039 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002040}
2041
2042/* gigaset_write_room
2043 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07002044 * return number of characters the driver will accept to be written via
2045 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002046 * parameter:
2047 * controller state structure
2048 * return value:
2049 * number of characters
2050 */
2051static int gigaset_write_room(struct cardstate *cs)
2052{
2053 return IF_WRITEBUF;
2054}
2055
2056/* gigaset_chars_in_buffer
2057 * tty_driver.chars_in_buffer interface routine
2058 * return number of characters waiting to be sent
2059 * parameter:
2060 * controller state structure
2061 * return value:
2062 * number of characters
2063 */
2064static int gigaset_chars_in_buffer(struct cardstate *cs)
2065{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002066 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002067}
2068
2069/* gigaset_brkchars
2070 * implementation of ioctl(GIGASET_BRKCHARS)
2071 * parameter:
2072 * controller state structure
2073 * return value:
2074 * -EINVAL (unimplemented function)
2075 */
2076static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2077{
2078 return -EINVAL;
2079}
2080
2081
2082/* Device Initialization/Shutdown */
2083/* ============================== */
2084
2085/* Free hardware dependent part of the B channel structure
2086 * parameter:
2087 * bcs B channel structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002088 */
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002089static void gigaset_freebcshw(struct bc_state *bcs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002090{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002091 struct bas_bc_state *ubc = bcs->hw.bas;
2092 int i;
2093
2094 if (!ubc)
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002095 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002096
Tilman Schmidt73a88812006-04-22 02:35:30 -07002097 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002098 ubc->running = 0;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00002099 gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002100 for (i = 0; i < BAS_OUTURBS; ++i) {
2101 usb_kill_urb(ubc->isoouturbs[i].urb);
2102 usb_free_urb(ubc->isoouturbs[i].urb);
2103 }
2104 for (i = 0; i < BAS_INURBS; ++i) {
2105 usb_kill_urb(ubc->isoinurbs[i]);
2106 usb_free_urb(ubc->isoinurbs[i]);
2107 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002108 tasklet_kill(&ubc->sent_tasklet);
2109 tasklet_kill(&ubc->rcvd_tasklet);
2110 kfree(ubc->isooutbuf);
2111 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002112 bcs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002113}
2114
2115/* Initialize hardware dependent part of the B channel structure
2116 * parameter:
2117 * bcs B channel structure
2118 * return value:
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002119 * 0 on success, error code < 0 on failure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002120 */
2121static int gigaset_initbcshw(struct bc_state *bcs)
2122{
2123 int i;
2124 struct bas_bc_state *ubc;
2125
2126 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2127 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002128 pr_err("out of memory\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002129 return -ENOMEM;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002130 }
2131
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002132 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002133 atomic_set(&ubc->corrbytes, 0);
2134 spin_lock_init(&ubc->isooutlock);
2135 for (i = 0; i < BAS_OUTURBS; ++i) {
2136 ubc->isoouturbs[i].urb = NULL;
2137 ubc->isoouturbs[i].bcs = bcs;
2138 }
2139 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2140 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002141 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2142 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002143 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002144 kfree(ubc);
2145 bcs->hw.bas = NULL;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002146 return -ENOMEM;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002147 }
2148 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002149 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002150
2151 spin_lock_init(&ubc->isoinlock);
2152 for (i = 0; i < BAS_INURBS; ++i)
2153 ubc->isoinurbs[i] = NULL;
2154 ubc->isoindone = NULL;
2155 ubc->loststatus = -EINPROGRESS;
2156 ubc->isoinlost = 0;
2157 ubc->seqlen = 0;
2158 ubc->inbyte = 0;
2159 ubc->inbits = 0;
2160 ubc->goodbytes = 0;
2161 ubc->alignerrs = 0;
2162 ubc->fcserrs = 0;
2163 ubc->frameerrs = 0;
2164 ubc->giants = 0;
2165 ubc->runts = 0;
2166 ubc->aborts = 0;
2167 ubc->shared0s = 0;
2168 ubc->stolen0s = 0;
2169 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002170 read_iso_tasklet, (unsigned long) bcs);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002171 return 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002172}
2173
2174static void gigaset_reinitbcshw(struct bc_state *bcs)
2175{
2176 struct bas_bc_state *ubc = bcs->hw.bas;
2177
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002178 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002179 atomic_set(&bcs->hw.bas->corrbytes, 0);
2180 bcs->hw.bas->numsub = 0;
2181 spin_lock_init(&ubc->isooutlock);
2182 spin_lock_init(&ubc->isoinlock);
2183 ubc->loststatus = -EINPROGRESS;
2184}
2185
2186static void gigaset_freecshw(struct cardstate *cs)
2187{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002188 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002189 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002190 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002191 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002192}
2193
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002194/* Initialize hardware dependent part of the cardstate structure
2195 * parameter:
2196 * cs cardstate structure
2197 * return value:
2198 * 0 on success, error code < 0 on failure
2199 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002200static int gigaset_initcshw(struct cardstate *cs)
2201{
2202 struct bas_cardstate *ucs;
2203
Kees Cookc788dd22017-10-16 17:29:15 -07002204 cs->hw.bas = ucs = kzalloc(sizeof(*ucs), GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002205 if (!ucs) {
2206 pr_err("out of memory\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002207 return -ENOMEM;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002208 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002209 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2210 if (!ucs->int_in_buf) {
2211 kfree(ucs);
2212 pr_err("out of memory\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002213 return -ENOMEM;
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002214 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002215
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002216 spin_lock_init(&ucs->lock);
Kees Cook33ad61d2017-10-20 13:47:08 -07002217 ucs->cs = cs;
Kees Cook4cfea082017-10-16 17:29:16 -07002218 timer_setup(&ucs->timer_ctrl, req_timeout, 0);
2219 timer_setup(&ucs->timer_atrdy, atrdy_timeout, 0);
2220 timer_setup(&ucs->timer_cmd_in, cmd_in_timeout, 0);
2221 timer_setup(&ucs->timer_int_in, int_in_resubmit, 0);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002222 init_waitqueue_head(&ucs->waitqueue);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002223 INIT_WORK(&ucs->int_in_wq, int_in_work);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002224
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002225 return 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002226}
2227
2228/* freeurbs
2229 * unlink and deallocate all URBs unconditionally
2230 * caller must make sure that no commands are still in progress
2231 * parameter:
2232 * cs controller state structure
2233 */
2234static void freeurbs(struct cardstate *cs)
2235{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002236 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002237 struct bas_bc_state *ubc;
2238 int i, j;
2239
Tilman Schmidt39f07222006-12-13 00:33:52 -08002240 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002241 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002242 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002243 for (i = 0; i < BAS_OUTURBS; ++i) {
2244 usb_kill_urb(ubc->isoouturbs[i].urb);
2245 usb_free_urb(ubc->isoouturbs[i].urb);
2246 ubc->isoouturbs[i].urb = NULL;
2247 }
2248 for (i = 0; i < BAS_INURBS; ++i) {
2249 usb_kill_urb(ubc->isoinurbs[i]);
2250 usb_free_urb(ubc->isoinurbs[i]);
2251 ubc->isoinurbs[i] = NULL;
2252 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002254 usb_kill_urb(ucs->urb_int_in);
2255 usb_free_urb(ucs->urb_int_in);
2256 ucs->urb_int_in = NULL;
2257 usb_kill_urb(ucs->urb_cmd_out);
2258 usb_free_urb(ucs->urb_cmd_out);
2259 ucs->urb_cmd_out = NULL;
2260 usb_kill_urb(ucs->urb_cmd_in);
2261 usb_free_urb(ucs->urb_cmd_in);
2262 ucs->urb_cmd_in = NULL;
2263 usb_kill_urb(ucs->urb_ctrl);
2264 usb_free_urb(ucs->urb_ctrl);
2265 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002266}
2267
2268/* gigaset_probe
2269 * This function is called when a new USB device is connected.
2270 * It checks whether the new device is handled by this driver.
2271 */
2272static int gigaset_probe(struct usb_interface *interface,
2273 const struct usb_device_id *id)
2274{
2275 struct usb_host_interface *hostif;
2276 struct usb_device *udev = interface_to_usbdev(interface);
2277 struct cardstate *cs = NULL;
2278 struct bas_cardstate *ucs = NULL;
2279 struct bas_bc_state *ubc;
2280 struct usb_endpoint_descriptor *endpoint;
2281 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002282 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283
Tilman Schmidt1528b182010-02-22 13:09:52 +00002284 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002285 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2286 __func__, le16_to_cpu(udev->descriptor.idVendor),
2287 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002288
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289 /* set required alternate setting */
2290 hostif = interface->cur_altsetting;
2291 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002292 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002293 "%s: wrong alternate setting %d - trying to switch",
2294 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002295 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2296 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002297 dev_warn(&udev->dev, "usb_set_interface failed, "
2298 "device %d interface %d altsetting %d\n",
2299 udev->devnum, hostif->desc.bInterfaceNumber,
2300 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301 return -ENODEV;
2302 }
2303 hostif = interface->cur_altsetting;
2304 }
2305
2306 /* Reject application specific interfaces
2307 */
2308 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002309 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2310 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002311 return -ENODEV;
2312 }
2313
Johan Hovold68c32f92017-03-13 13:39:01 +01002314 if (hostif->desc.bNumEndpoints < 1)
2315 return -ENODEV;
2316
Tilman Schmidt784d5852006-04-10 22:55:04 -07002317 dev_info(&udev->dev,
2318 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2319 __func__, le16_to_cpu(udev->descriptor.idVendor),
2320 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002321
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002322 /* allocate memory for our device state and initialize it */
Tilman Schmidte468c042008-02-06 01:38:29 -08002323 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2324 GIGASET_MODULENAME);
2325 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002326 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002327 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002328
2329 /* save off device structure ptrs for later use */
2330 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002331 ucs->udev = udev;
2332 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002333 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002334
2335 /* allocate URBs:
2336 * - one for the interrupt pipe
2337 * - three for the different uses of the default control pipe
2338 * - three for each isochronous pipe
2339 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002340 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2341 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2342 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2343 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002344 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002345
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002346 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002347 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002348 for (i = 0; i < BAS_OUTURBS; ++i)
2349 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002350 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002351 goto allocerr;
2352 for (i = 0; i < BAS_INURBS; ++i)
2353 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002354 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002355 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002356 }
2357
2358 ucs->rcvbuf = NULL;
2359 ucs->rcvbuf_size = 0;
2360
2361 /* Fill the interrupt urb and send it to the core */
2362 endpoint = &hostif->endpoint[0].desc;
2363 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002364 usb_rcvintpipe(udev,
Himangi Saraogi14462b62014-08-17 06:01:20 +05302365 usb_endpoint_num(endpoint)),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002366 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002367 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002368 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2369 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002370 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002371 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002372 goto error;
2373 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002374 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002375
2376 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002377 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2378 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002379 goto error;
2380
2381 /* tell common part that the device is ready */
2382 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002383 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002384
2385 /* save address of controller structure */
2386 usb_set_intfdata(interface, cs);
2387
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002388 rc = gigaset_start(cs);
2389 if (rc < 0)
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002390 goto error;
2391
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002392 return 0;
2393
Tilman Schmidt73a88812006-04-22 02:35:30 -07002394allocerr:
2395 dev_err(cs->dev, "could not allocate URBs\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002396 rc = -ENOMEM;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002397error:
2398 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002399 usb_set_intfdata(interface, NULL);
Alexey Khoroshilov86b79872014-07-26 02:34:31 +04002400 usb_put_dev(udev);
Tilman Schmidte468c042008-02-06 01:38:29 -08002401 gigaset_freecs(cs);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002402 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002403}
2404
2405/* gigaset_disconnect
2406 * This function is called when the Gigaset base is unplugged.
2407 */
2408static void gigaset_disconnect(struct usb_interface *interface)
2409{
2410 struct cardstate *cs;
2411 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002412 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002413
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002414 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002415
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002416 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002417
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002418 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002419
2420 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002421 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002422
2423 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002424 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002425 gigaset_bchannel_down(cs->bcs + j);
2426
2427 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002428 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002429
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002430 /* stop delayed work and URBs, free ressources */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002431 del_timer_sync(&ucs->timer_ctrl);
2432 del_timer_sync(&ucs->timer_atrdy);
2433 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002434 del_timer_sync(&ucs->timer_int_in);
2435 cancel_work_sync(&ucs->int_in_wq);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002436 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002437 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002438 kfree(ucs->rcvbuf);
2439 ucs->rcvbuf = NULL;
2440 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002441 usb_put_dev(ucs->udev);
2442 ucs->interface = NULL;
2443 ucs->udev = NULL;
2444 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002445 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002446}
2447
Tilman Schmidt024fd292008-02-06 01:38:26 -08002448/* gigaset_suspend
Tilman Schmidtc6fdd8e2012-10-24 08:44:32 +00002449 * This function is called before the USB connection is suspended
2450 * or before the USB device is reset.
2451 * In the latter case, message == PMSG_ON.
Tilman Schmidt024fd292008-02-06 01:38:26 -08002452 */
2453static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2454{
2455 struct cardstate *cs = usb_get_intfdata(intf);
2456 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002457 int rc;
2458
2459 /* set suspend flag; this stops AT command/response traffic */
2460 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2461 gig_dbg(DEBUG_SUSPEND, "already suspended");
2462 return 0;
2463 }
2464
2465 /* wait a bit for blocking conditions to go away */
2466 rc = wait_event_timeout(ucs->waitqueue,
Joe Perches475be4d2012-02-19 19:52:38 -08002467 !(ucs->basstate &
2468 (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)),
2469 BAS_TIMEOUT * HZ / 10);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002470 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2471
2472 /* check for conditions preventing suspend */
Joe Perches475be4d2012-02-19 19:52:38 -08002473 if (ucs->basstate & (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002474 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002475 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002476 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002477 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002478 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002479 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002480 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002481 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002482 dev_warn(cs->dev, " sending AT command\n");
2483 update_basstate(ucs, 0, BS_SUSPEND);
2484 return -EBUSY;
2485 }
2486
2487 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002488 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002489 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2490 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2491 if (rc) {
2492 update_basstate(ucs, 0, BS_SUSPEND);
2493 return rc;
2494 }
2495 wait_event_timeout(ucs->waitqueue, !ucs->pending,
Joe Perches475be4d2012-02-19 19:52:38 -08002496 BAS_TIMEOUT * HZ / 10);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002497 /* in case of timeout, proceed anyway */
2498 }
2499
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002500 /* kill all URBs and delayed work that might still be pending */
Tilman Schmidt024fd292008-02-06 01:38:26 -08002501 usb_kill_urb(ucs->urb_ctrl);
2502 usb_kill_urb(ucs->urb_int_in);
2503 del_timer_sync(&ucs->timer_ctrl);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002504 del_timer_sync(&ucs->timer_atrdy);
2505 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002506 del_timer_sync(&ucs->timer_int_in);
Tilman Schmidtc6fdd8e2012-10-24 08:44:32 +00002507
2508 /* don't try to cancel int_in_wq from within reset as it
2509 * might be the one requesting the reset
2510 */
2511 if (message.event != PM_EVENT_ON)
2512 cancel_work_sync(&ucs->int_in_wq);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002513
2514 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2515 return 0;
2516}
2517
2518/* gigaset_resume
2519 * This function is called after the USB connection has been resumed.
2520 */
2521static int gigaset_resume(struct usb_interface *intf)
2522{
2523 struct cardstate *cs = usb_get_intfdata(intf);
2524 struct bas_cardstate *ucs = cs->hw.bas;
2525 int rc;
2526
2527 /* resubmit interrupt URB for spontaneous messages from base */
2528 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2529 if (rc) {
2530 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2531 get_usb_rcmsg(rc));
2532 return rc;
2533 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002534 ucs->retry_int_in = 0;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002535
2536 /* clear suspend flag to reallow activity */
2537 update_basstate(ucs, 0, BS_SUSPEND);
2538
2539 gig_dbg(DEBUG_SUSPEND, "resume complete");
2540 return 0;
2541}
2542
2543/* gigaset_pre_reset
2544 * This function is called before the USB connection is reset.
2545 */
2546static int gigaset_pre_reset(struct usb_interface *intf)
2547{
2548 /* handle just like suspend */
2549 return gigaset_suspend(intf, PMSG_ON);
2550}
2551
2552/* gigaset_post_reset
2553 * This function is called after the USB connection has been reset.
2554 */
2555static int gigaset_post_reset(struct usb_interface *intf)
2556{
2557 /* FIXME: send HD_DEVICE_INIT_ACK? */
2558
2559 /* resume operations */
2560 return gigaset_resume(intf);
2561}
2562
2563
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002564static const struct gigaset_ops gigops = {
Kees Cook47941952016-12-16 16:58:06 -08002565 .write_cmd = gigaset_write_cmd,
2566 .write_room = gigaset_write_room,
2567 .chars_in_buffer = gigaset_chars_in_buffer,
2568 .brkchars = gigaset_brkchars,
2569 .init_bchannel = gigaset_init_bchannel,
2570 .close_bchannel = gigaset_close_bchannel,
2571 .initbcshw = gigaset_initbcshw,
2572 .freebcshw = gigaset_freebcshw,
2573 .reinitbcshw = gigaset_reinitbcshw,
2574 .initcshw = gigaset_initcshw,
2575 .freecshw = gigaset_freecshw,
2576 .set_modem_ctrl = gigaset_set_modem_ctrl,
2577 .baud_rate = gigaset_baud_rate,
2578 .set_line_ctrl = gigaset_set_line_ctrl,
2579 .send_skb = gigaset_isoc_send_skb,
2580 .handle_input = gigaset_isoc_input,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002581};
2582
2583/* bas_gigaset_init
2584 * This function is called after the kernel module is loaded.
2585 */
2586static int __init bas_gigaset_init(void)
2587{
2588 int result;
2589
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002590 /* allocate memory for our driver state and initialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002591 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2592 GIGASET_MODULENAME, GIGASET_DEVNAME,
2593 &gigops, THIS_MODULE);
2594 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002595 goto error;
2596
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002597 /* register this driver with the USB subsystem */
2598 result = usb_register(&gigaset_usb_driver);
2599 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002600 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002601 goto error;
2602 }
2603
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002604 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002605 return 0;
2606
Tilman Schmidte468c042008-02-06 01:38:29 -08002607error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002608 if (driver)
2609 gigaset_freedriver(driver);
2610 driver = NULL;
2611 return -1;
2612}
2613
2614/* bas_gigaset_exit
2615 * This function is called before the kernel module is unloaded.
2616 */
2617static void __exit bas_gigaset_exit(void)
2618{
Tilman Schmidte468c042008-02-06 01:38:29 -08002619 struct bas_cardstate *ucs;
2620 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002621
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002622 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002623 * => no gigaset_start any more
2624 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002625
Tilman Schmidte468c042008-02-06 01:38:29 -08002626 /* stop all connected devices */
2627 for (i = 0; i < driver->minors; i++) {
2628 if (gigaset_shutdown(driver->cs + i) < 0)
2629 continue; /* no device */
2630 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002631
Tilman Schmidte468c042008-02-06 01:38:29 -08002632 /* close all still open channels */
2633 ucs = driver->cs[i].hw.bas;
2634 if (ucs->basstate & BS_B1OPEN) {
2635 gig_dbg(DEBUG_INIT, "closing B1 channel");
2636 usb_control_msg(ucs->udev,
2637 usb_sndctrlpipe(ucs->udev, 0),
2638 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2639 0, 0, NULL, 0, BAS_TIMEOUT);
2640 }
2641 if (ucs->basstate & BS_B2OPEN) {
2642 gig_dbg(DEBUG_INIT, "closing B2 channel");
2643 usb_control_msg(ucs->udev,
2644 usb_sndctrlpipe(ucs->udev, 0),
2645 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2646 0, 0, NULL, 0, BAS_TIMEOUT);
2647 }
2648 if (ucs->basstate & BS_ATOPEN) {
2649 gig_dbg(DEBUG_INIT, "closing AT channel");
2650 usb_control_msg(ucs->udev,
2651 usb_sndctrlpipe(ucs->udev, 0),
2652 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2653 0, 0, NULL, 0, BAS_TIMEOUT);
2654 }
2655 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002656 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002657
2658 /* deregister this driver with the USB subsystem */
2659 usb_deregister(&gigaset_usb_driver);
2660 /* this will call the disconnect-callback */
2661 /* from now on, no disconnect/probe callback should be running */
2662
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002663 gigaset_freedriver(driver);
2664 driver = NULL;
2665}
2666
2667
2668module_init(bas_gigaset_init);
2669module_exit(bas_gigaset_exit);
2670
2671MODULE_AUTHOR(DRIVER_AUTHOR);
2672MODULE_DESCRIPTION(DRIVER_DESC);
2673MODULE_LICENSE("GPL");