blob: e865c5dc0282c87e4bc7c0747ed944fc77e20cbc [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;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800112
113 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800114 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800115 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800116 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800117 int rcvbuf_size; /* size of AT receive buffer */
118 /* 0: no receive in progress */
119 int retry_cmd_in; /* receive req retry count */
120};
121
122/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700123#define BS_ATOPEN 0x001 /* AT channel open */
124#define BS_B1OPEN 0x002 /* B channel 1 open */
125#define BS_B2OPEN 0x004 /* B channel 2 open */
126#define BS_ATREADY 0x008 /* base ready for AT command */
127#define BS_INIT 0x010 /* base has signalled INIT_OK */
128#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
129#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
130#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800131#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000132#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800133
134
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000135static struct gigaset_driver *driver;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800136
137/* usb specific object needed to register this driver with the usb subsystem */
138static struct usb_driver gigaset_usb_driver = {
139 .name = GIGASET_MODULENAME,
140 .probe = gigaset_probe,
141 .disconnect = gigaset_disconnect,
142 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800143 .suspend = gigaset_suspend,
144 .resume = gigaset_resume,
145 .reset_resume = gigaset_post_reset,
146 .pre_reset = gigaset_pre_reset,
147 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800148};
149
Tilman Schmidt73a88812006-04-22 02:35:30 -0700150/* get message text for usb_submit_urb return code
151 */
152static char *get_usb_rcmsg(int rc)
153{
154 static char unkmsg[28];
155
156 switch (rc) {
157 case 0:
158 return "success";
159 case -ENOMEM:
160 return "out of memory";
161 case -ENODEV:
162 return "device not present";
163 case -ENOENT:
164 return "endpoint not present";
165 case -ENXIO:
166 return "URB type not supported";
167 case -EINVAL:
168 return "invalid argument";
169 case -EAGAIN:
170 return "start frame too early or too much scheduled";
171 case -EFBIG:
172 return "too many isochronous frames requested";
173 case -EPIPE:
174 return "endpoint stalled";
175 case -EMSGSIZE:
176 return "invalid packet size";
177 case -ENOSPC:
178 return "would overcommit USB bandwidth";
179 case -ESHUTDOWN:
180 return "device shut down";
181 case -EPERM:
182 return "reject flag set";
183 case -EHOSTUNREACH:
184 return "device suspended";
185 default:
186 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
187 return unkmsg;
188 }
189}
190
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800191/* get message text for USB status code
192 */
193static char *get_usb_statmsg(int status)
194{
195 static char unkmsg[28];
196
197 switch (status) {
198 case 0:
199 return "success";
200 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700201 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800202 case -EINPROGRESS:
203 return "pending";
204 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700205 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800206 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700208 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700210 case -EPIPE:
211 return "endpoint stalled";
212 case -ECOMM:
213 return "IN buffer overrun";
214 case -ENOSR:
215 return "OUT buffer underrun";
216 case -EOVERFLOW:
217 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800218 case -EREMOTEIO:
219 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700220 case -ENODEV:
221 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800222 case -EXDEV:
223 return "partial isochronous transfer";
224 case -EINVAL:
225 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700226 case -ECONNRESET:
227 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800228 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700229 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800230 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 return unkmsg;
233 }
234}
235
236/* usb_pipetype_str
237 * retrieve string representation of USB pipe type
238 */
239static inline char *usb_pipetype_str(int pipe)
240{
241 if (usb_pipeisoc(pipe))
242 return "Isoc";
243 if (usb_pipeint(pipe))
244 return "Int";
245 if (usb_pipecontrol(pipe))
246 return "Ctrl";
247 if (usb_pipebulk(pipe))
248 return "Bulk";
249 return "?";
250}
251
252/* dump_urb
253 * write content of URB to syslog for debugging
254 */
255static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700256 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800257{
258#ifdef CONFIG_GIGASET_DEBUG
259 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700260 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800261 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 gig_dbg(level,
263 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800264 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 (unsigned long) urb->dev,
266 usb_pipetype_str(urb->pipe),
267 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
268 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800269 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700270 urb->transfer_flags);
271 gig_dbg(level,
272 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800273 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700274 (unsigned long) urb->transfer_buffer,
275 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800276 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 gig_dbg(level,
278 " start_frame=%d, number_of_packets=%d, interval=%d, "
279 "error_count=%d,",
280 urb->start_frame, urb->number_of_packets, urb->interval,
281 urb->error_count);
282 gig_dbg(level,
283 " context=0x%08lx, complete=0x%08lx, "
284 "iso_frame_desc[]={",
285 (unsigned long) urb->context,
286 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800287 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700288 struct usb_iso_packet_descriptor *pifd
289 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700290 gig_dbg(level,
291 " {offset=%u, length=%u, actual_length=%u, "
292 "status=%u}",
293 pifd->offset, pifd->length, pifd->actual_length,
294 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800295 }
296 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700297 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800298#endif
299}
300
301/* read/set modem control bits etc. (m10x only) */
302static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700303 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800304{
305 return -EINVAL;
306}
307
308static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
309{
310 return -EINVAL;
311}
312
313static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000318/* set/clear bits in base connection state, return previous state
319 */
320static inline int update_basstate(struct bas_cardstate *ucs,
321 int set, int clear)
322{
323 unsigned long flags;
324 int state;
325
326 spin_lock_irqsave(&ucs->lock, flags);
327 state = ucs->basstate;
328 ucs->basstate = (state & ~clear) | set;
329 spin_unlock_irqrestore(&ucs->lock, flags);
330 return state;
331}
332
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800333/* error_hangup
334 * hang up any existing connection because of an unrecoverable error
335 * This function may be called from any context and takes care of scheduling
336 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700337 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800338 * argument:
339 * B channel control structure
340 */
341static inline void error_hangup(struct bc_state *bcs)
342{
343 struct cardstate *cs = bcs->cs;
344
Tilman Schmidt1528b182010-02-22 13:09:52 +0000345 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800346 gigaset_schedule_event(cs);
347}
348
349/* error_reset
350 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700351 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700352 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700353 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800354 * argument:
355 * controller state structure
356 */
357static inline void error_reset(struct cardstate *cs)
358{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000359 /* reset interrupt pipe to recover (ignore errors) */
360 update_basstate(cs->hw.bas, BS_RESETTING, 0);
361 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800362}
363
364/* check_pending
365 * check for completion of pending control request
366 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700367 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800368 */
369static void check_pending(struct bas_cardstate *ucs)
370{
371 unsigned long flags;
372
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 spin_lock_irqsave(&ucs->lock, flags);
374 switch (ucs->pending) {
375 case 0:
376 break;
377 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800378 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800379 ucs->pending = 0;
380 break;
381 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800382 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800383 ucs->pending = 0;
384 break;
385 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800386 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800387 ucs->pending = 0;
388 break;
389 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800390 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800391 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 break;
393 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800394 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800395 ucs->pending = 0;
396 break;
397 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800398 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800399 ucs->pending = 0;
400 break;
401 case HD_DEVICE_INIT_ACK: /* no reply expected */
402 ucs->pending = 0;
403 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000404 case HD_RESET_INTERRUPT_PIPE:
405 if (!(ucs->basstate & BS_RESETTING))
406 ucs->pending = 0;
407 break;
408 /*
409 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
410 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411 */
412 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700413 dev_warn(&ucs->interface->dev,
414 "unknown pending request 0x%02x cleared\n",
415 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800416 ucs->pending = 0;
417 }
418
419 if (!ucs->pending)
420 del_timer(&ucs->timer_ctrl);
421
422 spin_unlock_irqrestore(&ucs->lock, flags);
423}
424
425/* cmd_in_timeout
426 * timeout routine for command input request
427 * argument:
428 * controller state structure
429 */
430static void cmd_in_timeout(unsigned long data)
431{
432 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700433 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700434 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800435
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800436 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700437 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800438 return;
439 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000441 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700442 dev_err(cs->dev,
443 "control read: timeout, giving up after %d tries\n",
444 ucs->retry_cmd_in);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000445 kfree(ucs->rcvbuf);
446 ucs->rcvbuf = NULL;
447 ucs->rcvbuf_size = 0;
448 error_reset(cs);
449 return;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700450 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000451
452 gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
453 __func__, ucs->retry_cmd_in);
454 rc = atread_submit(cs, BAS_TIMEOUT);
455 if (rc < 0) {
456 kfree(ucs->rcvbuf);
457 ucs->rcvbuf = NULL;
458 ucs->rcvbuf_size = 0;
459 if (rc != -ENODEV)
460 error_reset(cs);
461 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800462}
463
Tilman Schmidt06163f82006-06-26 00:25:33 -0700464/* read_ctrl_callback
465 * USB completion handler for control pipe input
466 * called by the USB subsystem in interrupt context
467 * parameter:
468 * urb USB request block
469 * urb->context = inbuf structure for controller state
470 */
David Howells7d12e782006-10-05 14:55:46 +0100471static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700472{
473 struct inbuf_t *inbuf = urb->context;
474 struct cardstate *cs = inbuf->cs;
475 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800476 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700477 unsigned numbytes;
478 int rc;
479
480 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800481 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700482 del_timer(&ucs->timer_cmd_in);
483
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800484 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700485 case 0: /* normal completion */
486 numbytes = urb->actual_length;
487 if (unlikely(numbytes != ucs->rcvbuf_size)) {
488 dev_warn(cs->dev,
489 "control read: received %d chars, expected %d\n",
490 numbytes, ucs->rcvbuf_size);
491 if (numbytes > ucs->rcvbuf_size)
492 numbytes = ucs->rcvbuf_size;
493 }
494
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000495 /* copy received bytes to inbuf, notify event layer */
496 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
497 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
498 gigaset_schedule_event(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700499 }
500 break;
501
502 case -ENOENT: /* cancelled */
503 case -ECONNRESET: /* cancelled (async) */
504 case -EINPROGRESS: /* pending */
505 case -ENODEV: /* device removed */
506 case -ESHUTDOWN: /* device shut down */
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000507 /* no further action necessary */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700508 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800509 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700510 break;
511
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000512 default: /* other errors: retry */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700513 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000514 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
515 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700516 rc = atread_submit(cs, BAS_TIMEOUT);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000517 if (rc >= 0)
518 /* successfully resubmitted, skip freeing */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700519 return;
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000520 if (rc == -ENODEV)
521 /* disconnect, no further action necessary */
522 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700523 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000524 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
525 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700526 error_reset(cs);
527 }
528
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000529 /* read finished, free buffer */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700530 kfree(ucs->rcvbuf);
531 ucs->rcvbuf = NULL;
532 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700533}
534
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800535/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700536 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800537 * parameters:
538 * cs controller state structure
539 * timeout timeout in 1/10 sec., 0: none
540 * return value:
541 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800542 * -EBUSY if another request is pending
543 * any URB submission error code
544 */
545static int atread_submit(struct cardstate *cs, int timeout)
546{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700547 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800548 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800549 int ret;
550
Tilman Schmidt784d5852006-04-10 22:55:04 -0700551 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
552 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800553
Tilman Schmidt024fd292008-02-06 01:38:26 -0800554 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
555 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700556 dev_err(cs->dev,
557 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800558 return -EBUSY;
559 }
560
Tilman Schmidt024fd292008-02-06 01:38:26 -0800561 if (basstate & BS_SUSPEND) {
562 dev_notice(cs->dev,
563 "HD_READ_ATMESSAGE not submitted, "
564 "suspend in progress\n");
565 update_basstate(ucs, 0, BS_ATRDPEND);
566 /* treat like disconnect */
567 return -ENODEV;
568 }
569
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800570 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
571 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
572 ucs->dr_cmd_in.wValue = 0;
573 ucs->dr_cmd_in.wIndex = 0;
574 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
575 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700576 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000577 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 ucs->rcvbuf, ucs->rcvbuf_size,
579 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000581 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
582 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700583 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700584 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700585 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800586 return ret;
587 }
588
589 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700590 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +0000591 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592 }
593 return 0;
594}
595
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800596/* read_int_callback
597 * USB completion handler for interrupt pipe input
598 * called by the USB subsystem in interrupt context
599 * parameter:
600 * urb USB request block
601 * urb->context = controller state structure
602 */
David Howells7d12e782006-10-05 14:55:46 +0100603static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800604{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700605 struct cardstate *cs = urb->context;
606 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800608 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800609 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700610 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800611 unsigned l;
612 int channel;
613
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800614 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800615 case 0: /* success */
616 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700617 case -ENOENT: /* cancelled */
618 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800619 case -EINPROGRESS: /* pending */
620 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700621 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800622 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800623 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700624 case -ENODEV: /* device removed */
625 case -ESHUTDOWN: /* device shut down */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700626 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
627 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800628 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700629 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800630 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800631 goto resubmit;
632 }
633
Tilman Schmidt73a88812006-04-22 02:35:30 -0700634 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700635 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700636 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
637 urb->actual_length);
638 goto resubmit;
639 }
640
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800641 l = (unsigned) ucs->int_in_buf[1] +
642 (((unsigned) ucs->int_in_buf[2]) << 8);
643
Tilman Schmidt784d5852006-04-10 22:55:04 -0700644 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
645 urb->actual_length, (int)ucs->int_in_buf[0], l,
646 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800647
648 channel = 0;
649
650 switch (ucs->int_in_buf[0]) {
651 case HD_DEVICE_INIT_OK:
652 update_basstate(ucs, BS_INIT, 0);
653 break;
654
655 case HD_READY_SEND_ATDATA:
656 del_timer(&ucs->timer_atrdy);
657 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
658 start_cbsend(cs);
659 break;
660
661 case HD_OPEN_B2CHANNEL_ACK:
662 ++channel;
663 case HD_OPEN_B1CHANNEL_ACK:
664 bcs = cs->bcs + channel;
665 update_basstate(ucs, BS_B1OPEN << channel, 0);
666 gigaset_bchannel_up(bcs);
667 break;
668
669 case HD_OPEN_ATCHANNEL_ACK:
670 update_basstate(ucs, BS_ATOPEN, 0);
671 start_cbsend(cs);
672 break;
673
674 case HD_CLOSE_B2CHANNEL_ACK:
675 ++channel;
676 case HD_CLOSE_B1CHANNEL_ACK:
677 bcs = cs->bcs + channel;
678 update_basstate(ucs, 0, BS_B1OPEN << channel);
679 stopurbs(bcs->hw.bas);
680 gigaset_bchannel_down(bcs);
681 break;
682
683 case HD_CLOSE_ATCHANNEL_ACK:
684 update_basstate(ucs, 0, BS_ATOPEN);
685 break;
686
687 case HD_B2_FLOW_CONTROL:
688 ++channel;
689 case HD_B1_FLOW_CONTROL:
690 bcs = cs->bcs + channel;
691 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700692 &bcs->hw.bas->corrbytes);
693 gig_dbg(DEBUG_ISO,
694 "Flow control (channel %d, sub %d): 0x%02x => %d",
695 channel, bcs->hw.bas->numsub, l,
696 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800697 break;
698
699 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
700 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700701 dev_warn(cs->dev,
702 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800703 break;
704 }
705 spin_lock_irqsave(&cs->lock, flags);
706 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700707 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700708 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700709 "receive AT data overrun, %d bytes lost\n",
710 ucs->rcvbuf_size);
711 kfree(ucs->rcvbuf);
712 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800713 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000714 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
715 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800716 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700717 dev_err(cs->dev, "out of memory receiving AT data\n");
718 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800719 break;
720 }
721 ucs->rcvbuf_size = l;
722 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000723 rc = atread_submit(cs, BAS_TIMEOUT);
724 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800725 kfree(ucs->rcvbuf);
726 ucs->rcvbuf = NULL;
727 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700728 if (rc != -ENODEV) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700729 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700730 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700731 break;
732 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800733 }
734 spin_unlock_irqrestore(&cs->lock, flags);
735 break;
736
737 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000738 update_basstate(ucs, 0, BS_RESETTING);
739 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800740 break;
741
742 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700743 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800744 break;
745
746 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700747 dev_warn(cs->dev,
748 "unknown Gigaset signal 0x%02x (%u) ignored\n",
749 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800750 }
751
752 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800753 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800754
755resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800756 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700757 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700758 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700759 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800760 error_reset(cs);
761 }
762}
763
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764/* read_iso_callback
765 * USB completion handler for B channel isochronous input
766 * called by the USB subsystem in interrupt context
767 * parameter:
768 * urb USB request block of completed request
769 * urb->context = bc_state structure
770 */
David Howells7d12e782006-10-05 14:55:46 +0100771static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800772{
773 struct bc_state *bcs;
774 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800775 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800776 unsigned long flags;
777 int i, rc;
778
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800779 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800780 if (unlikely(status == -ENOENT ||
781 status == -ECONNRESET ||
782 status == -EINPROGRESS ||
783 status == -ENODEV ||
784 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700785 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800786 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800787 return;
788 }
789
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700790 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800791 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800792
793 spin_lock_irqsave(&ubc->isoinlock, flags);
794 if (likely(ubc->isoindone == NULL)) {
795 /* pass URB to tasklet */
796 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800797 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000798 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800799 } else {
800 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800801 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802 for (i = 0; i < BAS_NUMFRAMES; i++) {
803 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
804 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700805 urb->iso_frame_desc[i].status !=
806 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800807 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800808 urb->iso_frame_desc[i].status = 0;
809 urb->iso_frame_desc[i].actual_length = 0;
810 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800811 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700812 /* urb->dev is clobbered by USB subsystem */
813 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800814 urb->transfer_flags = URB_ISO_ASAP;
815 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700816 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
817 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800818 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700819 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700820 dev_err(bcs->cs->dev,
821 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700822 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800823 dump_urb(DEBUG_ISO, "isoc read", urb);
824 error_hangup(bcs);
825 }
826 }
827 }
828 spin_unlock_irqrestore(&ubc->isoinlock, flags);
829}
830
831/* write_iso_callback
832 * USB completion handler for B channel isochronous output
833 * called by the USB subsystem in interrupt context
834 * parameter:
835 * urb USB request block of completed request
836 * urb->context = isow_urbctx_t structure
837 */
David Howells7d12e782006-10-05 14:55:46 +0100838static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800839{
840 struct isow_urbctx_t *ucx;
841 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800842 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800843 unsigned long flags;
844
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800845 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800846 if (unlikely(status == -ENOENT ||
847 status == -ECONNRESET ||
848 status == -EINPROGRESS ||
849 status == -ENODEV ||
850 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700851 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800852 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800853 return;
854 }
855
856 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700857 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800858 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800859 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800860
861 spin_lock_irqsave(&ubc->isooutlock, flags);
862 ubc->isooutovfl = ubc->isooutdone;
863 ubc->isooutdone = ucx;
864 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000865 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800866}
867
868/* starturbs
869 * prepare and submit USB request blocks for isochronous input and output
870 * argument:
871 * B channel control structure
872 * return value:
873 * 0 on success
874 * < 0 on error (no URBs submitted)
875 */
876static int starturbs(struct bc_state *bcs)
877{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700878 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800879 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800880 int j, k;
881 int rc;
882
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000884 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800885 bcs->inputstate |= INS_flag_hunt;
886
887 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800888 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800889 for (k = 0; k < BAS_INURBS; k++) {
890 urb = ubc->isoinurbs[k];
891 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800892 rc = -EFAULT;
893 goto error;
894 }
895
896 urb->dev = bcs->cs->hw.bas->udev;
897 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
898 urb->transfer_flags = URB_ISO_ASAP;
899 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
900 urb->transfer_buffer_length = BAS_INBUFSIZE;
901 urb->number_of_packets = BAS_NUMFRAMES;
902 urb->interval = BAS_FRAMETIME;
903 urb->complete = read_iso_callback;
904 urb->context = bcs;
905 for (j = 0; j < BAS_NUMFRAMES; j++) {
906 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
907 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
908 urb->iso_frame_desc[j].status = 0;
909 urb->iso_frame_desc[j].actual_length = 0;
910 }
911
912 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000913 rc = usb_submit_urb(urb, GFP_ATOMIC);
914 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800915 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800916 }
917
918 /* initialize L2 transmission */
919 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
920
921 /* set up isochronous output URBs for flag idling */
922 for (k = 0; k < BAS_OUTURBS; ++k) {
923 urb = ubc->isoouturbs[k].urb;
924 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800925 rc = -EFAULT;
926 goto error;
927 }
928 urb->dev = bcs->cs->hw.bas->udev;
929 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
930 urb->transfer_flags = URB_ISO_ASAP;
931 urb->transfer_buffer = ubc->isooutbuf->data;
932 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
933 urb->number_of_packets = BAS_NUMFRAMES;
934 urb->interval = BAS_FRAMETIME;
935 urb->complete = write_iso_callback;
936 urb->context = &ubc->isoouturbs[k];
937 for (j = 0; j < BAS_NUMFRAMES; ++j) {
938 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
939 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
940 urb->iso_frame_desc[j].status = 0;
941 urb->iso_frame_desc[j].actual_length = 0;
942 }
943 ubc->isoouturbs[k].limit = -1;
944 }
945
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800946 /* keep one URB free, submit the others */
947 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800948 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800949 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700950 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800951 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800952 }
953 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800954 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800955 ubc->isooutdone = ubc->isooutovfl = NULL;
956 return 0;
957 error:
958 stopurbs(ubc);
959 return rc;
960}
961
962/* stopurbs
963 * cancel the USB request blocks for isochronous input and output
964 * errors are silently ignored
965 * argument:
966 * B channel control structure
967 */
968static void stopurbs(struct bas_bc_state *ubc)
969{
970 int k, rc;
971
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800972 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800973
974 for (k = 0; k < BAS_INURBS; ++k) {
975 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700976 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700977 "%s: isoc input URB %d unlinked, result = %s",
978 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800979 }
980
981 for (k = 0; k < BAS_OUTURBS; ++k) {
982 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700983 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700984 "%s: isoc output URB %d unlinked, result = %s",
985 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800986 }
987}
988
989/* Isochronous Write - Bottom Half */
990/* =============================== */
991
992/* submit_iso_write_urb
993 * fill and submit the next isochronous write URB
994 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700995 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800996 * return value:
997 * number of frames submitted in URB
998 * 0 if URB not submitted because no data available (isooutbuf busy)
999 * error code < 0 on error
1000 */
1001static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1002{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001003 struct urb *urb = ucx->urb;
1004 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001005 struct usb_iso_packet_descriptor *ifd;
1006 int corrbytes, nframe, rc;
1007
Tilman Schmidt784d5852006-04-10 22:55:04 -07001008 /* urb->dev is clobbered by USB subsystem */
1009 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001010 urb->transfer_flags = URB_ISO_ASAP;
1011 urb->transfer_buffer = ubc->isooutbuf->data;
1012 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1013
1014 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1015 ifd = &urb->iso_frame_desc[nframe];
1016
1017 /* compute frame length according to flow control */
1018 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001019 corrbytes = atomic_read(&ubc->corrbytes);
1020 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001021 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1022 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001023 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1024 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1025 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1026 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1027 ifd->length += corrbytes;
1028 atomic_add(-corrbytes, &ubc->corrbytes);
1029 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030
1031 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001032 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1033 if (rc < 0) {
1034 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001035 gig_dbg(DEBUG_ISO,
1036 "%s: buffer busy at frame %d",
1037 __func__, nframe);
1038 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001039 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001040 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001041 dev_err(ucx->bcs->cs->dev,
1042 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001043 __func__, rc, nframe);
1044 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001045 }
1046 break;
1047 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001048 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001049 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001050 ifd->status = 0;
1051 ifd->actual_length = 0;
1052 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001053 if (unlikely(nframe == 0))
1054 return 0; /* no data to send */
1055 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001056
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001057 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001058 if (unlikely(rc)) {
1059 if (rc == -ENODEV)
1060 /* device removed - give up silently */
1061 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1062 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001063 dev_err(ucx->bcs->cs->dev,
1064 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001065 get_usb_rcmsg(rc));
1066 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001067 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001068 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001069 return nframe;
1070}
1071
1072/* write_iso_tasklet
1073 * tasklet scheduled when an isochronous output URB from the Gigaset device
1074 * has completed
1075 * parameter:
1076 * data B channel state structure
1077 */
1078static void write_iso_tasklet(unsigned long data)
1079{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001080 struct bc_state *bcs = (struct bc_state *) data;
1081 struct bas_bc_state *ubc = bcs->hw.bas;
1082 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001083 struct isow_urbctx_t *done, *next, *ovfl;
1084 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001085 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001086 struct usb_iso_packet_descriptor *ifd;
1087 int offset;
1088 unsigned long flags;
1089 int i;
1090 struct sk_buff *skb;
1091 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001092 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001093
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001094 /* loop while completed URBs arrive in time */
1095 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001096 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001097 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001098 return;
1099 }
1100
1101 /* retrieve completed URBs */
1102 spin_lock_irqsave(&ubc->isooutlock, flags);
1103 done = ubc->isooutdone;
1104 ubc->isooutdone = NULL;
1105 ovfl = ubc->isooutovfl;
1106 ubc->isooutovfl = NULL;
1107 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1108 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001109 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001110 error_hangup(bcs);
1111 break;
1112 }
1113 if (!done)
1114 break;
1115
1116 /* submit free URB if available */
1117 spin_lock_irqsave(&ubc->isooutlock, flags);
1118 next = ubc->isooutfree;
1119 ubc->isooutfree = NULL;
1120 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1121 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001122 rc = submit_iso_write_urb(next);
1123 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001124 /* could not submit URB, put it back */
1125 spin_lock_irqsave(&ubc->isooutlock, flags);
1126 if (ubc->isooutfree == NULL) {
1127 ubc->isooutfree = next;
1128 next = NULL;
1129 }
1130 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1131 if (next) {
1132 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001133 dev_err(cs->dev,
1134 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001135 error_hangup(bcs);
1136 }
1137 }
1138 }
1139
1140 /* process completed URB */
1141 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001142 status = done->status;
1143 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001144 case -EXDEV: /* partial completion */
1145 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1146 __func__);
1147 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001148 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001149 /* inspect individual frames
1150 * assumptions (for lack of documentation):
1151 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001152 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001153 * - all following frames are not sent at all
1154 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001155 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001156 for (i = 0; i < BAS_NUMFRAMES; i++) {
1157 ifd = &urb->iso_frame_desc[i];
1158 if (ifd->status ||
1159 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001160 dev_warn(cs->dev,
1161 "isochronous write: frame %d: %s, "
1162 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001163 i, get_usb_statmsg(ifd->status),
1164 ifd->actual_length, ifd->length);
1165 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001166 ifd->actual_length)
1167 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001168 break;
1169 }
1170 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001171 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001172 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001173 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001174 error_hangup(bcs);
1175 break;
1176 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001177 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001178 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001179 }
1180
1181 /* mark the write buffer area covered by this URB as free */
1182 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001183 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001184
1185 /* mark URB as free */
1186 spin_lock_irqsave(&ubc->isooutlock, flags);
1187 next = ubc->isooutfree;
1188 ubc->isooutfree = done;
1189 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1190 if (next) {
1191 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001192 rc = submit_iso_write_urb(next);
1193 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001194 /* couldn't submit */
1195 error_hangup(bcs);
1196 }
1197 }
1198 }
1199
1200 /* process queued SKBs */
1201 while ((skb = skb_dequeue(&bcs->squeue))) {
1202 /* copy to output buffer, doing L2 encapsulation */
1203 len = skb->len;
1204 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1205 /* insufficient buffer space, push back onto queue */
1206 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001207 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1208 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001209 break;
1210 }
1211 skb_pull(skb, len);
1212 gigaset_skb_sent(bcs, skb);
1213 dev_kfree_skb_any(skb);
1214 }
1215}
1216
1217/* Isochronous Read - Bottom Half */
1218/* ============================== */
1219
1220/* read_iso_tasklet
1221 * tasklet scheduled when an isochronous input URB from the Gigaset device
1222 * has completed
1223 * parameter:
1224 * data B channel state structure
1225 */
1226static void read_iso_tasklet(unsigned long data)
1227{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001228 struct bc_state *bcs = (struct bc_state *) data;
1229 struct bas_bc_state *ubc = bcs->hw.bas;
1230 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001231 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001232 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001233 char *rcvbuf;
1234 unsigned long flags;
1235 int totleft, numbytes, offset, frame, rc;
1236
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 /* loop while more completed URBs arrive in the meantime */
1238 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001239 /* retrieve URB */
1240 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001241 urb = ubc->isoindone;
1242 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1244 return;
1245 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001246 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001247 ubc->isoindone = NULL;
1248 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001249 dev_warn(cs->dev,
1250 "isochronous read overrun, "
1251 "dropped URB with status: %s, %d bytes lost\n",
1252 get_usb_statmsg(ubc->loststatus),
1253 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001254 ubc->loststatus = -EINPROGRESS;
1255 }
1256 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1257
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001258 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001259 gig_dbg(DEBUG_ISO,
1260 "%s: channel not running, "
1261 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001262 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001263 return;
1264 }
1265
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001266 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001267 case 0: /* normal completion */
1268 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001269 case -EXDEV: /* inspect individual frames
1270 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001271 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1272 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001273 break;
1274 case -ENOENT:
1275 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001276 case -EINPROGRESS:
1277 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001278 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 continue; /* -> skip */
1280 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001281 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001282 error_hangup(bcs);
1283 continue; /* -> skip */
1284 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001285 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001286 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001287 goto error;
1288 }
1289
1290 rcvbuf = urb->transfer_buffer;
1291 totleft = urb->actual_length;
1292 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001293 numbytes = urb->iso_frame_desc[frame].actual_length;
1294 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001295 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001296 "isochronous read: frame %d[%d]: %s\n",
1297 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001298 get_usb_statmsg(
1299 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001300 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001301 dev_warn(cs->dev,
1302 "isochronous read: frame %d: "
1303 "numbytes (%d) > BAS_MAXFRAME\n",
1304 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001305 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001306 dev_warn(cs->dev,
1307 "isochronous read: frame %d: "
1308 "numbytes (%d) > totleft (%d)\n",
1309 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001310 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001311 }
1312 offset = urb->iso_frame_desc[frame].offset;
1313 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001314 dev_warn(cs->dev,
1315 "isochronous read: frame %d: "
1316 "offset (%d) + numbytes (%d) "
1317 "> BAS_INBUFSIZE\n",
1318 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001319 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001320 }
1321 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1322 totleft -= numbytes;
1323 }
1324 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001325 dev_warn(cs->dev,
1326 "isochronous read: %d data bytes missing\n",
1327 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001328
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001329error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001330 /* URB processed, resubmit */
1331 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1332 urb->iso_frame_desc[frame].status = 0;
1333 urb->iso_frame_desc[frame].actual_length = 0;
1334 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001335 /* urb->dev is clobbered by USB subsystem */
1336 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001337 urb->transfer_flags = URB_ISO_ASAP;
1338 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001339 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001340 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001341 dev_err(cs->dev,
1342 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001343 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001344 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1345 error_hangup(bcs);
1346 }
1347 }
1348}
1349
1350/* Channel Operations */
1351/* ================== */
1352
1353/* req_timeout
1354 * timeout routine for control output request
1355 * argument:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001356 * controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001357 */
1358static void req_timeout(unsigned long data)
1359{
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001360 struct cardstate *cs = (struct cardstate *) data;
1361 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001362 int pending;
1363 unsigned long flags;
1364
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001365 check_pending(ucs);
1366
1367 spin_lock_irqsave(&ucs->lock, flags);
1368 pending = ucs->pending;
1369 ucs->pending = 0;
1370 spin_unlock_irqrestore(&ucs->lock, flags);
1371
1372 switch (pending) {
1373 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001374 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001375 break;
1376
1377 case HD_OPEN_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001378 dev_err(cs->dev, "timeout opening AT channel\n");
1379 error_reset(cs);
1380 break;
1381
1382 case HD_OPEN_B1CHANNEL:
1383 dev_err(cs->dev, "timeout opening channel 1\n");
1384 error_hangup(&cs->bcs[0]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001385 break;
1386
1387 case HD_OPEN_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001388 dev_err(cs->dev, "timeout opening channel 2\n");
1389 error_hangup(&cs->bcs[1]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001390 break;
1391
1392 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001393 dev_err(cs->dev, "timeout closing AT channel\n");
1394 error_reset(cs);
1395 break;
1396
1397 case HD_CLOSE_B1CHANNEL:
1398 dev_err(cs->dev, "timeout closing channel 1\n");
1399 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001400 break;
1401
1402 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001403 dev_err(cs->dev, "timeout closing channel 2\n");
1404 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001405 break;
1406
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001407 case HD_RESET_INTERRUPT_PIPE:
1408 /* error recovery escalation */
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001409 dev_err(cs->dev,
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001410 "reset interrupt pipe timeout, attempting USB reset\n");
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001411 usb_queue_reset_device(ucs->interface);
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001412 break;
1413
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001414 default:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001415 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001417 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001418
1419 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001420}
1421
1422/* write_ctrl_callback
1423 * USB completion handler for control pipe output
1424 * called by the USB subsystem in interrupt context
1425 * parameter:
1426 * urb USB request block of completed request
1427 * urb->context = hardware specific controller state structure
1428 */
David Howells7d12e782006-10-05 14:55:46 +01001429static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001430{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001431 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001432 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001433 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001434 unsigned long flags;
1435
Tilman Schmidt06163f82006-06-26 00:25:33 -07001436 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001437 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001438 case 0: /* normal completion */
1439 spin_lock_irqsave(&ucs->lock, flags);
1440 switch (ucs->pending) {
1441 case HD_DEVICE_INIT_ACK: /* no reply expected */
1442 del_timer(&ucs->timer_ctrl);
1443 ucs->pending = 0;
1444 break;
1445 }
1446 spin_unlock_irqrestore(&ucs->lock, flags);
1447 return;
1448
1449 case -ENOENT: /* cancelled */
1450 case -ECONNRESET: /* cancelled (async) */
1451 case -EINPROGRESS: /* pending */
1452 case -ENODEV: /* device removed */
1453 case -ESHUTDOWN: /* device shut down */
1454 /* ignore silently */
1455 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001456 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001457 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001458
1459 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001460 /* don't retry if suspend requested */
1461 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001462 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001463 dev_err(&ucs->interface->dev,
1464 "control request 0x%02x failed: %s\n",
1465 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001466 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001467 break; /* give up */
1468 }
1469 dev_notice(&ucs->interface->dev,
1470 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001471 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001472 ucs->retry_ctrl);
1473 /* urb->dev is clobbered by USB subsystem */
1474 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001475 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001476 if (unlikely(rc)) {
1477 dev_err(&ucs->interface->dev,
1478 "could not resubmit request 0x%02x: %s\n",
1479 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1480 break;
1481 }
1482 /* resubmitted */
1483 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001484 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001485
1486 /* failed, clear pending request */
1487 spin_lock_irqsave(&ucs->lock, flags);
1488 del_timer(&ucs->timer_ctrl);
1489 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001490 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001491 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001492}
1493
1494/* req_submit
1495 * submit a control output request without message buffer to the Gigaset base
1496 * and optionally start a timeout
1497 * parameters:
1498 * bcs B channel control structure
1499 * req control request code (HD_*)
1500 * val control request parameter value (set to 0 if unused)
1501 * timeout timeout in seconds (0: no timeout)
1502 * return value:
1503 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001504 * -EBUSY if another request is pending
1505 * any URB submission error code
1506 */
1507static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1508{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001509 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001510 int ret;
1511 unsigned long flags;
1512
Tilman Schmidt784d5852006-04-10 22:55:04 -07001513 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001514
1515 spin_lock_irqsave(&ucs->lock, flags);
1516 if (ucs->pending) {
1517 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001518 dev_err(bcs->cs->dev,
1519 "submission of request 0x%02x failed: "
1520 "request 0x%02x still pending\n",
1521 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001522 return -EBUSY;
1523 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001524
1525 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1526 ucs->dr_ctrl.bRequest = req;
1527 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1528 ucs->dr_ctrl.wIndex = 0;
1529 ucs->dr_ctrl.wLength = 0;
1530 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001531 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001532 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001533 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001534 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001535 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001536 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001537 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001538 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001539 spin_unlock_irqrestore(&ucs->lock, flags);
1540 return ret;
1541 }
1542 ucs->pending = req;
1543
1544 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001545 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001546 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001547 }
1548
1549 spin_unlock_irqrestore(&ucs->lock, flags);
1550 return 0;
1551}
1552
1553/* gigaset_init_bchannel
1554 * called by common.c to connect a B channel
1555 * initialize isochronous I/O and tell the Gigaset base to open the channel
1556 * argument:
1557 * B channel control structure
1558 * return value:
1559 * 0 on success, error code < 0 on error
1560 */
1561static int gigaset_init_bchannel(struct bc_state *bcs)
1562{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001563 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001564 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001565 unsigned long flags;
1566
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001567 spin_lock_irqsave(&cs->lock, flags);
1568 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001569 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001570 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001571 return -ENODEV;
1572 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001573
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001574 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001575 dev_notice(cs->dev,
1576 "not starting isochronous I/O, "
1577 "suspend in progress\n");
1578 spin_unlock_irqrestore(&cs->lock, flags);
1579 return -EHOSTUNREACH;
1580 }
1581
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001582 ret = starturbs(bcs);
1583 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001584 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001585 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001586 "could not start isochronous I/O for channel B%d: %s\n",
1587 bcs->channel + 1,
1588 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1589 if (ret != -ENODEV)
1590 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001591 return ret;
1592 }
1593
1594 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001595 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1596 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001597 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001598 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001599 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001600 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001601
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001602 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001603 if (ret < 0 && ret != -ENODEV)
1604 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001605 return ret;
1606}
1607
1608/* gigaset_close_bchannel
1609 * called by common.c to disconnect a B channel
1610 * tell the Gigaset base to close the channel
1611 * stopping isochronous I/O and LL notification will be done when the
1612 * acknowledgement for the close arrives
1613 * argument:
1614 * B channel control structure
1615 * return value:
1616 * 0 on success, error code < 0 on error
1617 */
1618static int gigaset_close_bchannel(struct bc_state *bcs)
1619{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001620 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001621 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001622 unsigned long flags;
1623
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001624 spin_lock_irqsave(&cs->lock, flags);
1625 if (unlikely(!cs->connected)) {
1626 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001627 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1628 return -ENODEV;
1629 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001630
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001631 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001632 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001633 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001634 gigaset_bchannel_down(bcs);
1635 return 0;
1636 }
1637
Tilman Schmidt73a88812006-04-22 02:35:30 -07001638 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001639 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001640 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1641 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001642 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001643 bcs->channel + 1);
1644
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001645 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001646 return ret;
1647}
1648
1649/* Device Operations */
1650/* ================= */
1651
1652/* complete_cb
1653 * unqueue first command buffer from queue, waking any sleepers
1654 * must be called with cs->cmdlock held
1655 * parameter:
1656 * cs controller state structure
1657 */
1658static void complete_cb(struct cardstate *cs)
1659{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001660 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001661
1662 /* unqueue completed buffer */
1663 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001664 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001665 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001666 if (cb->next != NULL) {
1667 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001668 cs->cmdbuf->prev = NULL;
1669 cs->curlen = cs->cmdbuf->len;
1670 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001671 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001672 cs->lastcmdbuf = NULL;
1673 cs->curlen = 0;
1674 }
1675
1676 if (cb->wake_tasklet)
1677 tasklet_schedule(cb->wake_tasklet);
1678
1679 kfree(cb);
1680}
1681
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001682/* write_command_callback
1683 * USB completion handler for AT command transmission
1684 * called by the USB subsystem in interrupt context
1685 * parameter:
1686 * urb USB request block of completed request
1687 * urb->context = controller state structure
1688 */
David Howells7d12e782006-10-05 14:55:46 +01001689static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001690{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001691 struct cardstate *cs = urb->context;
1692 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001693 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001694 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001695
Tilman Schmidt73a88812006-04-22 02:35:30 -07001696 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001697 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001698
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001699 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001700 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001701 case 0: /* normal completion */
1702 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001703 case -ENOENT: /* cancelled */
1704 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001705 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001706 case -ENODEV: /* device removed */
1707 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001708 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001709 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001710 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001711 return;
1712 default: /* any failure */
1713 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001714 dev_warn(cs->dev,
1715 "command write: %s, "
1716 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001717 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001718 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001719 break;
1720 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001721 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001722 dev_warn(cs->dev,
1723 "command write: %s, "
1724 "won't retry - suspend requested\n",
1725 get_usb_statmsg(status));
1726 break;
1727 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001729 dev_warn(cs->dev,
1730 "command write: %s, "
1731 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001732 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001733 break;
1734 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001735 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001736 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001737 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1738 /* resubmitted - bypass regular exit block */
1739 return;
1740 /* command send failed, assume base still waiting */
1741 update_basstate(ucs, BS_ATREADY, 0);
1742 }
1743
1744 spin_lock_irqsave(&cs->cmdlock, flags);
1745 if (cs->cmdbuf != NULL)
1746 complete_cb(cs);
1747 spin_unlock_irqrestore(&cs->cmdlock, flags);
1748}
1749
1750/* atrdy_timeout
1751 * timeout routine for AT command transmission
1752 * argument:
1753 * controller state structure
1754 */
1755static void atrdy_timeout(unsigned long data)
1756{
1757 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001758 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001759
Tilman Schmidt784d5852006-04-10 22:55:04 -07001760 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001761
1762 /* fake the missing signal - what else can I do? */
1763 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1764 start_cbsend(cs);
1765}
1766
1767/* atwrite_submit
1768 * submit an HD_WRITE_ATMESSAGE command URB
1769 * parameters:
1770 * cs controller state structure
1771 * buf buffer containing command to send
1772 * len length of command to send
1773 * return value:
1774 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001775 * -EBUSY if another request is pending
1776 * any URB submission error code
1777 */
1778static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1779{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001780 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001781 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001782
Tilman Schmidt784d5852006-04-10 22:55:04 -07001783 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001784
Tilman Schmidt73a88812006-04-22 02:35:30 -07001785 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001786 dev_err(cs->dev,
1787 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001788 return -EBUSY;
1789 }
1790
1791 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1792 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1793 ucs->dr_cmd_out.wValue = 0;
1794 ucs->dr_cmd_out.wIndex = 0;
1795 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1796 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1797 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001798 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001800 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001801 if (unlikely(rc)) {
1802 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001803 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001804 get_usb_rcmsg(rc));
1805 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001806 }
1807
Tilman Schmidt73a88812006-04-22 02:35:30 -07001808 /* submitted successfully, start timeout if necessary */
1809 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001810 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1811 ATRDY_TIMEOUT);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001812 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001813 }
1814 return 0;
1815}
1816
1817/* start_cbsend
1818 * start transmission of AT command queue if necessary
1819 * parameter:
1820 * cs controller state structure
1821 * return value:
1822 * 0 on success
1823 * error code < 0 on error
1824 */
1825static int start_cbsend(struct cardstate *cs)
1826{
1827 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001828 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001829 unsigned long flags;
1830 int rc;
1831 int retval = 0;
1832
Tilman Schmidt024fd292008-02-06 01:38:26 -08001833 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001834 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001835 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001836 return -EHOSTUNREACH;
1837 }
1838
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001839 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001840 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001841 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001842 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1843 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001844 /* flush command queue */
1845 spin_lock_irqsave(&cs->cmdlock, flags);
1846 while (cs->cmdbuf != NULL)
1847 complete_cb(cs);
1848 spin_unlock_irqrestore(&cs->cmdlock, flags);
1849 }
1850 return rc;
1851 }
1852
1853 /* try to send first command in queue */
1854 spin_lock_irqsave(&cs->cmdlock, flags);
1855
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001856 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001857 ucs->retry_cmd_out = 0;
1858 rc = atwrite_submit(cs, cb->buf, cb->len);
1859 if (unlikely(rc)) {
1860 retval = rc;
1861 complete_cb(cs);
1862 }
1863 }
1864
1865 spin_unlock_irqrestore(&cs->cmdlock, flags);
1866 return retval;
1867}
1868
1869/* gigaset_write_cmd
1870 * This function is called by the device independent part of the driver
1871 * to transmit an AT command string to the Gigaset device.
1872 * It encapsulates the device specific method for transmission over the
1873 * direct USB connection to the base.
1874 * The command string is added to the queue of commands to send, and
1875 * USB transmission is started if necessary.
1876 * parameters:
1877 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001878 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001879 * return value:
1880 * number of bytes queued on success
1881 * error code < 0 on error
1882 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001883static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001884{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001885 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001886 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001887
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001888 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001889 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001890 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001891
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001892 /* translate "+++" escape sequence sent as a single separate command
1893 * into "close AT channel" command for error recovery
1894 * The next command will reopen the AT channel automatically.
1895 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001896 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001897 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001898 if (cb->wake_tasklet)
1899 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00001900 if (!rc)
1901 rc = cb->len;
1902 kfree(cb);
1903 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001904 }
1905
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001906 spin_lock_irqsave(&cs->cmdlock, flags);
1907 cb->prev = cs->lastcmdbuf;
1908 if (cs->lastcmdbuf)
1909 cs->lastcmdbuf->next = cb;
1910 else {
1911 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001912 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001913 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001914 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001915 cs->lastcmdbuf = cb;
1916 spin_unlock_irqrestore(&cs->cmdlock, flags);
1917
Tilman Schmidt73a88812006-04-22 02:35:30 -07001918 spin_lock_irqsave(&cs->lock, flags);
1919 if (unlikely(!cs->connected)) {
1920 spin_unlock_irqrestore(&cs->lock, flags);
1921 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001922 /* flush command queue */
1923 spin_lock_irqsave(&cs->cmdlock, flags);
1924 while (cs->cmdbuf != NULL)
1925 complete_cb(cs);
1926 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001927 return -ENODEV;
1928 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001929 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001930 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001931 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001932}
1933
1934/* gigaset_write_room
1935 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001936 * return number of characters the driver will accept to be written via
1937 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001938 * parameter:
1939 * controller state structure
1940 * return value:
1941 * number of characters
1942 */
1943static int gigaset_write_room(struct cardstate *cs)
1944{
1945 return IF_WRITEBUF;
1946}
1947
1948/* gigaset_chars_in_buffer
1949 * tty_driver.chars_in_buffer interface routine
1950 * return number of characters waiting to be sent
1951 * parameter:
1952 * controller state structure
1953 * return value:
1954 * number of characters
1955 */
1956static int gigaset_chars_in_buffer(struct cardstate *cs)
1957{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07001958 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001959}
1960
1961/* gigaset_brkchars
1962 * implementation of ioctl(GIGASET_BRKCHARS)
1963 * parameter:
1964 * controller state structure
1965 * return value:
1966 * -EINVAL (unimplemented function)
1967 */
1968static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1969{
1970 return -EINVAL;
1971}
1972
1973
1974/* Device Initialization/Shutdown */
1975/* ============================== */
1976
1977/* Free hardware dependent part of the B channel structure
1978 * parameter:
1979 * bcs B channel structure
1980 * return value:
1981 * !=0 on success
1982 */
1983static int gigaset_freebcshw(struct bc_state *bcs)
1984{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001985 struct bas_bc_state *ubc = bcs->hw.bas;
1986 int i;
1987
1988 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001989 return 0;
1990
Tilman Schmidt73a88812006-04-22 02:35:30 -07001991 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001992 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001993 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
1994 for (i = 0; i < BAS_OUTURBS; ++i) {
1995 usb_kill_urb(ubc->isoouturbs[i].urb);
1996 usb_free_urb(ubc->isoouturbs[i].urb);
1997 }
1998 for (i = 0; i < BAS_INURBS; ++i) {
1999 usb_kill_urb(ubc->isoinurbs[i]);
2000 usb_free_urb(ubc->isoinurbs[i]);
2001 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002002 tasklet_kill(&ubc->sent_tasklet);
2003 tasklet_kill(&ubc->rcvd_tasklet);
2004 kfree(ubc->isooutbuf);
2005 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002006 bcs->hw.bas = NULL;
2007 return 1;
2008}
2009
2010/* Initialize hardware dependent part of the B channel structure
2011 * parameter:
2012 * bcs B channel structure
2013 * return value:
2014 * !=0 on success
2015 */
2016static int gigaset_initbcshw(struct bc_state *bcs)
2017{
2018 int i;
2019 struct bas_bc_state *ubc;
2020
2021 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2022 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002023 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002024 return 0;
2025 }
2026
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002027 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002028 atomic_set(&ubc->corrbytes, 0);
2029 spin_lock_init(&ubc->isooutlock);
2030 for (i = 0; i < BAS_OUTURBS; ++i) {
2031 ubc->isoouturbs[i].urb = NULL;
2032 ubc->isoouturbs[i].bcs = bcs;
2033 }
2034 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2035 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002036 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2037 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002038 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002039 kfree(ubc);
2040 bcs->hw.bas = NULL;
2041 return 0;
2042 }
2043 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002044 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002045
2046 spin_lock_init(&ubc->isoinlock);
2047 for (i = 0; i < BAS_INURBS; ++i)
2048 ubc->isoinurbs[i] = NULL;
2049 ubc->isoindone = NULL;
2050 ubc->loststatus = -EINPROGRESS;
2051 ubc->isoinlost = 0;
2052 ubc->seqlen = 0;
2053 ubc->inbyte = 0;
2054 ubc->inbits = 0;
2055 ubc->goodbytes = 0;
2056 ubc->alignerrs = 0;
2057 ubc->fcserrs = 0;
2058 ubc->frameerrs = 0;
2059 ubc->giants = 0;
2060 ubc->runts = 0;
2061 ubc->aborts = 0;
2062 ubc->shared0s = 0;
2063 ubc->stolen0s = 0;
2064 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002065 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002066 return 1;
2067}
2068
2069static void gigaset_reinitbcshw(struct bc_state *bcs)
2070{
2071 struct bas_bc_state *ubc = bcs->hw.bas;
2072
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002073 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002074 atomic_set(&bcs->hw.bas->corrbytes, 0);
2075 bcs->hw.bas->numsub = 0;
2076 spin_lock_init(&ubc->isooutlock);
2077 spin_lock_init(&ubc->isoinlock);
2078 ubc->loststatus = -EINPROGRESS;
2079}
2080
2081static void gigaset_freecshw(struct cardstate *cs)
2082{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002083 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002084 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002085 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002086 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002087}
2088
2089static int gigaset_initcshw(struct cardstate *cs)
2090{
2091 struct bas_cardstate *ucs;
2092
2093 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002094 if (!ucs) {
2095 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002096 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002097 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002098 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2099 if (!ucs->int_in_buf) {
2100 kfree(ucs);
2101 pr_err("out of memory\n");
2102 return 0;
2103 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002104
2105 ucs->urb_cmd_in = NULL;
2106 ucs->urb_cmd_out = NULL;
2107 ucs->rcvbuf = NULL;
2108 ucs->rcvbuf_size = 0;
2109
2110 spin_lock_init(&ucs->lock);
2111 ucs->pending = 0;
2112
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002113 ucs->basstate = 0;
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002114 setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
2115 setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
2116 setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002117 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002118
2119 return 1;
2120}
2121
2122/* freeurbs
2123 * unlink and deallocate all URBs unconditionally
2124 * caller must make sure that no commands are still in progress
2125 * parameter:
2126 * cs controller state structure
2127 */
2128static void freeurbs(struct cardstate *cs)
2129{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002130 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002131 struct bas_bc_state *ubc;
2132 int i, j;
2133
Tilman Schmidt39f07222006-12-13 00:33:52 -08002134 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002135 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002136 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002137 for (i = 0; i < BAS_OUTURBS; ++i) {
2138 usb_kill_urb(ubc->isoouturbs[i].urb);
2139 usb_free_urb(ubc->isoouturbs[i].urb);
2140 ubc->isoouturbs[i].urb = NULL;
2141 }
2142 for (i = 0; i < BAS_INURBS; ++i) {
2143 usb_kill_urb(ubc->isoinurbs[i]);
2144 usb_free_urb(ubc->isoinurbs[i]);
2145 ubc->isoinurbs[i] = NULL;
2146 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002147 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002148 usb_kill_urb(ucs->urb_int_in);
2149 usb_free_urb(ucs->urb_int_in);
2150 ucs->urb_int_in = NULL;
2151 usb_kill_urb(ucs->urb_cmd_out);
2152 usb_free_urb(ucs->urb_cmd_out);
2153 ucs->urb_cmd_out = NULL;
2154 usb_kill_urb(ucs->urb_cmd_in);
2155 usb_free_urb(ucs->urb_cmd_in);
2156 ucs->urb_cmd_in = NULL;
2157 usb_kill_urb(ucs->urb_ctrl);
2158 usb_free_urb(ucs->urb_ctrl);
2159 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002160}
2161
2162/* gigaset_probe
2163 * This function is called when a new USB device is connected.
2164 * It checks whether the new device is handled by this driver.
2165 */
2166static int gigaset_probe(struct usb_interface *interface,
2167 const struct usb_device_id *id)
2168{
2169 struct usb_host_interface *hostif;
2170 struct usb_device *udev = interface_to_usbdev(interface);
2171 struct cardstate *cs = NULL;
2172 struct bas_cardstate *ucs = NULL;
2173 struct bas_bc_state *ubc;
2174 struct usb_endpoint_descriptor *endpoint;
2175 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002176 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002177
Tilman Schmidt1528b182010-02-22 13:09:52 +00002178 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002179 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2180 __func__, le16_to_cpu(udev->descriptor.idVendor),
2181 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002182
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002183 /* set required alternate setting */
2184 hostif = interface->cur_altsetting;
2185 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002186 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002187 "%s: wrong alternate setting %d - trying to switch",
2188 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002189 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2190 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002191 dev_warn(&udev->dev, "usb_set_interface failed, "
2192 "device %d interface %d altsetting %d\n",
2193 udev->devnum, hostif->desc.bInterfaceNumber,
2194 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002195 return -ENODEV;
2196 }
2197 hostif = interface->cur_altsetting;
2198 }
2199
2200 /* Reject application specific interfaces
2201 */
2202 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002203 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2204 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002205 return -ENODEV;
2206 }
2207
Tilman Schmidt784d5852006-04-10 22:55:04 -07002208 dev_info(&udev->dev,
2209 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2210 __func__, le16_to_cpu(udev->descriptor.idVendor),
2211 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002212
Tilman Schmidte468c042008-02-06 01:38:29 -08002213 /* allocate memory for our device state and intialize it */
2214 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2215 GIGASET_MODULENAME);
2216 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002217 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002218 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002219
2220 /* save off device structure ptrs for later use */
2221 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002222 ucs->udev = udev;
2223 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002224 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002225
2226 /* allocate URBs:
2227 * - one for the interrupt pipe
2228 * - three for the different uses of the default control pipe
2229 * - three for each isochronous pipe
2230 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002231 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2232 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2233 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2234 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002235 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002236
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002237 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002238 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002239 for (i = 0; i < BAS_OUTURBS; ++i)
2240 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002241 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002242 goto allocerr;
2243 for (i = 0; i < BAS_INURBS; ++i)
2244 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002245 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002246 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002247 }
2248
2249 ucs->rcvbuf = NULL;
2250 ucs->rcvbuf_size = 0;
2251
2252 /* Fill the interrupt urb and send it to the core */
2253 endpoint = &hostif->endpoint[0].desc;
2254 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002255 usb_rcvintpipe(udev,
2256 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002257 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002258 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002259 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2260 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002261 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002262 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002263 goto error;
2264 }
2265
2266 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002267 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2268 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002269 goto error;
2270
2271 /* tell common part that the device is ready */
2272 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002273 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002274
2275 /* save address of controller structure */
2276 usb_set_intfdata(interface, cs);
2277
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002278 if (!gigaset_start(cs))
2279 goto error;
2280
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002281 return 0;
2282
Tilman Schmidt73a88812006-04-22 02:35:30 -07002283allocerr:
2284 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002285error:
2286 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002287 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002288 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289 return -ENODEV;
2290}
2291
2292/* gigaset_disconnect
2293 * This function is called when the Gigaset base is unplugged.
2294 */
2295static void gigaset_disconnect(struct usb_interface *interface)
2296{
2297 struct cardstate *cs;
2298 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002299 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002300
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002302
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002303 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002304
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002305 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002306
2307 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002308 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002309
2310 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002311 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002312 gigaset_bchannel_down(cs->bcs + j);
2313
2314 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002315 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002316
2317 /* stop timers and URBs, free ressources */
2318 del_timer_sync(&ucs->timer_ctrl);
2319 del_timer_sync(&ucs->timer_atrdy);
2320 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002321 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002322 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323 kfree(ucs->rcvbuf);
2324 ucs->rcvbuf = NULL;
2325 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002326 usb_put_dev(ucs->udev);
2327 ucs->interface = NULL;
2328 ucs->udev = NULL;
2329 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002330 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002331}
2332
Tilman Schmidt024fd292008-02-06 01:38:26 -08002333/* gigaset_suspend
2334 * This function is called before the USB connection is suspended.
2335 */
2336static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2337{
2338 struct cardstate *cs = usb_get_intfdata(intf);
2339 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002340 int rc;
2341
2342 /* set suspend flag; this stops AT command/response traffic */
2343 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2344 gig_dbg(DEBUG_SUSPEND, "already suspended");
2345 return 0;
2346 }
2347
2348 /* wait a bit for blocking conditions to go away */
2349 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002350 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002351 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2352 BAS_TIMEOUT*HZ/10);
2353 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2354
2355 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002356 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002357 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002358 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002359 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002360 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002361 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002362 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002363 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002364 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002365 dev_warn(cs->dev, " sending AT command\n");
2366 update_basstate(ucs, 0, BS_SUSPEND);
2367 return -EBUSY;
2368 }
2369
2370 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002371 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002372 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2373 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2374 if (rc) {
2375 update_basstate(ucs, 0, BS_SUSPEND);
2376 return rc;
2377 }
2378 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2379 BAS_TIMEOUT*HZ/10);
2380 /* in case of timeout, proceed anyway */
2381 }
2382
2383 /* kill all URBs and timers that might still be pending */
2384 usb_kill_urb(ucs->urb_ctrl);
2385 usb_kill_urb(ucs->urb_int_in);
2386 del_timer_sync(&ucs->timer_ctrl);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002387 del_timer_sync(&ucs->timer_atrdy);
2388 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002389
2390 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2391 return 0;
2392}
2393
2394/* gigaset_resume
2395 * This function is called after the USB connection has been resumed.
2396 */
2397static int gigaset_resume(struct usb_interface *intf)
2398{
2399 struct cardstate *cs = usb_get_intfdata(intf);
2400 struct bas_cardstate *ucs = cs->hw.bas;
2401 int rc;
2402
2403 /* resubmit interrupt URB for spontaneous messages from base */
2404 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2405 if (rc) {
2406 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2407 get_usb_rcmsg(rc));
2408 return rc;
2409 }
2410
2411 /* clear suspend flag to reallow activity */
2412 update_basstate(ucs, 0, BS_SUSPEND);
2413
2414 gig_dbg(DEBUG_SUSPEND, "resume complete");
2415 return 0;
2416}
2417
2418/* gigaset_pre_reset
2419 * This function is called before the USB connection is reset.
2420 */
2421static int gigaset_pre_reset(struct usb_interface *intf)
2422{
2423 /* handle just like suspend */
2424 return gigaset_suspend(intf, PMSG_ON);
2425}
2426
2427/* gigaset_post_reset
2428 * This function is called after the USB connection has been reset.
2429 */
2430static int gigaset_post_reset(struct usb_interface *intf)
2431{
2432 /* FIXME: send HD_DEVICE_INIT_ACK? */
2433
2434 /* resume operations */
2435 return gigaset_resume(intf);
2436}
2437
2438
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002439static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002440 gigaset_write_cmd,
2441 gigaset_write_room,
2442 gigaset_chars_in_buffer,
2443 gigaset_brkchars,
2444 gigaset_init_bchannel,
2445 gigaset_close_bchannel,
2446 gigaset_initbcshw,
2447 gigaset_freebcshw,
2448 gigaset_reinitbcshw,
2449 gigaset_initcshw,
2450 gigaset_freecshw,
2451 gigaset_set_modem_ctrl,
2452 gigaset_baud_rate,
2453 gigaset_set_line_ctrl,
2454 gigaset_isoc_send_skb,
2455 gigaset_isoc_input,
2456};
2457
2458/* bas_gigaset_init
2459 * This function is called after the kernel module is loaded.
2460 */
2461static int __init bas_gigaset_init(void)
2462{
2463 int result;
2464
2465 /* allocate memory for our driver state and intialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002466 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2467 GIGASET_MODULENAME, GIGASET_DEVNAME,
2468 &gigops, THIS_MODULE);
2469 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002470 goto error;
2471
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002472 /* register this driver with the USB subsystem */
2473 result = usb_register(&gigaset_usb_driver);
2474 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002475 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002476 goto error;
2477 }
2478
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002479 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002480 return 0;
2481
Tilman Schmidte468c042008-02-06 01:38:29 -08002482error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002483 if (driver)
2484 gigaset_freedriver(driver);
2485 driver = NULL;
2486 return -1;
2487}
2488
2489/* bas_gigaset_exit
2490 * This function is called before the kernel module is unloaded.
2491 */
2492static void __exit bas_gigaset_exit(void)
2493{
Tilman Schmidte468c042008-02-06 01:38:29 -08002494 struct bas_cardstate *ucs;
2495 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002496
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002497 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002498 * => no gigaset_start any more
2499 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002500
Tilman Schmidte468c042008-02-06 01:38:29 -08002501 /* stop all connected devices */
2502 for (i = 0; i < driver->minors; i++) {
2503 if (gigaset_shutdown(driver->cs + i) < 0)
2504 continue; /* no device */
2505 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002506
Tilman Schmidte468c042008-02-06 01:38:29 -08002507 /* close all still open channels */
2508 ucs = driver->cs[i].hw.bas;
2509 if (ucs->basstate & BS_B1OPEN) {
2510 gig_dbg(DEBUG_INIT, "closing B1 channel");
2511 usb_control_msg(ucs->udev,
2512 usb_sndctrlpipe(ucs->udev, 0),
2513 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2514 0, 0, NULL, 0, BAS_TIMEOUT);
2515 }
2516 if (ucs->basstate & BS_B2OPEN) {
2517 gig_dbg(DEBUG_INIT, "closing B2 channel");
2518 usb_control_msg(ucs->udev,
2519 usb_sndctrlpipe(ucs->udev, 0),
2520 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2521 0, 0, NULL, 0, BAS_TIMEOUT);
2522 }
2523 if (ucs->basstate & BS_ATOPEN) {
2524 gig_dbg(DEBUG_INIT, "closing AT channel");
2525 usb_control_msg(ucs->udev,
2526 usb_sndctrlpipe(ucs->udev, 0),
2527 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2528 0, 0, NULL, 0, BAS_TIMEOUT);
2529 }
2530 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002531 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002532
2533 /* deregister this driver with the USB subsystem */
2534 usb_deregister(&gigaset_usb_driver);
2535 /* this will call the disconnect-callback */
2536 /* from now on, no disconnect/probe callback should be running */
2537
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002538 gigaset_freedriver(driver);
2539 driver = NULL;
2540}
2541
2542
2543module_init(bas_gigaset_init);
2544module_exit(bas_gigaset_exit);
2545
2546MODULE_AUTHOR(DRIVER_AUTHOR);
2547MODULE_DESCRIPTION(DRIVER_DESC);
2548MODULE_LICENSE("GPL");