blob: 71e3fdee441080cfdfc13f9f5a28e0e36c337134 [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 */
92 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080093 unsigned char minor; /* starting minor number */
94
Tilman Schmidt784d5852006-04-10 22:55:04 -070095 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080096 struct usb_ctrlrequest dr_ctrl;
97 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070098 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080099
100 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700101 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800102 struct usb_ctrlrequest dr_cmd_out;
103 int retry_cmd_out;
104
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800106 struct usb_ctrlrequest dr_cmd_in;
107 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700108 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800109
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700111 unsigned char *int_in_buf;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000112 struct work_struct int_in_wq; /* for usb_clear_halt() */
113 struct timer_list timer_int_in; /* int read retry delay */
114 int retry_int_in;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800115
116 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800117 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800118 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800119 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800120 int rcvbuf_size; /* size of AT receive buffer */
121 /* 0: no receive in progress */
122 int retry_cmd_in; /* receive req retry count */
123};
124
125/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700126#define BS_ATOPEN 0x001 /* AT channel open */
127#define BS_B1OPEN 0x002 /* B channel 1 open */
128#define BS_B2OPEN 0x004 /* B channel 2 open */
129#define BS_ATREADY 0x008 /* base ready for AT command */
130#define BS_INIT 0x010 /* base has signalled INIT_OK */
131#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
132#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
133#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800134#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000135#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800136
137
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000138static struct gigaset_driver *driver;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800139
140/* usb specific object needed to register this driver with the usb subsystem */
141static struct usb_driver gigaset_usb_driver = {
142 .name = GIGASET_MODULENAME,
143 .probe = gigaset_probe,
144 .disconnect = gigaset_disconnect,
145 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800146 .suspend = gigaset_suspend,
147 .resume = gigaset_resume,
148 .reset_resume = gigaset_post_reset,
149 .pre_reset = gigaset_pre_reset,
150 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800151};
152
Tilman Schmidt73a88812006-04-22 02:35:30 -0700153/* get message text for usb_submit_urb return code
154 */
155static char *get_usb_rcmsg(int rc)
156{
157 static char unkmsg[28];
158
159 switch (rc) {
160 case 0:
161 return "success";
162 case -ENOMEM:
163 return "out of memory";
164 case -ENODEV:
165 return "device not present";
166 case -ENOENT:
167 return "endpoint not present";
168 case -ENXIO:
169 return "URB type not supported";
170 case -EINVAL:
171 return "invalid argument";
172 case -EAGAIN:
173 return "start frame too early or too much scheduled";
174 case -EFBIG:
175 return "too many isochronous frames requested";
176 case -EPIPE:
177 return "endpoint stalled";
178 case -EMSGSIZE:
179 return "invalid packet size";
180 case -ENOSPC:
181 return "would overcommit USB bandwidth";
182 case -ESHUTDOWN:
183 return "device shut down";
184 case -EPERM:
185 return "reject flag set";
186 case -EHOSTUNREACH:
187 return "device suspended";
188 default:
189 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
190 return unkmsg;
191 }
192}
193
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800194/* get message text for USB status code
195 */
196static char *get_usb_statmsg(int status)
197{
198 static char unkmsg[28];
199
200 switch (status) {
201 case 0:
202 return "success";
203 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700204 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800205 case -EINPROGRESS:
206 return "pending";
207 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700208 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700210 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700211 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800212 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700213 case -EPIPE:
214 return "endpoint stalled";
215 case -ECOMM:
216 return "IN buffer overrun";
217 case -ENOSR:
218 return "OUT buffer underrun";
219 case -EOVERFLOW:
220 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800221 case -EREMOTEIO:
222 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700223 case -ENODEV:
224 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800225 case -EXDEV:
226 return "partial isochronous transfer";
227 case -EINVAL:
228 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700229 case -ECONNRESET:
230 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800231 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700232 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800233 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700234 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800235 return unkmsg;
236 }
237}
238
239/* usb_pipetype_str
240 * retrieve string representation of USB pipe type
241 */
242static inline char *usb_pipetype_str(int pipe)
243{
244 if (usb_pipeisoc(pipe))
245 return "Isoc";
246 if (usb_pipeint(pipe))
247 return "Int";
248 if (usb_pipecontrol(pipe))
249 return "Ctrl";
250 if (usb_pipebulk(pipe))
251 return "Bulk";
252 return "?";
253}
254
255/* dump_urb
256 * write content of URB to syslog for debugging
257 */
258static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700259 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800260{
261#ifdef CONFIG_GIGASET_DEBUG
262 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700263 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800264 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 gig_dbg(level,
266 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800267 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700268 (unsigned long) urb->dev,
269 usb_pipetype_str(urb->pipe),
270 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
271 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800272 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700273 urb->transfer_flags);
274 gig_dbg(level,
275 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800276 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 (unsigned long) urb->transfer_buffer,
278 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800279 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700280 gig_dbg(level,
281 " start_frame=%d, number_of_packets=%d, interval=%d, "
282 "error_count=%d,",
283 urb->start_frame, urb->number_of_packets, urb->interval,
284 urb->error_count);
285 gig_dbg(level,
286 " context=0x%08lx, complete=0x%08lx, "
287 "iso_frame_desc[]={",
288 (unsigned long) urb->context,
289 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800290 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700291 struct usb_iso_packet_descriptor *pifd
292 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700293 gig_dbg(level,
294 " {offset=%u, length=%u, actual_length=%u, "
295 "status=%u}",
296 pifd->offset, pifd->length, pifd->actual_length,
297 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800298 }
299 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700300 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800301#endif
302}
303
304/* read/set modem control bits etc. (m10x only) */
305static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700306 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800307{
308 return -EINVAL;
309}
310
311static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
312{
313 return -EINVAL;
314}
315
316static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
317{
318 return -EINVAL;
319}
320
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000321/* set/clear bits in base connection state, return previous state
322 */
323static inline int update_basstate(struct bas_cardstate *ucs,
324 int set, int clear)
325{
326 unsigned long flags;
327 int state;
328
329 spin_lock_irqsave(&ucs->lock, flags);
330 state = ucs->basstate;
331 ucs->basstate = (state & ~clear) | set;
332 spin_unlock_irqrestore(&ucs->lock, flags);
333 return state;
334}
335
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800336/* error_hangup
337 * hang up any existing connection because of an unrecoverable error
338 * This function may be called from any context and takes care of scheduling
339 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700340 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800341 * argument:
342 * B channel control structure
343 */
344static inline void error_hangup(struct bc_state *bcs)
345{
346 struct cardstate *cs = bcs->cs;
347
Tilman Schmidt1528b182010-02-22 13:09:52 +0000348 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800349 gigaset_schedule_event(cs);
350}
351
352/* error_reset
353 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700354 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700355 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt60798c682010-09-30 13:35:21 +0000356 * cs->hw.bas->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800357 * argument:
358 * controller state structure
359 */
360static inline void error_reset(struct cardstate *cs)
361{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000362 /* reset interrupt pipe to recover (ignore errors) */
363 update_basstate(cs->hw.bas, BS_RESETTING, 0);
Tilman Schmidt60798c682010-09-30 13:35:21 +0000364 if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
365 /* submission failed, escalate to USB port reset */
366 usb_queue_reset_device(cs->hw.bas->interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800367}
368
369/* check_pending
370 * check for completion of pending control request
371 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700372 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 */
374static void check_pending(struct bas_cardstate *ucs)
375{
376 unsigned long flags;
377
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800378 spin_lock_irqsave(&ucs->lock, flags);
379 switch (ucs->pending) {
380 case 0:
381 break;
382 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800383 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800384 ucs->pending = 0;
385 break;
386 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800387 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800388 ucs->pending = 0;
389 break;
390 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800391 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 ucs->pending = 0;
393 break;
394 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800395 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800396 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800397 break;
398 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800399 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800400 ucs->pending = 0;
401 break;
402 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800403 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800404 ucs->pending = 0;
405 break;
406 case HD_DEVICE_INIT_ACK: /* no reply expected */
407 ucs->pending = 0;
408 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000409 case HD_RESET_INTERRUPT_PIPE:
410 if (!(ucs->basstate & BS_RESETTING))
411 ucs->pending = 0;
412 break;
413 /*
414 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
415 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800416 */
417 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700418 dev_warn(&ucs->interface->dev,
419 "unknown pending request 0x%02x cleared\n",
420 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800421 ucs->pending = 0;
422 }
423
424 if (!ucs->pending)
425 del_timer(&ucs->timer_ctrl);
426
427 spin_unlock_irqrestore(&ucs->lock, flags);
428}
429
430/* cmd_in_timeout
431 * timeout routine for command input request
432 * argument:
433 * controller state structure
434 */
435static void cmd_in_timeout(unsigned long data)
436{
437 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700438 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700439 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800441 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700442 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800443 return;
444 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800445
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000446 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700447 dev_err(cs->dev,
448 "control read: timeout, giving up after %d tries\n",
449 ucs->retry_cmd_in);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000450 kfree(ucs->rcvbuf);
451 ucs->rcvbuf = NULL;
452 ucs->rcvbuf_size = 0;
453 error_reset(cs);
454 return;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700455 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000456
457 gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
458 __func__, ucs->retry_cmd_in);
459 rc = atread_submit(cs, BAS_TIMEOUT);
460 if (rc < 0) {
461 kfree(ucs->rcvbuf);
462 ucs->rcvbuf = NULL;
463 ucs->rcvbuf_size = 0;
464 if (rc != -ENODEV)
465 error_reset(cs);
466 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800467}
468
Tilman Schmidt06163f82006-06-26 00:25:33 -0700469/* read_ctrl_callback
470 * USB completion handler for control pipe input
471 * called by the USB subsystem in interrupt context
472 * parameter:
473 * urb USB request block
474 * urb->context = inbuf structure for controller state
475 */
David Howells7d12e782006-10-05 14:55:46 +0100476static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700477{
478 struct inbuf_t *inbuf = urb->context;
479 struct cardstate *cs = inbuf->cs;
480 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800481 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700482 unsigned numbytes;
483 int rc;
484
485 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800486 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700487 del_timer(&ucs->timer_cmd_in);
488
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800489 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700490 case 0: /* normal completion */
491 numbytes = urb->actual_length;
492 if (unlikely(numbytes != ucs->rcvbuf_size)) {
493 dev_warn(cs->dev,
494 "control read: received %d chars, expected %d\n",
495 numbytes, ucs->rcvbuf_size);
496 if (numbytes > ucs->rcvbuf_size)
497 numbytes = ucs->rcvbuf_size;
498 }
499
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000500 /* copy received bytes to inbuf, notify event layer */
501 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
502 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
503 gigaset_schedule_event(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700504 }
505 break;
506
507 case -ENOENT: /* cancelled */
508 case -ECONNRESET: /* cancelled (async) */
509 case -EINPROGRESS: /* pending */
510 case -ENODEV: /* device removed */
511 case -ESHUTDOWN: /* device shut down */
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000512 /* no further action necessary */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700513 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800514 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700515 break;
516
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000517 default: /* other errors: retry */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700518 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000519 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
520 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700521 rc = atread_submit(cs, BAS_TIMEOUT);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000522 if (rc >= 0)
523 /* successfully resubmitted, skip freeing */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700524 return;
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000525 if (rc == -ENODEV)
526 /* disconnect, no further action necessary */
527 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700528 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000529 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
530 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700531 error_reset(cs);
532 }
533
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000534 /* read finished, free buffer */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700535 kfree(ucs->rcvbuf);
536 ucs->rcvbuf = NULL;
537 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700538}
539
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800540/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700541 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800542 * parameters:
543 * cs controller state structure
544 * timeout timeout in 1/10 sec., 0: none
545 * return value:
546 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800547 * -EBUSY if another request is pending
548 * any URB submission error code
549 */
550static int atread_submit(struct cardstate *cs, int timeout)
551{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700552 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800553 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800554 int ret;
555
Tilman Schmidt784d5852006-04-10 22:55:04 -0700556 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
557 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800558
Tilman Schmidt024fd292008-02-06 01:38:26 -0800559 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
560 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700561 dev_err(cs->dev,
562 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800563 return -EBUSY;
564 }
565
Tilman Schmidt024fd292008-02-06 01:38:26 -0800566 if (basstate & BS_SUSPEND) {
567 dev_notice(cs->dev,
568 "HD_READ_ATMESSAGE not submitted, "
569 "suspend in progress\n");
570 update_basstate(ucs, 0, BS_ATRDPEND);
571 /* treat like disconnect */
572 return -ENODEV;
573 }
574
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
576 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
577 ucs->dr_cmd_in.wValue = 0;
578 ucs->dr_cmd_in.wIndex = 0;
579 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
580 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700581 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000582 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700583 ucs->rcvbuf, ucs->rcvbuf_size,
584 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800585
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000586 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
587 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700588 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700589 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700590 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800591 return ret;
592 }
593
594 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700595 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +0000596 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800597 }
598 return 0;
599}
600
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000601/* int_in_work
602 * workqueue routine to clear halt on interrupt in endpoint
603 */
604
605static void int_in_work(struct work_struct *work)
606{
607 struct bas_cardstate *ucs =
608 container_of(work, struct bas_cardstate, int_in_wq);
609 struct urb *urb = ucs->urb_int_in;
610 struct cardstate *cs = urb->context;
611 int rc;
612
613 /* clear halt condition */
614 rc = usb_clear_halt(ucs->udev, urb->pipe);
615 gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
616 if (rc == 0)
617 /* success, resubmit interrupt read URB */
618 rc = usb_submit_urb(urb, GFP_ATOMIC);
619 if (rc != 0 && rc != -ENODEV) {
620 dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
621 rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
622 if (rc == 0) {
623 rc = usb_reset_device(ucs->udev);
624 usb_unlock_device(ucs->udev);
625 }
626 }
627 ucs->retry_int_in = 0;
628}
629
630/* int_in_resubmit
631 * timer routine for interrupt read delayed resubmit
632 * argument:
633 * controller state structure
634 */
635static void int_in_resubmit(unsigned long data)
636{
637 struct cardstate *cs = (struct cardstate *) data;
638 struct bas_cardstate *ucs = cs->hw.bas;
639 int rc;
640
641 if (ucs->retry_int_in++ >= BAS_RETRY) {
642 dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
643 ucs->retry_int_in);
644 usb_queue_reset_device(ucs->interface);
645 return;
646 }
647
648 gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
649 rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
650 if (rc != 0 && rc != -ENODEV) {
651 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
652 get_usb_rcmsg(rc));
653 usb_queue_reset_device(ucs->interface);
654 }
655}
656
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800657/* read_int_callback
658 * USB completion handler for interrupt pipe input
659 * called by the USB subsystem in interrupt context
660 * parameter:
661 * urb USB request block
662 * urb->context = controller state structure
663 */
David Howells7d12e782006-10-05 14:55:46 +0100664static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800665{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700666 struct cardstate *cs = urb->context;
667 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800668 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800669 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800670 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700671 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800672 unsigned l;
673 int channel;
674
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800675 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800676 case 0: /* success */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000677 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800678 break;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000679 case -EPIPE: /* endpoint stalled */
680 schedule_work(&ucs->int_in_wq);
681 /* fall through */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700682 case -ENOENT: /* cancelled */
683 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800684 case -EINPROGRESS: /* pending */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000685 case -ENODEV: /* device removed */
686 case -ESHUTDOWN: /* device shut down */
687 /* no further action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700688 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800689 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800690 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000691 case -EPROTO: /* protocol error or unplug */
692 case -EILSEQ:
693 case -ETIME:
694 /* resubmit after delay */
695 gig_dbg(DEBUG_USBREQ, "%s: %s",
696 __func__, get_usb_statmsg(status));
697 mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700698 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000699 default: /* other errors: just resubmit */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700700 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800701 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800702 goto resubmit;
703 }
704
Tilman Schmidt73a88812006-04-22 02:35:30 -0700705 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700706 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700707 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
708 urb->actual_length);
709 goto resubmit;
710 }
711
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800712 l = (unsigned) ucs->int_in_buf[1] +
713 (((unsigned) ucs->int_in_buf[2]) << 8);
714
Tilman Schmidt784d5852006-04-10 22:55:04 -0700715 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
716 urb->actual_length, (int)ucs->int_in_buf[0], l,
717 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800718
719 channel = 0;
720
721 switch (ucs->int_in_buf[0]) {
722 case HD_DEVICE_INIT_OK:
723 update_basstate(ucs, BS_INIT, 0);
724 break;
725
726 case HD_READY_SEND_ATDATA:
727 del_timer(&ucs->timer_atrdy);
728 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
729 start_cbsend(cs);
730 break;
731
732 case HD_OPEN_B2CHANNEL_ACK:
733 ++channel;
734 case HD_OPEN_B1CHANNEL_ACK:
735 bcs = cs->bcs + channel;
736 update_basstate(ucs, BS_B1OPEN << channel, 0);
737 gigaset_bchannel_up(bcs);
738 break;
739
740 case HD_OPEN_ATCHANNEL_ACK:
741 update_basstate(ucs, BS_ATOPEN, 0);
742 start_cbsend(cs);
743 break;
744
745 case HD_CLOSE_B2CHANNEL_ACK:
746 ++channel;
747 case HD_CLOSE_B1CHANNEL_ACK:
748 bcs = cs->bcs + channel;
749 update_basstate(ucs, 0, BS_B1OPEN << channel);
750 stopurbs(bcs->hw.bas);
751 gigaset_bchannel_down(bcs);
752 break;
753
754 case HD_CLOSE_ATCHANNEL_ACK:
755 update_basstate(ucs, 0, BS_ATOPEN);
756 break;
757
758 case HD_B2_FLOW_CONTROL:
759 ++channel;
760 case HD_B1_FLOW_CONTROL:
761 bcs = cs->bcs + channel;
762 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700763 &bcs->hw.bas->corrbytes);
764 gig_dbg(DEBUG_ISO,
765 "Flow control (channel %d, sub %d): 0x%02x => %d",
766 channel, bcs->hw.bas->numsub, l,
767 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800768 break;
769
770 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
771 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700772 dev_warn(cs->dev,
773 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774 break;
775 }
776 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000777 if (ucs->basstate & BS_ATRDPEND) {
778 spin_unlock_irqrestore(&cs->lock, flags);
779 dev_warn(cs->dev,
780 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
781 l, ucs->rcvbuf_size);
782 break;
783 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700785 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700786 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700787 "receive AT data overrun, %d bytes lost\n",
788 ucs->rcvbuf_size);
789 kfree(ucs->rcvbuf);
790 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800791 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000792 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
793 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800794 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700795 dev_err(cs->dev, "out of memory receiving AT data\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800796 break;
797 }
798 ucs->rcvbuf_size = l;
799 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000800 rc = atread_submit(cs, BAS_TIMEOUT);
801 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802 kfree(ucs->rcvbuf);
803 ucs->rcvbuf = NULL;
804 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800805 }
806 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000807 if (rc < 0 && rc != -ENODEV)
808 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800809 break;
810
811 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000812 update_basstate(ucs, 0, BS_RESETTING);
813 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800814 break;
815
816 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700817 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800818 break;
819
820 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700821 dev_warn(cs->dev,
822 "unknown Gigaset signal 0x%02x (%u) ignored\n",
823 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800824 }
825
826 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800827 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800828
829resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800830 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700831 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700832 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700833 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800834 error_reset(cs);
835 }
836}
837
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800838/* read_iso_callback
839 * USB completion handler for B channel isochronous input
840 * called by the USB subsystem in interrupt context
841 * parameter:
842 * urb USB request block of completed request
843 * urb->context = bc_state structure
844 */
David Howells7d12e782006-10-05 14:55:46 +0100845static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800846{
847 struct bc_state *bcs;
848 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800849 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800850 unsigned long flags;
851 int i, rc;
852
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800853 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800854 if (unlikely(status == -ENOENT ||
855 status == -ECONNRESET ||
856 status == -EINPROGRESS ||
857 status == -ENODEV ||
858 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700859 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800860 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800861 return;
862 }
863
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700864 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800865 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800866
867 spin_lock_irqsave(&ubc->isoinlock, flags);
868 if (likely(ubc->isoindone == NULL)) {
869 /* pass URB to tasklet */
870 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800871 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000872 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800873 } else {
874 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800875 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800876 for (i = 0; i < BAS_NUMFRAMES; i++) {
877 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
878 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700879 urb->iso_frame_desc[i].status !=
880 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800881 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800882 urb->iso_frame_desc[i].status = 0;
883 urb->iso_frame_desc[i].actual_length = 0;
884 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800885 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700886 /* urb->dev is clobbered by USB subsystem */
887 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800888 urb->transfer_flags = URB_ISO_ASAP;
889 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700890 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
891 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800892 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700893 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700894 dev_err(bcs->cs->dev,
895 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700896 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800897 dump_urb(DEBUG_ISO, "isoc read", urb);
898 error_hangup(bcs);
899 }
900 }
901 }
902 spin_unlock_irqrestore(&ubc->isoinlock, flags);
903}
904
905/* write_iso_callback
906 * USB completion handler for B channel isochronous output
907 * called by the USB subsystem in interrupt context
908 * parameter:
909 * urb USB request block of completed request
910 * urb->context = isow_urbctx_t structure
911 */
David Howells7d12e782006-10-05 14:55:46 +0100912static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800913{
914 struct isow_urbctx_t *ucx;
915 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800916 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800917 unsigned long flags;
918
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800919 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800920 if (unlikely(status == -ENOENT ||
921 status == -ECONNRESET ||
922 status == -EINPROGRESS ||
923 status == -ENODEV ||
924 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700925 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800926 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800927 return;
928 }
929
930 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700931 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800932 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800933 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800934
935 spin_lock_irqsave(&ubc->isooutlock, flags);
936 ubc->isooutovfl = ubc->isooutdone;
937 ubc->isooutdone = ucx;
938 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000939 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940}
941
942/* starturbs
943 * prepare and submit USB request blocks for isochronous input and output
944 * argument:
945 * B channel control structure
946 * return value:
947 * 0 on success
948 * < 0 on error (no URBs submitted)
949 */
950static int starturbs(struct bc_state *bcs)
951{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700952 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800953 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800954 int j, k;
955 int rc;
956
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800957 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000958 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800959 bcs->inputstate |= INS_flag_hunt;
960
961 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800962 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800963 for (k = 0; k < BAS_INURBS; k++) {
964 urb = ubc->isoinurbs[k];
965 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800966 rc = -EFAULT;
967 goto error;
968 }
969
970 urb->dev = bcs->cs->hw.bas->udev;
971 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
972 urb->transfer_flags = URB_ISO_ASAP;
973 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
974 urb->transfer_buffer_length = BAS_INBUFSIZE;
975 urb->number_of_packets = BAS_NUMFRAMES;
976 urb->interval = BAS_FRAMETIME;
977 urb->complete = read_iso_callback;
978 urb->context = bcs;
979 for (j = 0; j < BAS_NUMFRAMES; j++) {
980 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
981 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
982 urb->iso_frame_desc[j].status = 0;
983 urb->iso_frame_desc[j].actual_length = 0;
984 }
985
986 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000987 rc = usb_submit_urb(urb, GFP_ATOMIC);
988 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800989 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800990 }
991
992 /* initialize L2 transmission */
993 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
994
995 /* set up isochronous output URBs for flag idling */
996 for (k = 0; k < BAS_OUTURBS; ++k) {
997 urb = ubc->isoouturbs[k].urb;
998 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800999 rc = -EFAULT;
1000 goto error;
1001 }
1002 urb->dev = bcs->cs->hw.bas->udev;
1003 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
1004 urb->transfer_flags = URB_ISO_ASAP;
1005 urb->transfer_buffer = ubc->isooutbuf->data;
1006 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1007 urb->number_of_packets = BAS_NUMFRAMES;
1008 urb->interval = BAS_FRAMETIME;
1009 urb->complete = write_iso_callback;
1010 urb->context = &ubc->isoouturbs[k];
1011 for (j = 0; j < BAS_NUMFRAMES; ++j) {
1012 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
1013 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
1014 urb->iso_frame_desc[j].status = 0;
1015 urb->iso_frame_desc[j].actual_length = 0;
1016 }
1017 ubc->isoouturbs[k].limit = -1;
1018 }
1019
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001020 /* keep one URB free, submit the others */
1021 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001022 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001023 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001024 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001025 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001026 }
1027 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001028 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001029 ubc->isooutdone = ubc->isooutovfl = NULL;
1030 return 0;
1031 error:
1032 stopurbs(ubc);
1033 return rc;
1034}
1035
1036/* stopurbs
1037 * cancel the USB request blocks for isochronous input and output
1038 * errors are silently ignored
1039 * argument:
1040 * B channel control structure
1041 */
1042static void stopurbs(struct bas_bc_state *ubc)
1043{
1044 int k, rc;
1045
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001046 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001047
1048 for (k = 0; k < BAS_INURBS; ++k) {
1049 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001050 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001051 "%s: isoc input URB %d unlinked, result = %s",
1052 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001053 }
1054
1055 for (k = 0; k < BAS_OUTURBS; ++k) {
1056 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001057 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001058 "%s: isoc output URB %d unlinked, result = %s",
1059 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001060 }
1061}
1062
1063/* Isochronous Write - Bottom Half */
1064/* =============================== */
1065
1066/* submit_iso_write_urb
1067 * fill and submit the next isochronous write URB
1068 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001069 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001070 * return value:
1071 * number of frames submitted in URB
1072 * 0 if URB not submitted because no data available (isooutbuf busy)
1073 * error code < 0 on error
1074 */
1075static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1076{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001077 struct urb *urb = ucx->urb;
1078 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001079 struct usb_iso_packet_descriptor *ifd;
1080 int corrbytes, nframe, rc;
1081
Tilman Schmidt784d5852006-04-10 22:55:04 -07001082 /* urb->dev is clobbered by USB subsystem */
1083 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001084 urb->transfer_flags = URB_ISO_ASAP;
1085 urb->transfer_buffer = ubc->isooutbuf->data;
1086 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1087
1088 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1089 ifd = &urb->iso_frame_desc[nframe];
1090
1091 /* compute frame length according to flow control */
1092 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001093 corrbytes = atomic_read(&ubc->corrbytes);
1094 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001095 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1096 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001097 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1098 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1099 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1100 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1101 ifd->length += corrbytes;
1102 atomic_add(-corrbytes, &ubc->corrbytes);
1103 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001104
1105 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001106 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1107 if (rc < 0) {
1108 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001109 gig_dbg(DEBUG_ISO,
1110 "%s: buffer busy at frame %d",
1111 __func__, nframe);
1112 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001113 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001114 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001115 dev_err(ucx->bcs->cs->dev,
1116 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001117 __func__, rc, nframe);
1118 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001119 }
1120 break;
1121 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001122 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001123 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001124 ifd->status = 0;
1125 ifd->actual_length = 0;
1126 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001127 if (unlikely(nframe == 0))
1128 return 0; /* no data to send */
1129 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001130
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001131 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001132 if (unlikely(rc)) {
1133 if (rc == -ENODEV)
1134 /* device removed - give up silently */
1135 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1136 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001137 dev_err(ucx->bcs->cs->dev,
1138 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001139 get_usb_rcmsg(rc));
1140 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001141 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001142 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001143 return nframe;
1144}
1145
1146/* write_iso_tasklet
1147 * tasklet scheduled when an isochronous output URB from the Gigaset device
1148 * has completed
1149 * parameter:
1150 * data B channel state structure
1151 */
1152static void write_iso_tasklet(unsigned long data)
1153{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001154 struct bc_state *bcs = (struct bc_state *) data;
1155 struct bas_bc_state *ubc = bcs->hw.bas;
1156 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 struct isow_urbctx_t *done, *next, *ovfl;
1158 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001159 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001160 struct usb_iso_packet_descriptor *ifd;
1161 int offset;
1162 unsigned long flags;
1163 int i;
1164 struct sk_buff *skb;
1165 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001166 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001167
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001168 /* loop while completed URBs arrive in time */
1169 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001170 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001171 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001172 return;
1173 }
1174
1175 /* retrieve completed URBs */
1176 spin_lock_irqsave(&ubc->isooutlock, flags);
1177 done = ubc->isooutdone;
1178 ubc->isooutdone = NULL;
1179 ovfl = ubc->isooutovfl;
1180 ubc->isooutovfl = NULL;
1181 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1182 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001183 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001184 error_hangup(bcs);
1185 break;
1186 }
1187 if (!done)
1188 break;
1189
1190 /* submit free URB if available */
1191 spin_lock_irqsave(&ubc->isooutlock, flags);
1192 next = ubc->isooutfree;
1193 ubc->isooutfree = NULL;
1194 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1195 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001196 rc = submit_iso_write_urb(next);
1197 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001198 /* could not submit URB, put it back */
1199 spin_lock_irqsave(&ubc->isooutlock, flags);
1200 if (ubc->isooutfree == NULL) {
1201 ubc->isooutfree = next;
1202 next = NULL;
1203 }
1204 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1205 if (next) {
1206 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001207 dev_err(cs->dev,
1208 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001209 error_hangup(bcs);
1210 }
1211 }
1212 }
1213
1214 /* process completed URB */
1215 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001216 status = done->status;
1217 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001218 case -EXDEV: /* partial completion */
1219 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1220 __func__);
1221 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001222 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001223 /* inspect individual frames
1224 * assumptions (for lack of documentation):
1225 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001226 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001227 * - all following frames are not sent at all
1228 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001229 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001230 for (i = 0; i < BAS_NUMFRAMES; i++) {
1231 ifd = &urb->iso_frame_desc[i];
1232 if (ifd->status ||
1233 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001234 dev_warn(cs->dev,
1235 "isochronous write: frame %d: %s, "
1236 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 i, get_usb_statmsg(ifd->status),
1238 ifd->actual_length, ifd->length);
1239 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001240 ifd->actual_length)
1241 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001242 break;
1243 }
1244 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001245 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001246 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001247 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001248 error_hangup(bcs);
1249 break;
1250 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001251 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001252 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001253 }
1254
1255 /* mark the write buffer area covered by this URB as free */
1256 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001257 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001258
1259 /* mark URB as free */
1260 spin_lock_irqsave(&ubc->isooutlock, flags);
1261 next = ubc->isooutfree;
1262 ubc->isooutfree = done;
1263 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1264 if (next) {
1265 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001266 rc = submit_iso_write_urb(next);
1267 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001268 /* couldn't submit */
1269 error_hangup(bcs);
1270 }
1271 }
1272 }
1273
1274 /* process queued SKBs */
1275 while ((skb = skb_dequeue(&bcs->squeue))) {
1276 /* copy to output buffer, doing L2 encapsulation */
1277 len = skb->len;
1278 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1279 /* insufficient buffer space, push back onto queue */
1280 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001281 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1282 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001283 break;
1284 }
1285 skb_pull(skb, len);
1286 gigaset_skb_sent(bcs, skb);
1287 dev_kfree_skb_any(skb);
1288 }
1289}
1290
1291/* Isochronous Read - Bottom Half */
1292/* ============================== */
1293
1294/* read_iso_tasklet
1295 * tasklet scheduled when an isochronous input URB from the Gigaset device
1296 * has completed
1297 * parameter:
1298 * data B channel state structure
1299 */
1300static void read_iso_tasklet(unsigned long data)
1301{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001302 struct bc_state *bcs = (struct bc_state *) data;
1303 struct bas_bc_state *ubc = bcs->hw.bas;
1304 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001305 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001306 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001307 char *rcvbuf;
1308 unsigned long flags;
1309 int totleft, numbytes, offset, frame, rc;
1310
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001311 /* loop while more completed URBs arrive in the meantime */
1312 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001313 /* retrieve URB */
1314 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001315 urb = ubc->isoindone;
1316 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001317 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1318 return;
1319 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001320 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 ubc->isoindone = NULL;
1322 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001323 dev_warn(cs->dev,
1324 "isochronous read overrun, "
1325 "dropped URB with status: %s, %d bytes lost\n",
1326 get_usb_statmsg(ubc->loststatus),
1327 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001328 ubc->loststatus = -EINPROGRESS;
1329 }
1330 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1331
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001332 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001333 gig_dbg(DEBUG_ISO,
1334 "%s: channel not running, "
1335 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001336 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001337 return;
1338 }
1339
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001340 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001341 case 0: /* normal completion */
1342 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001343 case -EXDEV: /* inspect individual frames
1344 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001345 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1346 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001347 break;
1348 case -ENOENT:
1349 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001350 case -EINPROGRESS:
1351 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001352 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001353 continue; /* -> skip */
1354 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001355 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001356 error_hangup(bcs);
1357 continue; /* -> skip */
1358 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001359 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001360 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001361 goto error;
1362 }
1363
1364 rcvbuf = urb->transfer_buffer;
1365 totleft = urb->actual_length;
1366 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001367 numbytes = urb->iso_frame_desc[frame].actual_length;
1368 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001369 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001370 "isochronous read: frame %d[%d]: %s\n",
1371 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001372 get_usb_statmsg(
1373 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001374 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001375 dev_warn(cs->dev,
1376 "isochronous read: frame %d: "
1377 "numbytes (%d) > BAS_MAXFRAME\n",
1378 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001379 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001380 dev_warn(cs->dev,
1381 "isochronous read: frame %d: "
1382 "numbytes (%d) > totleft (%d)\n",
1383 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001384 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001385 }
1386 offset = urb->iso_frame_desc[frame].offset;
1387 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001388 dev_warn(cs->dev,
1389 "isochronous read: frame %d: "
1390 "offset (%d) + numbytes (%d) "
1391 "> BAS_INBUFSIZE\n",
1392 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001393 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001394 }
1395 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1396 totleft -= numbytes;
1397 }
1398 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001399 dev_warn(cs->dev,
1400 "isochronous read: %d data bytes missing\n",
1401 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001402
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001403error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001404 /* URB processed, resubmit */
1405 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1406 urb->iso_frame_desc[frame].status = 0;
1407 urb->iso_frame_desc[frame].actual_length = 0;
1408 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001409 /* urb->dev is clobbered by USB subsystem */
1410 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001411 urb->transfer_flags = URB_ISO_ASAP;
1412 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001413 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001414 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001415 dev_err(cs->dev,
1416 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001417 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1419 error_hangup(bcs);
1420 }
1421 }
1422}
1423
1424/* Channel Operations */
1425/* ================== */
1426
1427/* req_timeout
1428 * timeout routine for control output request
1429 * argument:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001430 * controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001431 */
1432static void req_timeout(unsigned long data)
1433{
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001434 struct cardstate *cs = (struct cardstate *) data;
1435 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001436 int pending;
1437 unsigned long flags;
1438
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001439 check_pending(ucs);
1440
1441 spin_lock_irqsave(&ucs->lock, flags);
1442 pending = ucs->pending;
1443 ucs->pending = 0;
1444 spin_unlock_irqrestore(&ucs->lock, flags);
1445
1446 switch (pending) {
1447 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001448 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001449 break;
1450
1451 case HD_OPEN_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001452 dev_err(cs->dev, "timeout opening AT channel\n");
1453 error_reset(cs);
1454 break;
1455
1456 case HD_OPEN_B1CHANNEL:
1457 dev_err(cs->dev, "timeout opening channel 1\n");
1458 error_hangup(&cs->bcs[0]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001459 break;
1460
1461 case HD_OPEN_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001462 dev_err(cs->dev, "timeout opening channel 2\n");
1463 error_hangup(&cs->bcs[1]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001464 break;
1465
1466 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001467 dev_err(cs->dev, "timeout closing AT channel\n");
1468 error_reset(cs);
1469 break;
1470
1471 case HD_CLOSE_B1CHANNEL:
1472 dev_err(cs->dev, "timeout closing channel 1\n");
1473 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001474 break;
1475
1476 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001477 dev_err(cs->dev, "timeout closing channel 2\n");
1478 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001479 break;
1480
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001481 case HD_RESET_INTERRUPT_PIPE:
1482 /* error recovery escalation */
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001483 dev_err(cs->dev,
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001484 "reset interrupt pipe timeout, attempting USB reset\n");
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001485 usb_queue_reset_device(ucs->interface);
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001486 break;
1487
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001488 default:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001489 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001490 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001491 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001492
1493 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001494}
1495
1496/* write_ctrl_callback
1497 * USB completion handler for control pipe output
1498 * called by the USB subsystem in interrupt context
1499 * parameter:
1500 * urb USB request block of completed request
1501 * urb->context = hardware specific controller state structure
1502 */
David Howells7d12e782006-10-05 14:55:46 +01001503static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001504{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001505 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001506 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001507 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001508 unsigned long flags;
1509
Tilman Schmidt06163f82006-06-26 00:25:33 -07001510 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001511 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001512 case 0: /* normal completion */
1513 spin_lock_irqsave(&ucs->lock, flags);
1514 switch (ucs->pending) {
1515 case HD_DEVICE_INIT_ACK: /* no reply expected */
1516 del_timer(&ucs->timer_ctrl);
1517 ucs->pending = 0;
1518 break;
1519 }
1520 spin_unlock_irqrestore(&ucs->lock, flags);
1521 return;
1522
1523 case -ENOENT: /* cancelled */
1524 case -ECONNRESET: /* cancelled (async) */
1525 case -EINPROGRESS: /* pending */
1526 case -ENODEV: /* device removed */
1527 case -ESHUTDOWN: /* device shut down */
1528 /* ignore silently */
1529 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001530 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001531 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001532
1533 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001534 /* don't retry if suspend requested */
1535 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001536 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001537 dev_err(&ucs->interface->dev,
1538 "control request 0x%02x failed: %s\n",
1539 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001540 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001541 break; /* give up */
1542 }
1543 dev_notice(&ucs->interface->dev,
1544 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001545 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001546 ucs->retry_ctrl);
1547 /* urb->dev is clobbered by USB subsystem */
1548 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001549 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001550 if (unlikely(rc)) {
1551 dev_err(&ucs->interface->dev,
1552 "could not resubmit request 0x%02x: %s\n",
1553 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1554 break;
1555 }
1556 /* resubmitted */
1557 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001558 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001559
1560 /* failed, clear pending request */
1561 spin_lock_irqsave(&ucs->lock, flags);
1562 del_timer(&ucs->timer_ctrl);
1563 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001564 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001565 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001566}
1567
1568/* req_submit
1569 * submit a control output request without message buffer to the Gigaset base
1570 * and optionally start a timeout
1571 * parameters:
1572 * bcs B channel control structure
1573 * req control request code (HD_*)
1574 * val control request parameter value (set to 0 if unused)
1575 * timeout timeout in seconds (0: no timeout)
1576 * return value:
1577 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001578 * -EBUSY if another request is pending
1579 * any URB submission error code
1580 */
1581static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1582{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001583 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001584 int ret;
1585 unsigned long flags;
1586
Tilman Schmidt784d5852006-04-10 22:55:04 -07001587 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001588
1589 spin_lock_irqsave(&ucs->lock, flags);
1590 if (ucs->pending) {
1591 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001592 dev_err(bcs->cs->dev,
1593 "submission of request 0x%02x failed: "
1594 "request 0x%02x still pending\n",
1595 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001596 return -EBUSY;
1597 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001598
1599 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1600 ucs->dr_ctrl.bRequest = req;
1601 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1602 ucs->dr_ctrl.wIndex = 0;
1603 ucs->dr_ctrl.wLength = 0;
1604 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001605 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001606 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001607 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001608 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001609 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001610 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001611 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001612 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001613 spin_unlock_irqrestore(&ucs->lock, flags);
1614 return ret;
1615 }
1616 ucs->pending = req;
1617
1618 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001619 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001620 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001621 }
1622
1623 spin_unlock_irqrestore(&ucs->lock, flags);
1624 return 0;
1625}
1626
1627/* gigaset_init_bchannel
1628 * called by common.c to connect a B channel
1629 * initialize isochronous I/O and tell the Gigaset base to open the channel
1630 * argument:
1631 * B channel control structure
1632 * return value:
1633 * 0 on success, error code < 0 on error
1634 */
1635static int gigaset_init_bchannel(struct bc_state *bcs)
1636{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001637 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001638 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001639 unsigned long flags;
1640
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001641 spin_lock_irqsave(&cs->lock, flags);
1642 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001643 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001644 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001645 return -ENODEV;
1646 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001647
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001648 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001649 dev_notice(cs->dev,
1650 "not starting isochronous I/O, "
1651 "suspend in progress\n");
1652 spin_unlock_irqrestore(&cs->lock, flags);
1653 return -EHOSTUNREACH;
1654 }
1655
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001656 ret = starturbs(bcs);
1657 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001658 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001659 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001660 "could not start isochronous I/O for channel B%d: %s\n",
1661 bcs->channel + 1,
1662 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1663 if (ret != -ENODEV)
1664 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001665 return ret;
1666 }
1667
1668 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001669 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1670 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001671 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001672 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001673 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001674 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001675
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001676 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001677 if (ret < 0 && ret != -ENODEV)
1678 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001679 return ret;
1680}
1681
1682/* gigaset_close_bchannel
1683 * called by common.c to disconnect a B channel
1684 * tell the Gigaset base to close the channel
1685 * stopping isochronous I/O and LL notification will be done when the
1686 * acknowledgement for the close arrives
1687 * argument:
1688 * B channel control structure
1689 * return value:
1690 * 0 on success, error code < 0 on error
1691 */
1692static int gigaset_close_bchannel(struct bc_state *bcs)
1693{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001694 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001695 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001696 unsigned long flags;
1697
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001698 spin_lock_irqsave(&cs->lock, flags);
1699 if (unlikely(!cs->connected)) {
1700 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001701 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1702 return -ENODEV;
1703 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001704
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001705 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001706 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001707 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001708 gigaset_bchannel_down(bcs);
1709 return 0;
1710 }
1711
Tilman Schmidt73a88812006-04-22 02:35:30 -07001712 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001713 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001714 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1715 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001716 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001717 bcs->channel + 1);
1718
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001719 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001720 return ret;
1721}
1722
1723/* Device Operations */
1724/* ================= */
1725
1726/* complete_cb
1727 * unqueue first command buffer from queue, waking any sleepers
1728 * must be called with cs->cmdlock held
1729 * parameter:
1730 * cs controller state structure
1731 */
1732static void complete_cb(struct cardstate *cs)
1733{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001734 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001735
1736 /* unqueue completed buffer */
1737 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001738 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001739 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001740 if (cb->next != NULL) {
1741 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001742 cs->cmdbuf->prev = NULL;
1743 cs->curlen = cs->cmdbuf->len;
1744 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001745 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001746 cs->lastcmdbuf = NULL;
1747 cs->curlen = 0;
1748 }
1749
1750 if (cb->wake_tasklet)
1751 tasklet_schedule(cb->wake_tasklet);
1752
1753 kfree(cb);
1754}
1755
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001756/* write_command_callback
1757 * USB completion handler for AT command transmission
1758 * called by the USB subsystem in interrupt context
1759 * parameter:
1760 * urb USB request block of completed request
1761 * urb->context = controller state structure
1762 */
David Howells7d12e782006-10-05 14:55:46 +01001763static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001764{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001765 struct cardstate *cs = urb->context;
1766 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001767 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001768 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001769
Tilman Schmidt73a88812006-04-22 02:35:30 -07001770 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001771 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001772
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001773 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001774 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001775 case 0: /* normal completion */
1776 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001777 case -ENOENT: /* cancelled */
1778 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001779 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001780 case -ENODEV: /* device removed */
1781 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001782 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001783 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001784 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001785 return;
1786 default: /* any failure */
1787 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001788 dev_warn(cs->dev,
1789 "command write: %s, "
1790 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001791 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001792 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001793 break;
1794 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001795 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001796 dev_warn(cs->dev,
1797 "command write: %s, "
1798 "won't retry - suspend requested\n",
1799 get_usb_statmsg(status));
1800 break;
1801 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001802 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001803 dev_warn(cs->dev,
1804 "command write: %s, "
1805 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001806 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001807 break;
1808 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001809 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001810 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001811 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1812 /* resubmitted - bypass regular exit block */
1813 return;
1814 /* command send failed, assume base still waiting */
1815 update_basstate(ucs, BS_ATREADY, 0);
1816 }
1817
1818 spin_lock_irqsave(&cs->cmdlock, flags);
1819 if (cs->cmdbuf != NULL)
1820 complete_cb(cs);
1821 spin_unlock_irqrestore(&cs->cmdlock, flags);
1822}
1823
1824/* atrdy_timeout
1825 * timeout routine for AT command transmission
1826 * argument:
1827 * controller state structure
1828 */
1829static void atrdy_timeout(unsigned long data)
1830{
1831 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001832 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001833
Tilman Schmidt784d5852006-04-10 22:55:04 -07001834 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001835
1836 /* fake the missing signal - what else can I do? */
1837 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1838 start_cbsend(cs);
1839}
1840
1841/* atwrite_submit
1842 * submit an HD_WRITE_ATMESSAGE command URB
1843 * parameters:
1844 * cs controller state structure
1845 * buf buffer containing command to send
1846 * len length of command to send
1847 * return value:
1848 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001849 * -EBUSY if another request is pending
1850 * any URB submission error code
1851 */
1852static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1853{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001854 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001855 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001856
Tilman Schmidt784d5852006-04-10 22:55:04 -07001857 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001858
Tilman Schmidt73a88812006-04-22 02:35:30 -07001859 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001860 dev_err(cs->dev,
1861 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001862 return -EBUSY;
1863 }
1864
1865 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1866 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1867 ucs->dr_cmd_out.wValue = 0;
1868 ucs->dr_cmd_out.wIndex = 0;
1869 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1870 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1871 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001872 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001873 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001874 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001875 if (unlikely(rc)) {
1876 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001877 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001878 get_usb_rcmsg(rc));
1879 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001880 }
1881
Tilman Schmidt73a88812006-04-22 02:35:30 -07001882 /* submitted successfully, start timeout if necessary */
1883 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001884 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1885 ATRDY_TIMEOUT);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001886 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001887 }
1888 return 0;
1889}
1890
1891/* start_cbsend
1892 * start transmission of AT command queue if necessary
1893 * parameter:
1894 * cs controller state structure
1895 * return value:
1896 * 0 on success
1897 * error code < 0 on error
1898 */
1899static int start_cbsend(struct cardstate *cs)
1900{
1901 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001902 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001903 unsigned long flags;
1904 int rc;
1905 int retval = 0;
1906
Tilman Schmidt024fd292008-02-06 01:38:26 -08001907 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001908 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001909 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001910 return -EHOSTUNREACH;
1911 }
1912
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001913 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001914 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001915 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001916 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1917 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001918 /* flush command queue */
1919 spin_lock_irqsave(&cs->cmdlock, flags);
1920 while (cs->cmdbuf != NULL)
1921 complete_cb(cs);
1922 spin_unlock_irqrestore(&cs->cmdlock, flags);
1923 }
1924 return rc;
1925 }
1926
1927 /* try to send first command in queue */
1928 spin_lock_irqsave(&cs->cmdlock, flags);
1929
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001930 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001931 ucs->retry_cmd_out = 0;
1932 rc = atwrite_submit(cs, cb->buf, cb->len);
1933 if (unlikely(rc)) {
1934 retval = rc;
1935 complete_cb(cs);
1936 }
1937 }
1938
1939 spin_unlock_irqrestore(&cs->cmdlock, flags);
1940 return retval;
1941}
1942
1943/* gigaset_write_cmd
1944 * This function is called by the device independent part of the driver
1945 * to transmit an AT command string to the Gigaset device.
1946 * It encapsulates the device specific method for transmission over the
1947 * direct USB connection to the base.
1948 * The command string is added to the queue of commands to send, and
1949 * USB transmission is started if necessary.
1950 * parameters:
1951 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001952 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001953 * return value:
1954 * number of bytes queued on success
1955 * error code < 0 on error
1956 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001957static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001958{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001959 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001960 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001961
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001962 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001963 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001964 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001965
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001966 /* translate "+++" escape sequence sent as a single separate command
1967 * into "close AT channel" command for error recovery
1968 * The next command will reopen the AT channel automatically.
1969 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001970 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidt4cb5e422010-09-30 13:35:31 +00001971 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1972 * because of an error, the base never sends another one.
1973 * The response channel is thus effectively blocked.
1974 * Closing and reopening the AT channel does *not* clear
1975 * this condition.
1976 * As a stopgap measure, submit a zero-length AT read
1977 * before closing the AT channel. This has the undocumented
1978 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1979 * from the base if necessary.
1980 * The subsequent AT channel close then discards any pending
1981 * messages.
1982 */
1983 spin_lock_irqsave(&cs->lock, flags);
1984 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1985 kfree(cs->hw.bas->rcvbuf);
1986 cs->hw.bas->rcvbuf = NULL;
1987 cs->hw.bas->rcvbuf_size = 0;
1988 cs->hw.bas->retry_cmd_in = 0;
1989 atread_submit(cs, 0);
1990 }
1991 spin_unlock_irqrestore(&cs->lock, flags);
1992
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001993 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001994 if (cb->wake_tasklet)
1995 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00001996 if (!rc)
1997 rc = cb->len;
1998 kfree(cb);
1999 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00002000 }
2001
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002002 spin_lock_irqsave(&cs->cmdlock, flags);
2003 cb->prev = cs->lastcmdbuf;
2004 if (cs->lastcmdbuf)
2005 cs->lastcmdbuf->next = cb;
2006 else {
2007 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002008 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002009 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002010 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002011 cs->lastcmdbuf = cb;
2012 spin_unlock_irqrestore(&cs->cmdlock, flags);
2013
Tilman Schmidt73a88812006-04-22 02:35:30 -07002014 spin_lock_irqsave(&cs->lock, flags);
2015 if (unlikely(!cs->connected)) {
2016 spin_unlock_irqrestore(&cs->lock, flags);
2017 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002018 /* flush command queue */
2019 spin_lock_irqsave(&cs->cmdlock, flags);
2020 while (cs->cmdbuf != NULL)
2021 complete_cb(cs);
2022 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002023 return -ENODEV;
2024 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002025 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002026 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002027 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002028}
2029
2030/* gigaset_write_room
2031 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07002032 * return number of characters the driver will accept to be written via
2033 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002034 * parameter:
2035 * controller state structure
2036 * return value:
2037 * number of characters
2038 */
2039static int gigaset_write_room(struct cardstate *cs)
2040{
2041 return IF_WRITEBUF;
2042}
2043
2044/* gigaset_chars_in_buffer
2045 * tty_driver.chars_in_buffer interface routine
2046 * return number of characters waiting to be sent
2047 * parameter:
2048 * controller state structure
2049 * return value:
2050 * number of characters
2051 */
2052static int gigaset_chars_in_buffer(struct cardstate *cs)
2053{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002054 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002055}
2056
2057/* gigaset_brkchars
2058 * implementation of ioctl(GIGASET_BRKCHARS)
2059 * parameter:
2060 * controller state structure
2061 * return value:
2062 * -EINVAL (unimplemented function)
2063 */
2064static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2065{
2066 return -EINVAL;
2067}
2068
2069
2070/* Device Initialization/Shutdown */
2071/* ============================== */
2072
2073/* Free hardware dependent part of the B channel structure
2074 * parameter:
2075 * bcs B channel structure
2076 * return value:
2077 * !=0 on success
2078 */
2079static int gigaset_freebcshw(struct bc_state *bcs)
2080{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002081 struct bas_bc_state *ubc = bcs->hw.bas;
2082 int i;
2083
2084 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002085 return 0;
2086
Tilman Schmidt73a88812006-04-22 02:35:30 -07002087 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002088 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002089 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2090 for (i = 0; i < BAS_OUTURBS; ++i) {
2091 usb_kill_urb(ubc->isoouturbs[i].urb);
2092 usb_free_urb(ubc->isoouturbs[i].urb);
2093 }
2094 for (i = 0; i < BAS_INURBS; ++i) {
2095 usb_kill_urb(ubc->isoinurbs[i]);
2096 usb_free_urb(ubc->isoinurbs[i]);
2097 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002098 tasklet_kill(&ubc->sent_tasklet);
2099 tasklet_kill(&ubc->rcvd_tasklet);
2100 kfree(ubc->isooutbuf);
2101 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002102 bcs->hw.bas = NULL;
2103 return 1;
2104}
2105
2106/* Initialize hardware dependent part of the B channel structure
2107 * parameter:
2108 * bcs B channel structure
2109 * return value:
2110 * !=0 on success
2111 */
2112static int gigaset_initbcshw(struct bc_state *bcs)
2113{
2114 int i;
2115 struct bas_bc_state *ubc;
2116
2117 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2118 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002119 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002120 return 0;
2121 }
2122
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002123 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002124 atomic_set(&ubc->corrbytes, 0);
2125 spin_lock_init(&ubc->isooutlock);
2126 for (i = 0; i < BAS_OUTURBS; ++i) {
2127 ubc->isoouturbs[i].urb = NULL;
2128 ubc->isoouturbs[i].bcs = bcs;
2129 }
2130 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2131 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002132 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2133 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002134 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002135 kfree(ubc);
2136 bcs->hw.bas = NULL;
2137 return 0;
2138 }
2139 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002140 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002141
2142 spin_lock_init(&ubc->isoinlock);
2143 for (i = 0; i < BAS_INURBS; ++i)
2144 ubc->isoinurbs[i] = NULL;
2145 ubc->isoindone = NULL;
2146 ubc->loststatus = -EINPROGRESS;
2147 ubc->isoinlost = 0;
2148 ubc->seqlen = 0;
2149 ubc->inbyte = 0;
2150 ubc->inbits = 0;
2151 ubc->goodbytes = 0;
2152 ubc->alignerrs = 0;
2153 ubc->fcserrs = 0;
2154 ubc->frameerrs = 0;
2155 ubc->giants = 0;
2156 ubc->runts = 0;
2157 ubc->aborts = 0;
2158 ubc->shared0s = 0;
2159 ubc->stolen0s = 0;
2160 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002161 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002162 return 1;
2163}
2164
2165static void gigaset_reinitbcshw(struct bc_state *bcs)
2166{
2167 struct bas_bc_state *ubc = bcs->hw.bas;
2168
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002169 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002170 atomic_set(&bcs->hw.bas->corrbytes, 0);
2171 bcs->hw.bas->numsub = 0;
2172 spin_lock_init(&ubc->isooutlock);
2173 spin_lock_init(&ubc->isoinlock);
2174 ubc->loststatus = -EINPROGRESS;
2175}
2176
2177static void gigaset_freecshw(struct cardstate *cs)
2178{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002179 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002180 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002181 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002182 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002183}
2184
2185static int gigaset_initcshw(struct cardstate *cs)
2186{
2187 struct bas_cardstate *ucs;
2188
2189 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002190 if (!ucs) {
2191 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002192 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002193 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002194 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2195 if (!ucs->int_in_buf) {
2196 kfree(ucs);
2197 pr_err("out of memory\n");
2198 return 0;
2199 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002200
2201 ucs->urb_cmd_in = NULL;
2202 ucs->urb_cmd_out = NULL;
2203 ucs->rcvbuf = NULL;
2204 ucs->rcvbuf_size = 0;
2205
2206 spin_lock_init(&ucs->lock);
2207 ucs->pending = 0;
2208
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002209 ucs->basstate = 0;
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002210 setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
2211 setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
2212 setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002213 setup_timer(&ucs->timer_int_in, int_in_resubmit, (unsigned long) cs);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002214 init_waitqueue_head(&ucs->waitqueue);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002215 INIT_WORK(&ucs->int_in_wq, int_in_work);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002216
2217 return 1;
2218}
2219
2220/* freeurbs
2221 * unlink and deallocate all URBs unconditionally
2222 * caller must make sure that no commands are still in progress
2223 * parameter:
2224 * cs controller state structure
2225 */
2226static void freeurbs(struct cardstate *cs)
2227{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002228 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002229 struct bas_bc_state *ubc;
2230 int i, j;
2231
Tilman Schmidt39f07222006-12-13 00:33:52 -08002232 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002233 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002234 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002235 for (i = 0; i < BAS_OUTURBS; ++i) {
2236 usb_kill_urb(ubc->isoouturbs[i].urb);
2237 usb_free_urb(ubc->isoouturbs[i].urb);
2238 ubc->isoouturbs[i].urb = NULL;
2239 }
2240 for (i = 0; i < BAS_INURBS; ++i) {
2241 usb_kill_urb(ubc->isoinurbs[i]);
2242 usb_free_urb(ubc->isoinurbs[i]);
2243 ubc->isoinurbs[i] = NULL;
2244 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002245 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002246 usb_kill_urb(ucs->urb_int_in);
2247 usb_free_urb(ucs->urb_int_in);
2248 ucs->urb_int_in = NULL;
2249 usb_kill_urb(ucs->urb_cmd_out);
2250 usb_free_urb(ucs->urb_cmd_out);
2251 ucs->urb_cmd_out = NULL;
2252 usb_kill_urb(ucs->urb_cmd_in);
2253 usb_free_urb(ucs->urb_cmd_in);
2254 ucs->urb_cmd_in = NULL;
2255 usb_kill_urb(ucs->urb_ctrl);
2256 usb_free_urb(ucs->urb_ctrl);
2257 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002258}
2259
2260/* gigaset_probe
2261 * This function is called when a new USB device is connected.
2262 * It checks whether the new device is handled by this driver.
2263 */
2264static int gigaset_probe(struct usb_interface *interface,
2265 const struct usb_device_id *id)
2266{
2267 struct usb_host_interface *hostif;
2268 struct usb_device *udev = interface_to_usbdev(interface);
2269 struct cardstate *cs = NULL;
2270 struct bas_cardstate *ucs = NULL;
2271 struct bas_bc_state *ubc;
2272 struct usb_endpoint_descriptor *endpoint;
2273 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002274 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002275
Tilman Schmidt1528b182010-02-22 13:09:52 +00002276 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002277 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2278 __func__, le16_to_cpu(udev->descriptor.idVendor),
2279 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002280
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002281 /* set required alternate setting */
2282 hostif = interface->cur_altsetting;
2283 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002284 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002285 "%s: wrong alternate setting %d - trying to switch",
2286 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002287 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2288 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002289 dev_warn(&udev->dev, "usb_set_interface failed, "
2290 "device %d interface %d altsetting %d\n",
2291 udev->devnum, hostif->desc.bInterfaceNumber,
2292 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002293 return -ENODEV;
2294 }
2295 hostif = interface->cur_altsetting;
2296 }
2297
2298 /* Reject application specific interfaces
2299 */
2300 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002301 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2302 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002303 return -ENODEV;
2304 }
2305
Tilman Schmidt784d5852006-04-10 22:55:04 -07002306 dev_info(&udev->dev,
2307 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2308 __func__, le16_to_cpu(udev->descriptor.idVendor),
2309 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002310
Tilman Schmidte468c042008-02-06 01:38:29 -08002311 /* allocate memory for our device state and intialize it */
2312 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2313 GIGASET_MODULENAME);
2314 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002315 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002316 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002317
2318 /* save off device structure ptrs for later use */
2319 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002320 ucs->udev = udev;
2321 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002322 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323
2324 /* allocate URBs:
2325 * - one for the interrupt pipe
2326 * - three for the different uses of the default control pipe
2327 * - three for each isochronous pipe
2328 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002329 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2330 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2331 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2332 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002333 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002334
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002335 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002336 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002337 for (i = 0; i < BAS_OUTURBS; ++i)
2338 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002339 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002340 goto allocerr;
2341 for (i = 0; i < BAS_INURBS; ++i)
2342 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002343 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002344 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002345 }
2346
2347 ucs->rcvbuf = NULL;
2348 ucs->rcvbuf_size = 0;
2349
2350 /* Fill the interrupt urb and send it to the core */
2351 endpoint = &hostif->endpoint[0].desc;
2352 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002353 usb_rcvintpipe(udev,
2354 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002355 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002356 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002357 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2358 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002359 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002360 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002361 goto error;
2362 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002363 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002364
2365 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002366 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2367 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002368 goto error;
2369
2370 /* tell common part that the device is ready */
2371 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002372 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002373
2374 /* save address of controller structure */
2375 usb_set_intfdata(interface, cs);
2376
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002377 if (!gigaset_start(cs))
2378 goto error;
2379
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002380 return 0;
2381
Tilman Schmidt73a88812006-04-22 02:35:30 -07002382allocerr:
2383 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002384error:
2385 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002386 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002387 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002388 return -ENODEV;
2389}
2390
2391/* gigaset_disconnect
2392 * This function is called when the Gigaset base is unplugged.
2393 */
2394static void gigaset_disconnect(struct usb_interface *interface)
2395{
2396 struct cardstate *cs;
2397 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002398 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002399
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002400 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002401
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002402 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002403
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002404 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002405
2406 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002407 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002408
2409 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002410 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002411 gigaset_bchannel_down(cs->bcs + j);
2412
2413 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002414 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002415
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002416 /* stop delayed work and URBs, free ressources */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002417 del_timer_sync(&ucs->timer_ctrl);
2418 del_timer_sync(&ucs->timer_atrdy);
2419 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002420 del_timer_sync(&ucs->timer_int_in);
2421 cancel_work_sync(&ucs->int_in_wq);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002422 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002423 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002424 kfree(ucs->rcvbuf);
2425 ucs->rcvbuf = NULL;
2426 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002427 usb_put_dev(ucs->udev);
2428 ucs->interface = NULL;
2429 ucs->udev = NULL;
2430 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002431 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002432}
2433
Tilman Schmidt024fd292008-02-06 01:38:26 -08002434/* gigaset_suspend
2435 * This function is called before the USB connection is suspended.
2436 */
2437static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2438{
2439 struct cardstate *cs = usb_get_intfdata(intf);
2440 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002441 int rc;
2442
2443 /* set suspend flag; this stops AT command/response traffic */
2444 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2445 gig_dbg(DEBUG_SUSPEND, "already suspended");
2446 return 0;
2447 }
2448
2449 /* wait a bit for blocking conditions to go away */
2450 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002451 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002452 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2453 BAS_TIMEOUT*HZ/10);
2454 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2455
2456 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002457 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002458 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002459 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002460 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002461 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002462 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002463 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002464 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002465 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002466 dev_warn(cs->dev, " sending AT command\n");
2467 update_basstate(ucs, 0, BS_SUSPEND);
2468 return -EBUSY;
2469 }
2470
2471 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002472 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002473 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2474 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2475 if (rc) {
2476 update_basstate(ucs, 0, BS_SUSPEND);
2477 return rc;
2478 }
2479 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2480 BAS_TIMEOUT*HZ/10);
2481 /* in case of timeout, proceed anyway */
2482 }
2483
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002484 /* kill all URBs and delayed work that might still be pending */
Tilman Schmidt024fd292008-02-06 01:38:26 -08002485 usb_kill_urb(ucs->urb_ctrl);
2486 usb_kill_urb(ucs->urb_int_in);
2487 del_timer_sync(&ucs->timer_ctrl);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002488 del_timer_sync(&ucs->timer_atrdy);
2489 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002490 del_timer_sync(&ucs->timer_int_in);
2491 cancel_work_sync(&ucs->int_in_wq);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002492
2493 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2494 return 0;
2495}
2496
2497/* gigaset_resume
2498 * This function is called after the USB connection has been resumed.
2499 */
2500static int gigaset_resume(struct usb_interface *intf)
2501{
2502 struct cardstate *cs = usb_get_intfdata(intf);
2503 struct bas_cardstate *ucs = cs->hw.bas;
2504 int rc;
2505
2506 /* resubmit interrupt URB for spontaneous messages from base */
2507 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2508 if (rc) {
2509 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2510 get_usb_rcmsg(rc));
2511 return rc;
2512 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002513 ucs->retry_int_in = 0;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002514
2515 /* clear suspend flag to reallow activity */
2516 update_basstate(ucs, 0, BS_SUSPEND);
2517
2518 gig_dbg(DEBUG_SUSPEND, "resume complete");
2519 return 0;
2520}
2521
2522/* gigaset_pre_reset
2523 * This function is called before the USB connection is reset.
2524 */
2525static int gigaset_pre_reset(struct usb_interface *intf)
2526{
2527 /* handle just like suspend */
2528 return gigaset_suspend(intf, PMSG_ON);
2529}
2530
2531/* gigaset_post_reset
2532 * This function is called after the USB connection has been reset.
2533 */
2534static int gigaset_post_reset(struct usb_interface *intf)
2535{
2536 /* FIXME: send HD_DEVICE_INIT_ACK? */
2537
2538 /* resume operations */
2539 return gigaset_resume(intf);
2540}
2541
2542
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002543static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002544 gigaset_write_cmd,
2545 gigaset_write_room,
2546 gigaset_chars_in_buffer,
2547 gigaset_brkchars,
2548 gigaset_init_bchannel,
2549 gigaset_close_bchannel,
2550 gigaset_initbcshw,
2551 gigaset_freebcshw,
2552 gigaset_reinitbcshw,
2553 gigaset_initcshw,
2554 gigaset_freecshw,
2555 gigaset_set_modem_ctrl,
2556 gigaset_baud_rate,
2557 gigaset_set_line_ctrl,
2558 gigaset_isoc_send_skb,
2559 gigaset_isoc_input,
2560};
2561
2562/* bas_gigaset_init
2563 * This function is called after the kernel module is loaded.
2564 */
2565static int __init bas_gigaset_init(void)
2566{
2567 int result;
2568
2569 /* allocate memory for our driver state and intialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002570 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2571 GIGASET_MODULENAME, GIGASET_DEVNAME,
2572 &gigops, THIS_MODULE);
2573 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002574 goto error;
2575
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002576 /* register this driver with the USB subsystem */
2577 result = usb_register(&gigaset_usb_driver);
2578 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002579 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002580 goto error;
2581 }
2582
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002583 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002584 return 0;
2585
Tilman Schmidte468c042008-02-06 01:38:29 -08002586error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002587 if (driver)
2588 gigaset_freedriver(driver);
2589 driver = NULL;
2590 return -1;
2591}
2592
2593/* bas_gigaset_exit
2594 * This function is called before the kernel module is unloaded.
2595 */
2596static void __exit bas_gigaset_exit(void)
2597{
Tilman Schmidte468c042008-02-06 01:38:29 -08002598 struct bas_cardstate *ucs;
2599 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002600
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002601 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002602 * => no gigaset_start any more
2603 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002604
Tilman Schmidte468c042008-02-06 01:38:29 -08002605 /* stop all connected devices */
2606 for (i = 0; i < driver->minors; i++) {
2607 if (gigaset_shutdown(driver->cs + i) < 0)
2608 continue; /* no device */
2609 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002610
Tilman Schmidte468c042008-02-06 01:38:29 -08002611 /* close all still open channels */
2612 ucs = driver->cs[i].hw.bas;
2613 if (ucs->basstate & BS_B1OPEN) {
2614 gig_dbg(DEBUG_INIT, "closing B1 channel");
2615 usb_control_msg(ucs->udev,
2616 usb_sndctrlpipe(ucs->udev, 0),
2617 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2618 0, 0, NULL, 0, BAS_TIMEOUT);
2619 }
2620 if (ucs->basstate & BS_B2OPEN) {
2621 gig_dbg(DEBUG_INIT, "closing B2 channel");
2622 usb_control_msg(ucs->udev,
2623 usb_sndctrlpipe(ucs->udev, 0),
2624 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2625 0, 0, NULL, 0, BAS_TIMEOUT);
2626 }
2627 if (ucs->basstate & BS_ATOPEN) {
2628 gig_dbg(DEBUG_INIT, "closing AT channel");
2629 usb_control_msg(ucs->udev,
2630 usb_sndctrlpipe(ucs->udev, 0),
2631 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2632 0, 0, NULL, 0, BAS_TIMEOUT);
2633 }
2634 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002635 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002636
2637 /* deregister this driver with the USB subsystem */
2638 usb_deregister(&gigaset_usb_driver);
2639 /* this will call the disconnect-callback */
2640 /* from now on, no disconnect/probe callback should be running */
2641
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002642 gigaset_freedriver(driver);
2643 driver = NULL;
2644}
2645
2646
2647module_init(bas_gigaset_init);
2648module_exit(bas_gigaset_exit);
2649
2650MODULE_AUTHOR(DRIVER_AUTHOR);
2651MODULE_DESCRIPTION(DRIVER_DESC);
2652MODULE_LICENSE("GPL");