blob: 17ea0177a5292bb31217b8fcac7d177ee0fe52a6 [file] [log] [blame]
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001/*
2 * USB driver for Gigaset 307x base via direct USB connection.
3 *
4 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5 * Tilman Schmidt <tilman@imap.cc>,
Tilman Schmidt70440cf2006-04-10 22:55:14 -07006 * Stefan Eilers.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08007 *
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08008 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080014 */
15
16#include "gigaset.h"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080017#include <linux/usb.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20
21/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070022#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080023#define DRIVER_DESC "USB Driver for Gigaset 307x"
24
25
26/* Module parameters */
27
28static int startmode = SM_ISDN;
29static int cidmode = 1;
30
31module_param(startmode, int, S_IRUGO);
32module_param(cidmode, int, S_IRUGO);
33MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
34MODULE_PARM_DESC(cidmode, "Call-ID mode");
35
36#define GIGASET_MINORS 1
37#define GIGASET_MINOR 16
38#define GIGASET_MODULENAME "bas_gigaset"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080039#define GIGASET_DEVNAME "ttyGB"
40
Tilman Schmidt73a88812006-04-22 02:35:30 -070041/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
42#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080043
Tilman Schmidt170ebf82009-03-18 23:44:23 -070044/* interrupt pipe message size according to ibid. ch. 2.2 */
45#define IP_MSGSIZE 3
46
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080047/* Values for the Gigaset 307x */
48#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070049#define USB_3070_PRODUCT_ID 0x0001
50#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080051#define USB_SX303_PRODUCT_ID 0x0021
52#define USB_SX353_PRODUCT_ID 0x0022
53
54/* table of devices that work with this driver */
Tilman Schmidt7891adf2009-10-25 09:30:37 +000055static const struct usb_device_id gigaset_table[] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070056 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
57 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
60 { } /* Terminating entry */
61};
62
63MODULE_DEVICE_TABLE(usb, gigaset_table);
64
Tilman Schmidt06163f82006-06-26 00:25:33 -070065/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080066
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068static int gigaset_probe(struct usb_interface *interface,
69 const struct usb_device_id *id);
70
71/* Function will be called if the device is unplugged */
72static void gigaset_disconnect(struct usb_interface *interface);
73
Tilman Schmidt024fd292008-02-06 01:38:26 -080074/* functions called before/after suspend */
75static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
76static int gigaset_resume(struct usb_interface *intf);
77
78/* functions called before/after device reset */
79static int gigaset_pre_reset(struct usb_interface *intf);
80static int gigaset_post_reset(struct usb_interface *intf);
81
Tilman Schmidt06163f82006-06-26 00:25:33 -070082static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070083static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070084static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070085static int atwrite_submit(struct cardstate *, unsigned char *, int);
86static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080087
Tilman Schmidt06163f82006-06-26 00:25:33 -070088/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089
90struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070091 struct usb_device *udev; /* USB device pointer */
92 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080093 unsigned char minor; /* starting minor number */
94
Tilman Schmidt784d5852006-04-10 22:55:04 -070095 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080096 struct usb_ctrlrequest dr_ctrl;
97 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070098 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080099
100 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700101 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800102 struct usb_ctrlrequest dr_cmd_out;
103 int retry_cmd_out;
104
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800106 struct usb_ctrlrequest dr_cmd_in;
107 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700108 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800109
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700111 unsigned char *int_in_buf;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000112 struct work_struct int_in_wq; /* for usb_clear_halt() */
113 struct timer_list timer_int_in; /* int read retry delay */
114 int retry_int_in;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800115
116 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800117 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800118 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800119 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800120 int rcvbuf_size; /* size of AT receive buffer */
121 /* 0: no receive in progress */
122 int retry_cmd_in; /* receive req retry count */
123};
124
125/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700126#define BS_ATOPEN 0x001 /* AT channel open */
127#define BS_B1OPEN 0x002 /* B channel 1 open */
128#define BS_B2OPEN 0x004 /* B channel 2 open */
129#define BS_ATREADY 0x008 /* base ready for AT command */
130#define BS_INIT 0x010 /* base has signalled INIT_OK */
131#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
132#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
133#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800134#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000135#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800136
137
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000138static struct gigaset_driver *driver;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800139
140/* usb specific object needed to register this driver with the usb subsystem */
141static struct usb_driver gigaset_usb_driver = {
142 .name = GIGASET_MODULENAME,
143 .probe = gigaset_probe,
144 .disconnect = gigaset_disconnect,
145 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800146 .suspend = gigaset_suspend,
147 .resume = gigaset_resume,
148 .reset_resume = gigaset_post_reset,
149 .pre_reset = gigaset_pre_reset,
150 .post_reset = gigaset_post_reset,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700151 .disable_hub_initiated_lpm = 1,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800152};
153
Tilman Schmidt73a88812006-04-22 02:35:30 -0700154/* get message text for usb_submit_urb return code
155 */
156static char *get_usb_rcmsg(int rc)
157{
158 static char unkmsg[28];
159
160 switch (rc) {
161 case 0:
162 return "success";
163 case -ENOMEM:
164 return "out of memory";
165 case -ENODEV:
166 return "device not present";
167 case -ENOENT:
168 return "endpoint not present";
169 case -ENXIO:
170 return "URB type not supported";
171 case -EINVAL:
172 return "invalid argument";
173 case -EAGAIN:
174 return "start frame too early or too much scheduled";
175 case -EFBIG:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000176 return "too many isoc frames requested";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700177 case -EPIPE:
178 return "endpoint stalled";
179 case -EMSGSIZE:
180 return "invalid packet size";
181 case -ENOSPC:
182 return "would overcommit USB bandwidth";
183 case -ESHUTDOWN:
184 return "device shut down";
185 case -EPERM:
186 return "reject flag set";
187 case -EHOSTUNREACH:
188 return "device suspended";
189 default:
190 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
191 return unkmsg;
192 }
193}
194
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800195/* get message text for USB status code
196 */
197static char *get_usb_statmsg(int status)
198{
199 static char unkmsg[28];
200
201 switch (status) {
202 case 0:
203 return "success";
204 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700205 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800206 case -EINPROGRESS:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000207 return "URB still pending";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800208 case -EPROTO:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000209 return "bitstuff error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800210 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700211 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700212 case -ETIME:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000213 return "USB response timeout";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700214 case -EPIPE:
215 return "endpoint stalled";
216 case -ECOMM:
217 return "IN buffer overrun";
218 case -ENOSR:
219 return "OUT buffer underrun";
220 case -EOVERFLOW:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000221 return "endpoint babble";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800222 case -EREMOTEIO:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000223 return "short packet";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700224 case -ENODEV:
225 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800226 case -EXDEV:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000227 return "partial isoc transfer";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800228 case -EINVAL:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000229 return "ISO madness";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700230 case -ECONNRESET:
231 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700233 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800234 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700235 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800236 return unkmsg;
237 }
238}
239
240/* usb_pipetype_str
241 * retrieve string representation of USB pipe type
242 */
243static inline char *usb_pipetype_str(int pipe)
244{
245 if (usb_pipeisoc(pipe))
246 return "Isoc";
247 if (usb_pipeint(pipe))
248 return "Int";
249 if (usb_pipecontrol(pipe))
250 return "Ctrl";
251 if (usb_pipebulk(pipe))
252 return "Bulk";
253 return "?";
254}
255
256/* dump_urb
257 * write content of URB to syslog for debugging
258 */
259static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700260 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800261{
262#ifdef CONFIG_GIGASET_DEBUG
263 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700264 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800265 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700266 gig_dbg(level,
267 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800268 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700269 (unsigned long) urb->dev,
270 usb_pipetype_str(urb->pipe),
271 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
272 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800273 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700274 urb->transfer_flags);
275 gig_dbg(level,
276 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800277 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 (unsigned long) urb->transfer_buffer,
279 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800280 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700281 gig_dbg(level,
282 " start_frame=%d, number_of_packets=%d, interval=%d, "
283 "error_count=%d,",
284 urb->start_frame, urb->number_of_packets, urb->interval,
285 urb->error_count);
286 gig_dbg(level,
287 " context=0x%08lx, complete=0x%08lx, "
288 "iso_frame_desc[]={",
289 (unsigned long) urb->context,
290 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800291 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700292 struct usb_iso_packet_descriptor *pifd
293 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700294 gig_dbg(level,
295 " {offset=%u, length=%u, actual_length=%u, "
296 "status=%u}",
297 pifd->offset, pifd->length, pifd->actual_length,
298 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800299 }
300 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700301 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800302#endif
303}
304
305/* read/set modem control bits etc. (m10x only) */
306static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700307 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800308{
309 return -EINVAL;
310}
311
312static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
313{
314 return -EINVAL;
315}
316
317static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
318{
319 return -EINVAL;
320}
321
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000322/* set/clear bits in base connection state, return previous state
323 */
324static inline int update_basstate(struct bas_cardstate *ucs,
325 int set, int clear)
326{
327 unsigned long flags;
328 int state;
329
330 spin_lock_irqsave(&ucs->lock, flags);
331 state = ucs->basstate;
332 ucs->basstate = (state & ~clear) | set;
333 spin_unlock_irqrestore(&ucs->lock, flags);
334 return state;
335}
336
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800337/* error_hangup
338 * hang up any existing connection because of an unrecoverable error
339 * This function may be called from any context and takes care of scheduling
340 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700341 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800342 * argument:
343 * B channel control structure
344 */
345static inline void error_hangup(struct bc_state *bcs)
346{
347 struct cardstate *cs = bcs->cs;
348
Tilman Schmidt1528b182010-02-22 13:09:52 +0000349 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800350 gigaset_schedule_event(cs);
351}
352
353/* error_reset
354 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700355 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700356 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt60798c682010-09-30 13:35:21 +0000357 * cs->hw.bas->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800358 * argument:
359 * controller state structure
360 */
361static inline void error_reset(struct cardstate *cs)
362{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000363 /* reset interrupt pipe to recover (ignore errors) */
364 update_basstate(cs->hw.bas, BS_RESETTING, 0);
Tilman Schmidt60798c682010-09-30 13:35:21 +0000365 if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
366 /* submission failed, escalate to USB port reset */
367 usb_queue_reset_device(cs->hw.bas->interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800368}
369
370/* check_pending
371 * check for completion of pending control request
372 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700373 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800374 */
375static void check_pending(struct bas_cardstate *ucs)
376{
377 unsigned long flags;
378
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800379 spin_lock_irqsave(&ucs->lock, flags);
380 switch (ucs->pending) {
381 case 0:
382 break;
383 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800384 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800385 ucs->pending = 0;
386 break;
387 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800388 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800389 ucs->pending = 0;
390 break;
391 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800392 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800393 ucs->pending = 0;
394 break;
395 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800396 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800397 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800398 break;
399 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800400 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800401 ucs->pending = 0;
402 break;
403 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800404 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800405 ucs->pending = 0;
406 break;
407 case HD_DEVICE_INIT_ACK: /* no reply expected */
408 ucs->pending = 0;
409 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000410 case HD_RESET_INTERRUPT_PIPE:
411 if (!(ucs->basstate & BS_RESETTING))
412 ucs->pending = 0;
413 break;
Joe Perches475be4d2012-02-19 19:52:38 -0800414 /*
415 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
416 * and should never end up here
417 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800418 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700419 dev_warn(&ucs->interface->dev,
420 "unknown pending request 0x%02x cleared\n",
421 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800422 ucs->pending = 0;
423 }
424
425 if (!ucs->pending)
426 del_timer(&ucs->timer_ctrl);
427
428 spin_unlock_irqrestore(&ucs->lock, flags);
429}
430
431/* cmd_in_timeout
432 * timeout routine for command input request
433 * argument:
434 * controller state structure
435 */
436static void cmd_in_timeout(unsigned long data)
437{
438 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700439 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700440 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800441
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800442 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700443 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800444 return;
445 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800446
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000447 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700448 dev_err(cs->dev,
449 "control read: timeout, giving up after %d tries\n",
450 ucs->retry_cmd_in);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000451 kfree(ucs->rcvbuf);
452 ucs->rcvbuf = NULL;
453 ucs->rcvbuf_size = 0;
454 error_reset(cs);
455 return;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700456 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000457
458 gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
459 __func__, ucs->retry_cmd_in);
460 rc = atread_submit(cs, BAS_TIMEOUT);
461 if (rc < 0) {
462 kfree(ucs->rcvbuf);
463 ucs->rcvbuf = NULL;
464 ucs->rcvbuf_size = 0;
465 if (rc != -ENODEV)
466 error_reset(cs);
467 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800468}
469
Tilman Schmidt06163f82006-06-26 00:25:33 -0700470/* read_ctrl_callback
471 * USB completion handler for control pipe input
472 * called by the USB subsystem in interrupt context
473 * parameter:
474 * urb USB request block
475 * urb->context = inbuf structure for controller state
476 */
David Howells7d12e782006-10-05 14:55:46 +0100477static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700478{
479 struct inbuf_t *inbuf = urb->context;
480 struct cardstate *cs = inbuf->cs;
481 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800482 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700483 unsigned numbytes;
484 int rc;
485
486 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800487 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700488 del_timer(&ucs->timer_cmd_in);
489
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800490 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700491 case 0: /* normal completion */
492 numbytes = urb->actual_length;
493 if (unlikely(numbytes != ucs->rcvbuf_size)) {
494 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800495 "control read: received %d chars, expected %d\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700496 numbytes, ucs->rcvbuf_size);
497 if (numbytes > ucs->rcvbuf_size)
498 numbytes = ucs->rcvbuf_size;
499 }
500
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000501 /* copy received bytes to inbuf, notify event layer */
502 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
503 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
504 gigaset_schedule_event(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700505 }
506 break;
507
508 case -ENOENT: /* cancelled */
509 case -ECONNRESET: /* cancelled (async) */
510 case -EINPROGRESS: /* pending */
511 case -ENODEV: /* device removed */
512 case -ESHUTDOWN: /* device shut down */
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000513 /* no further action necessary */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700514 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800515 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700516 break;
517
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000518 default: /* other errors: retry */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700519 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000520 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
521 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700522 rc = atread_submit(cs, BAS_TIMEOUT);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000523 if (rc >= 0)
524 /* successfully resubmitted, skip freeing */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700525 return;
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000526 if (rc == -ENODEV)
527 /* disconnect, no further action necessary */
528 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700529 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000530 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
531 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 error_reset(cs);
533 }
534
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000535 /* read finished, free buffer */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700536 kfree(ucs->rcvbuf);
537 ucs->rcvbuf = NULL;
538 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700539}
540
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800541/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700542 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800543 * parameters:
544 * cs controller state structure
545 * timeout timeout in 1/10 sec., 0: none
546 * return value:
547 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800548 * -EBUSY if another request is pending
549 * any URB submission error code
550 */
551static int atread_submit(struct cardstate *cs, int timeout)
552{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700553 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800554 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800555 int ret;
556
Tilman Schmidt784d5852006-04-10 22:55:04 -0700557 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
558 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559
Tilman Schmidt024fd292008-02-06 01:38:26 -0800560 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
561 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700562 dev_err(cs->dev,
563 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 return -EBUSY;
565 }
566
Tilman Schmidt024fd292008-02-06 01:38:26 -0800567 if (basstate & BS_SUSPEND) {
568 dev_notice(cs->dev,
569 "HD_READ_ATMESSAGE not submitted, "
570 "suspend in progress\n");
571 update_basstate(ucs, 0, BS_ATRDPEND);
572 /* treat like disconnect */
573 return -ENODEV;
574 }
575
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800576 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
577 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
578 ucs->dr_cmd_in.wValue = 0;
579 ucs->dr_cmd_in.wIndex = 0;
580 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
581 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700582 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000583 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700584 ucs->rcvbuf, ucs->rcvbuf_size,
585 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800586
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000587 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
588 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700589 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700590 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700591 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592 return ret;
593 }
594
595 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700596 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +0000597 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800598 }
599 return 0;
600}
601
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000602/* int_in_work
603 * workqueue routine to clear halt on interrupt in endpoint
604 */
605
606static void int_in_work(struct work_struct *work)
607{
608 struct bas_cardstate *ucs =
609 container_of(work, struct bas_cardstate, int_in_wq);
610 struct urb *urb = ucs->urb_int_in;
611 struct cardstate *cs = urb->context;
612 int rc;
613
614 /* clear halt condition */
615 rc = usb_clear_halt(ucs->udev, urb->pipe);
616 gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
617 if (rc == 0)
618 /* success, resubmit interrupt read URB */
619 rc = usb_submit_urb(urb, GFP_ATOMIC);
620 if (rc != 0 && rc != -ENODEV) {
621 dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
622 rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
623 if (rc == 0) {
624 rc = usb_reset_device(ucs->udev);
625 usb_unlock_device(ucs->udev);
626 }
627 }
628 ucs->retry_int_in = 0;
629}
630
631/* int_in_resubmit
632 * timer routine for interrupt read delayed resubmit
633 * argument:
634 * controller state structure
635 */
636static void int_in_resubmit(unsigned long data)
637{
638 struct cardstate *cs = (struct cardstate *) data;
639 struct bas_cardstate *ucs = cs->hw.bas;
640 int rc;
641
642 if (ucs->retry_int_in++ >= BAS_RETRY) {
643 dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
644 ucs->retry_int_in);
645 usb_queue_reset_device(ucs->interface);
646 return;
647 }
648
649 gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
650 rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
651 if (rc != 0 && rc != -ENODEV) {
652 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
653 get_usb_rcmsg(rc));
654 usb_queue_reset_device(ucs->interface);
655 }
656}
657
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800658/* read_int_callback
659 * USB completion handler for interrupt pipe input
660 * called by the USB subsystem in interrupt context
661 * parameter:
662 * urb USB request block
663 * urb->context = controller state structure
664 */
David Howells7d12e782006-10-05 14:55:46 +0100665static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800666{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700667 struct cardstate *cs = urb->context;
668 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800669 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800670 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800671 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700672 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800673 unsigned l;
674 int channel;
675
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800676 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800677 case 0: /* success */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000678 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800679 break;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000680 case -EPIPE: /* endpoint stalled */
681 schedule_work(&ucs->int_in_wq);
682 /* fall through */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700683 case -ENOENT: /* cancelled */
684 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800685 case -EINPROGRESS: /* pending */
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000686 case -ENODEV: /* device removed */
687 case -ESHUTDOWN: /* device shut down */
688 /* no further action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700689 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800690 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800691 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000692 case -EPROTO: /* protocol error or unplug */
693 case -EILSEQ:
694 case -ETIME:
695 /* resubmit after delay */
696 gig_dbg(DEBUG_USBREQ, "%s: %s",
697 __func__, get_usb_statmsg(status));
698 mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700699 return;
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000700 default: /* other errors: just resubmit */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700701 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800702 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800703 goto resubmit;
704 }
705
Tilman Schmidt73a88812006-04-22 02:35:30 -0700706 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700707 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700708 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
709 urb->actual_length);
710 goto resubmit;
711 }
712
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800713 l = (unsigned) ucs->int_in_buf[1] +
Joe Perches475be4d2012-02-19 19:52:38 -0800714 (((unsigned) ucs->int_in_buf[2]) << 8);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800715
Tilman Schmidt784d5852006-04-10 22:55:04 -0700716 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
717 urb->actual_length, (int)ucs->int_in_buf[0], l,
718 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800719
720 channel = 0;
721
722 switch (ucs->int_in_buf[0]) {
723 case HD_DEVICE_INIT_OK:
724 update_basstate(ucs, BS_INIT, 0);
725 break;
726
727 case HD_READY_SEND_ATDATA:
728 del_timer(&ucs->timer_atrdy);
729 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
730 start_cbsend(cs);
731 break;
732
733 case HD_OPEN_B2CHANNEL_ACK:
734 ++channel;
735 case HD_OPEN_B1CHANNEL_ACK:
736 bcs = cs->bcs + channel;
737 update_basstate(ucs, BS_B1OPEN << channel, 0);
738 gigaset_bchannel_up(bcs);
739 break;
740
741 case HD_OPEN_ATCHANNEL_ACK:
742 update_basstate(ucs, BS_ATOPEN, 0);
743 start_cbsend(cs);
744 break;
745
746 case HD_CLOSE_B2CHANNEL_ACK:
747 ++channel;
748 case HD_CLOSE_B1CHANNEL_ACK:
749 bcs = cs->bcs + channel;
750 update_basstate(ucs, 0, BS_B1OPEN << channel);
751 stopurbs(bcs->hw.bas);
752 gigaset_bchannel_down(bcs);
753 break;
754
755 case HD_CLOSE_ATCHANNEL_ACK:
756 update_basstate(ucs, 0, BS_ATOPEN);
757 break;
758
759 case HD_B2_FLOW_CONTROL:
760 ++channel;
761 case HD_B1_FLOW_CONTROL:
762 bcs = cs->bcs + channel;
763 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700764 &bcs->hw.bas->corrbytes);
765 gig_dbg(DEBUG_ISO,
766 "Flow control (channel %d, sub %d): 0x%02x => %d",
767 channel, bcs->hw.bas->numsub, l,
768 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800769 break;
770
771 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
772 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700773 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800774 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800775 break;
776 }
777 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000778 if (ucs->basstate & BS_ATRDPEND) {
779 spin_unlock_irqrestore(&cs->lock, flags);
780 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800781 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000782 l, ucs->rcvbuf_size);
783 break;
784 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700786 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700787 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700788 "receive AT data overrun, %d bytes lost\n",
789 ucs->rcvbuf_size);
790 kfree(ucs->rcvbuf);
791 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800792 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000793 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
794 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800795 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700796 dev_err(cs->dev, "out of memory receiving AT data\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800797 break;
798 }
799 ucs->rcvbuf_size = l;
800 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000801 rc = atread_submit(cs, BAS_TIMEOUT);
802 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800803 kfree(ucs->rcvbuf);
804 ucs->rcvbuf = NULL;
805 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800806 }
807 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +0000808 if (rc < 0 && rc != -ENODEV)
809 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800810 break;
811
812 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000813 update_basstate(ucs, 0, BS_RESETTING);
814 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 break;
816
817 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700818 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800819 break;
820
821 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700822 dev_warn(cs->dev,
823 "unknown Gigaset signal 0x%02x (%u) ignored\n",
824 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800825 }
826
827 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800828 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800829
830resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800831 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700832 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700833 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700834 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800835 error_reset(cs);
836 }
837}
838
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800839/* read_iso_callback
840 * USB completion handler for B channel isochronous input
841 * called by the USB subsystem in interrupt context
842 * parameter:
843 * urb USB request block of completed request
844 * urb->context = bc_state structure
845 */
David Howells7d12e782006-10-05 14:55:46 +0100846static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800847{
848 struct bc_state *bcs;
849 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800850 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800851 unsigned long flags;
852 int i, rc;
853
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800854 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800855 if (unlikely(status == -ENOENT ||
856 status == -ECONNRESET ||
857 status == -EINPROGRESS ||
858 status == -ENODEV ||
859 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700860 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800861 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800862 return;
863 }
864
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700865 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800866 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800867
868 spin_lock_irqsave(&ubc->isoinlock, flags);
869 if (likely(ubc->isoindone == NULL)) {
870 /* pass URB to tasklet */
871 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800872 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000873 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800874 } else {
875 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000876 gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800877 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800878 for (i = 0; i < BAS_NUMFRAMES; i++) {
879 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
880 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700881 urb->iso_frame_desc[i].status !=
Joe Perches475be4d2012-02-19 19:52:38 -0800882 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800884 urb->iso_frame_desc[i].status = 0;
885 urb->iso_frame_desc[i].actual_length = 0;
886 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800887 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700888 /* urb->dev is clobbered by USB subsystem */
889 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800890 urb->transfer_flags = URB_ISO_ASAP;
891 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800892 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700893 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700894 dev_err(bcs->cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800895 "could not resubmit isoc read URB: %s\n",
Tilman Schmidtbb7196d2010-09-30 13:35:52 +0000896 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800897 dump_urb(DEBUG_ISO, "isoc read", urb);
898 error_hangup(bcs);
899 }
900 }
901 }
902 spin_unlock_irqrestore(&ubc->isoinlock, flags);
903}
904
905/* write_iso_callback
906 * USB completion handler for B channel isochronous output
907 * called by the USB subsystem in interrupt context
908 * parameter:
909 * urb USB request block of completed request
910 * urb->context = isow_urbctx_t structure
911 */
David Howells7d12e782006-10-05 14:55:46 +0100912static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800913{
914 struct isow_urbctx_t *ucx;
915 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800916 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800917 unsigned long flags;
918
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800919 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800920 if (unlikely(status == -ENOENT ||
921 status == -ECONNRESET ||
922 status == -EINPROGRESS ||
923 status == -ENODEV ||
924 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700925 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800926 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800927 return;
928 }
929
930 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700931 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800932 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800933 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800934
935 spin_lock_irqsave(&ubc->isooutlock, flags);
936 ubc->isooutovfl = ubc->isooutdone;
937 ubc->isooutdone = ucx;
938 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000939 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940}
941
942/* starturbs
943 * prepare and submit USB request blocks for isochronous input and output
944 * argument:
945 * B channel control structure
946 * return value:
947 * 0 on success
948 * < 0 on error (no URBs submitted)
949 */
950static int starturbs(struct bc_state *bcs)
951{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700952 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800953 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800954 int j, k;
955 int rc;
956
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800957 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000958 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800959 bcs->inputstate |= INS_flag_hunt;
960
961 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800962 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800963 for (k = 0; k < BAS_INURBS; k++) {
964 urb = ubc->isoinurbs[k];
965 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800966 rc = -EFAULT;
967 goto error;
968 }
969
970 urb->dev = bcs->cs->hw.bas->udev;
971 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
972 urb->transfer_flags = URB_ISO_ASAP;
973 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
974 urb->transfer_buffer_length = BAS_INBUFSIZE;
975 urb->number_of_packets = BAS_NUMFRAMES;
976 urb->interval = BAS_FRAMETIME;
977 urb->complete = read_iso_callback;
978 urb->context = bcs;
979 for (j = 0; j < BAS_NUMFRAMES; j++) {
980 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
981 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
982 urb->iso_frame_desc[j].status = 0;
983 urb->iso_frame_desc[j].actual_length = 0;
984 }
985
986 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000987 rc = usb_submit_urb(urb, GFP_ATOMIC);
988 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800989 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800990 }
991
992 /* initialize L2 transmission */
993 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
994
995 /* set up isochronous output URBs for flag idling */
996 for (k = 0; k < BAS_OUTURBS; ++k) {
997 urb = ubc->isoouturbs[k].urb;
998 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800999 rc = -EFAULT;
1000 goto error;
1001 }
1002 urb->dev = bcs->cs->hw.bas->udev;
1003 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
1004 urb->transfer_flags = URB_ISO_ASAP;
1005 urb->transfer_buffer = ubc->isooutbuf->data;
1006 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1007 urb->number_of_packets = BAS_NUMFRAMES;
1008 urb->interval = BAS_FRAMETIME;
1009 urb->complete = write_iso_callback;
1010 urb->context = &ubc->isoouturbs[k];
1011 for (j = 0; j < BAS_NUMFRAMES; ++j) {
1012 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
1013 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
1014 urb->iso_frame_desc[j].status = 0;
1015 urb->iso_frame_desc[j].actual_length = 0;
1016 }
1017 ubc->isoouturbs[k].limit = -1;
1018 }
1019
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001020 /* keep one URB free, submit the others */
Joe Perches475be4d2012-02-19 19:52:38 -08001021 for (k = 0; k < BAS_OUTURBS - 1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001022 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001023 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001024 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001025 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001026 }
1027 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Joe Perches475be4d2012-02-19 19:52:38 -08001028 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS - 1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001029 ubc->isooutdone = ubc->isooutovfl = NULL;
1030 return 0;
Joe Perches475be4d2012-02-19 19:52:38 -08001031error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001032 stopurbs(ubc);
1033 return rc;
1034}
1035
1036/* stopurbs
1037 * cancel the USB request blocks for isochronous input and output
1038 * errors are silently ignored
1039 * argument:
1040 * B channel control structure
1041 */
1042static void stopurbs(struct bas_bc_state *ubc)
1043{
1044 int k, rc;
1045
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001046 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001047
1048 for (k = 0; k < BAS_INURBS; ++k) {
1049 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001050 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001051 "%s: isoc input URB %d unlinked, result = %s",
1052 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001053 }
1054
1055 for (k = 0; k < BAS_OUTURBS; ++k) {
1056 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001057 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001058 "%s: isoc output URB %d unlinked, result = %s",
1059 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001060 }
1061}
1062
1063/* Isochronous Write - Bottom Half */
1064/* =============================== */
1065
1066/* submit_iso_write_urb
1067 * fill and submit the next isochronous write URB
1068 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001069 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001070 * return value:
1071 * number of frames submitted in URB
1072 * 0 if URB not submitted because no data available (isooutbuf busy)
1073 * error code < 0 on error
1074 */
1075static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1076{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001077 struct urb *urb = ucx->urb;
1078 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001079 struct usb_iso_packet_descriptor *ifd;
1080 int corrbytes, nframe, rc;
1081
Tilman Schmidt784d5852006-04-10 22:55:04 -07001082 /* urb->dev is clobbered by USB subsystem */
1083 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001084 urb->transfer_flags = URB_ISO_ASAP;
1085 urb->transfer_buffer = ubc->isooutbuf->data;
1086 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1087
1088 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1089 ifd = &urb->iso_frame_desc[nframe];
1090
1091 /* compute frame length according to flow control */
1092 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001093 corrbytes = atomic_read(&ubc->corrbytes);
1094 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001095 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1096 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001097 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1098 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1099 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1100 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1101 ifd->length += corrbytes;
1102 atomic_add(-corrbytes, &ubc->corrbytes);
1103 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001104
1105 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001106 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1107 if (rc < 0) {
1108 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001109 gig_dbg(DEBUG_ISO,
1110 "%s: buffer busy at frame %d",
1111 __func__, nframe);
1112 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001113 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001114 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001115 dev_err(ucx->bcs->cs->dev,
1116 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001117 __func__, rc, nframe);
1118 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001119 }
1120 break;
1121 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001122 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001123 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001124 ifd->status = 0;
1125 ifd->actual_length = 0;
1126 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001127 if (unlikely(nframe == 0))
1128 return 0; /* no data to send */
1129 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001130
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001131 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001132 if (unlikely(rc)) {
1133 if (rc == -ENODEV)
1134 /* device removed - give up silently */
1135 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1136 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001137 dev_err(ucx->bcs->cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001138 "could not submit isoc write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001139 get_usb_rcmsg(rc));
1140 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001141 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001142 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001143 return nframe;
1144}
1145
1146/* write_iso_tasklet
1147 * tasklet scheduled when an isochronous output URB from the Gigaset device
1148 * has completed
1149 * parameter:
1150 * data B channel state structure
1151 */
1152static void write_iso_tasklet(unsigned long data)
1153{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001154 struct bc_state *bcs = (struct bc_state *) data;
1155 struct bas_bc_state *ubc = bcs->hw.bas;
1156 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 struct isow_urbctx_t *done, *next, *ovfl;
1158 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001159 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001160 struct usb_iso_packet_descriptor *ifd;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001161 unsigned long flags;
1162 int i;
1163 struct sk_buff *skb;
1164 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001165 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001166
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001167 /* loop while completed URBs arrive in time */
1168 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001169 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001170 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001171 return;
1172 }
1173
1174 /* retrieve completed URBs */
1175 spin_lock_irqsave(&ubc->isooutlock, flags);
1176 done = ubc->isooutdone;
1177 ubc->isooutdone = NULL;
1178 ovfl = ubc->isooutovfl;
1179 ubc->isooutovfl = NULL;
1180 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1181 if (ovfl) {
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001182 dev_err(cs->dev, "isoc write underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001183 error_hangup(bcs);
1184 break;
1185 }
1186 if (!done)
1187 break;
1188
1189 /* submit free URB if available */
1190 spin_lock_irqsave(&ubc->isooutlock, flags);
1191 next = ubc->isooutfree;
1192 ubc->isooutfree = NULL;
1193 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1194 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001195 rc = submit_iso_write_urb(next);
1196 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001197 /* could not submit URB, put it back */
1198 spin_lock_irqsave(&ubc->isooutlock, flags);
1199 if (ubc->isooutfree == NULL) {
1200 ubc->isooutfree = next;
1201 next = NULL;
1202 }
1203 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1204 if (next) {
1205 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001206 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001207 "losing isoc write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001208 error_hangup(bcs);
1209 }
1210 }
1211 }
1212
1213 /* process completed URB */
1214 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001215 status = done->status;
1216 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001217 case -EXDEV: /* partial completion */
1218 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1219 __func__);
1220 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001221 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001222 /* inspect individual frames
1223 * assumptions (for lack of documentation):
1224 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001225 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001226 * - all following frames are not sent at all
1227 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001228 for (i = 0; i < BAS_NUMFRAMES; i++) {
1229 ifd = &urb->iso_frame_desc[i];
1230 if (ifd->status ||
1231 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001232 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001233 "isoc write: frame %d[%d/%d]: %s\n",
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001234 i, ifd->actual_length,
1235 ifd->length,
1236 get_usb_statmsg(ifd->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 break;
1238 }
1239 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001240 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001241 case -EPIPE: /* stall - probably underrun */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001242 dev_err(cs->dev, "isoc write: stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 error_hangup(bcs);
1244 break;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001245 default: /* other errors */
1246 dev_warn(cs->dev, "isoc write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001247 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001248 }
1249
1250 /* mark the write buffer area covered by this URB as free */
1251 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001252 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001253
1254 /* mark URB as free */
1255 spin_lock_irqsave(&ubc->isooutlock, flags);
1256 next = ubc->isooutfree;
1257 ubc->isooutfree = done;
1258 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1259 if (next) {
1260 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001261 rc = submit_iso_write_urb(next);
1262 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001263 /* couldn't submit */
1264 error_hangup(bcs);
1265 }
1266 }
1267 }
1268
1269 /* process queued SKBs */
1270 while ((skb = skb_dequeue(&bcs->squeue))) {
1271 /* copy to output buffer, doing L2 encapsulation */
1272 len = skb->len;
1273 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1274 /* insufficient buffer space, push back onto queue */
1275 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001276 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1277 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001278 break;
1279 }
1280 skb_pull(skb, len);
1281 gigaset_skb_sent(bcs, skb);
1282 dev_kfree_skb_any(skb);
1283 }
1284}
1285
1286/* Isochronous Read - Bottom Half */
1287/* ============================== */
1288
1289/* read_iso_tasklet
1290 * tasklet scheduled when an isochronous input URB from the Gigaset device
1291 * has completed
1292 * parameter:
1293 * data B channel state structure
1294 */
1295static void read_iso_tasklet(unsigned long data)
1296{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001297 struct bc_state *bcs = (struct bc_state *) data;
1298 struct bas_bc_state *ubc = bcs->hw.bas;
1299 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001300 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001301 int status;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001302 struct usb_iso_packet_descriptor *ifd;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001303 char *rcvbuf;
1304 unsigned long flags;
1305 int totleft, numbytes, offset, frame, rc;
1306
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001307 /* loop while more completed URBs arrive in the meantime */
1308 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001309 /* retrieve URB */
1310 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001311 urb = ubc->isoindone;
1312 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001313 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1314 return;
1315 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001316 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001317 ubc->isoindone = NULL;
1318 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001319 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001320 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001321 get_usb_statmsg(ubc->loststatus),
1322 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001323 ubc->loststatus = -EINPROGRESS;
1324 }
1325 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1326
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001327 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001328 gig_dbg(DEBUG_ISO,
1329 "%s: channel not running, "
1330 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001331 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001332 return;
1333 }
1334
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001335 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001336 case 0: /* normal completion */
1337 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001338 case -EXDEV: /* inspect individual frames
1339 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1341 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001342 break;
1343 case -ENOENT:
1344 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001345 case -EINPROGRESS:
1346 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001347 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001348 continue; /* -> skip */
1349 case -EPIPE:
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001350 dev_err(cs->dev, "isoc read: stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001351 error_hangup(bcs);
1352 continue; /* -> skip */
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001353 default: /* other error */
1354 dev_warn(cs->dev, "isoc read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001355 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001356 goto error;
1357 }
1358
1359 rcvbuf = urb->transfer_buffer;
1360 totleft = urb->actual_length;
1361 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001362 ifd = &urb->iso_frame_desc[frame];
1363 numbytes = ifd->actual_length;
1364 switch (ifd->status) {
1365 case 0: /* success */
1366 break;
1367 case -EPROTO: /* protocol error or unplug */
1368 case -EILSEQ:
1369 case -ETIME:
1370 /* probably just disconnected, ignore */
1371 gig_dbg(DEBUG_ISO,
1372 "isoc read: frame %d[%d]: %s\n",
1373 frame, numbytes,
1374 get_usb_statmsg(ifd->status));
1375 break;
1376 default: /* other error */
1377 /* report, assume transferred bytes are ok */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001378 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001379 "isoc read: frame %d[%d]: %s\n",
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001380 frame, numbytes,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001381 get_usb_statmsg(ifd->status));
1382 }
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001383 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001385 "isoc read: frame %d[%d]: %s\n",
1386 frame, numbytes,
1387 "exceeds max frame size");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001388 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001389 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001390 "isoc read: frame %d[%d]: %s\n",
1391 frame, numbytes,
1392 "exceeds total transfer length");
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001393 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001394 }
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001395 offset = ifd->offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001396 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001397 dev_warn(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001398 "isoc read: frame %d[%d]: %s\n",
1399 frame, numbytes,
1400 "exceeds end of buffer");
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001401 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001402 }
1403 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1404 totleft -= numbytes;
1405 }
1406 if (unlikely(totleft > 0))
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001407 dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001408 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001409
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001410error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001411 /* URB processed, resubmit */
1412 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1413 urb->iso_frame_desc[frame].status = 0;
1414 urb->iso_frame_desc[frame].actual_length = 0;
1415 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 /* urb->dev is clobbered by USB subsystem */
1417 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 urb->transfer_flags = URB_ISO_ASAP;
1419 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001420 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001421 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001422 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001423 "could not resubmit isoc read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001424 get_usb_rcmsg(rc));
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001425 dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001426 error_hangup(bcs);
1427 }
1428 }
1429}
1430
1431/* Channel Operations */
1432/* ================== */
1433
1434/* req_timeout
1435 * timeout routine for control output request
1436 * argument:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001437 * controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001438 */
1439static void req_timeout(unsigned long data)
1440{
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001441 struct cardstate *cs = (struct cardstate *) data;
1442 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001443 int pending;
1444 unsigned long flags;
1445
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446 check_pending(ucs);
1447
1448 spin_lock_irqsave(&ucs->lock, flags);
1449 pending = ucs->pending;
1450 ucs->pending = 0;
1451 spin_unlock_irqrestore(&ucs->lock, flags);
1452
1453 switch (pending) {
1454 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001455 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001456 break;
1457
1458 case HD_OPEN_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001459 dev_err(cs->dev, "timeout opening AT channel\n");
1460 error_reset(cs);
1461 break;
1462
1463 case HD_OPEN_B1CHANNEL:
1464 dev_err(cs->dev, "timeout opening channel 1\n");
1465 error_hangup(&cs->bcs[0]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001466 break;
1467
1468 case HD_OPEN_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001469 dev_err(cs->dev, "timeout opening channel 2\n");
1470 error_hangup(&cs->bcs[1]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001471 break;
1472
1473 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001474 dev_err(cs->dev, "timeout closing AT channel\n");
1475 error_reset(cs);
1476 break;
1477
1478 case HD_CLOSE_B1CHANNEL:
1479 dev_err(cs->dev, "timeout closing channel 1\n");
1480 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001481 break;
1482
1483 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001484 dev_err(cs->dev, "timeout closing channel 2\n");
1485 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001486 break;
1487
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001488 case HD_RESET_INTERRUPT_PIPE:
1489 /* error recovery escalation */
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001490 dev_err(cs->dev,
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001491 "reset interrupt pipe timeout, attempting USB reset\n");
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001492 usb_queue_reset_device(ucs->interface);
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001493 break;
1494
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001495 default:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001496 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001497 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001498 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001499
1500 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001501}
1502
1503/* write_ctrl_callback
1504 * USB completion handler for control pipe output
1505 * called by the USB subsystem in interrupt context
1506 * parameter:
1507 * urb USB request block of completed request
1508 * urb->context = hardware specific controller state structure
1509 */
David Howells7d12e782006-10-05 14:55:46 +01001510static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001511{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001512 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001513 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001514 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001515 unsigned long flags;
1516
Tilman Schmidt06163f82006-06-26 00:25:33 -07001517 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001518 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001519 case 0: /* normal completion */
1520 spin_lock_irqsave(&ucs->lock, flags);
1521 switch (ucs->pending) {
1522 case HD_DEVICE_INIT_ACK: /* no reply expected */
1523 del_timer(&ucs->timer_ctrl);
1524 ucs->pending = 0;
1525 break;
1526 }
1527 spin_unlock_irqrestore(&ucs->lock, flags);
1528 return;
1529
1530 case -ENOENT: /* cancelled */
1531 case -ECONNRESET: /* cancelled (async) */
1532 case -EINPROGRESS: /* pending */
1533 case -ENODEV: /* device removed */
1534 case -ESHUTDOWN: /* device shut down */
1535 /* ignore silently */
1536 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001537 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001538 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001539
1540 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001541 /* don't retry if suspend requested */
1542 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001543 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001544 dev_err(&ucs->interface->dev,
1545 "control request 0x%02x failed: %s\n",
1546 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001547 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001548 break; /* give up */
1549 }
1550 dev_notice(&ucs->interface->dev,
1551 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001552 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001553 ucs->retry_ctrl);
1554 /* urb->dev is clobbered by USB subsystem */
1555 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001556 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001557 if (unlikely(rc)) {
1558 dev_err(&ucs->interface->dev,
1559 "could not resubmit request 0x%02x: %s\n",
1560 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1561 break;
1562 }
1563 /* resubmitted */
1564 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001565 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001566
1567 /* failed, clear pending request */
1568 spin_lock_irqsave(&ucs->lock, flags);
1569 del_timer(&ucs->timer_ctrl);
1570 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001571 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001572 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001573}
1574
1575/* req_submit
1576 * submit a control output request without message buffer to the Gigaset base
1577 * and optionally start a timeout
1578 * parameters:
1579 * bcs B channel control structure
1580 * req control request code (HD_*)
1581 * val control request parameter value (set to 0 if unused)
1582 * timeout timeout in seconds (0: no timeout)
1583 * return value:
1584 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001585 * -EBUSY if another request is pending
1586 * any URB submission error code
1587 */
1588static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1589{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001590 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001591 int ret;
1592 unsigned long flags;
1593
Tilman Schmidt784d5852006-04-10 22:55:04 -07001594 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001595
1596 spin_lock_irqsave(&ucs->lock, flags);
1597 if (ucs->pending) {
1598 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001599 dev_err(bcs->cs->dev,
1600 "submission of request 0x%02x failed: "
1601 "request 0x%02x still pending\n",
1602 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001603 return -EBUSY;
1604 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001605
1606 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1607 ucs->dr_ctrl.bRequest = req;
1608 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1609 ucs->dr_ctrl.wIndex = 0;
1610 ucs->dr_ctrl.wLength = 0;
1611 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001612 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001613 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001614 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001615 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001616 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001617 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001618 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001619 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001620 spin_unlock_irqrestore(&ucs->lock, flags);
1621 return ret;
1622 }
1623 ucs->pending = req;
1624
1625 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001626 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001627 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001628 }
1629
1630 spin_unlock_irqrestore(&ucs->lock, flags);
1631 return 0;
1632}
1633
1634/* gigaset_init_bchannel
1635 * called by common.c to connect a B channel
1636 * initialize isochronous I/O and tell the Gigaset base to open the channel
1637 * argument:
1638 * B channel control structure
1639 * return value:
1640 * 0 on success, error code < 0 on error
1641 */
1642static int gigaset_init_bchannel(struct bc_state *bcs)
1643{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001644 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001645 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001646 unsigned long flags;
1647
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001648 spin_lock_irqsave(&cs->lock, flags);
1649 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001650 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001651 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001652 return -ENODEV;
1653 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001654
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001655 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001656 dev_notice(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001657 "not starting isoc I/O, suspend in progress\n");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001658 spin_unlock_irqrestore(&cs->lock, flags);
1659 return -EHOSTUNREACH;
1660 }
1661
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001662 ret = starturbs(bcs);
1663 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001664 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001665 dev_err(cs->dev,
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00001666 "could not start isoc I/O for channel B%d: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001667 bcs->channel + 1,
1668 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1669 if (ret != -ENODEV)
1670 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001671 return ret;
1672 }
1673
1674 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001675 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1676 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001677 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001678 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001679 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001680 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001681
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001682 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001683 if (ret < 0 && ret != -ENODEV)
1684 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685 return ret;
1686}
1687
1688/* gigaset_close_bchannel
1689 * called by common.c to disconnect a B channel
1690 * tell the Gigaset base to close the channel
1691 * stopping isochronous I/O and LL notification will be done when the
1692 * acknowledgement for the close arrives
1693 * argument:
1694 * B channel control structure
1695 * return value:
1696 * 0 on success, error code < 0 on error
1697 */
1698static int gigaset_close_bchannel(struct bc_state *bcs)
1699{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001700 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001701 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001702 unsigned long flags;
1703
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001704 spin_lock_irqsave(&cs->lock, flags);
1705 if (unlikely(!cs->connected)) {
1706 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001707 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1708 return -ENODEV;
1709 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001710
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001711 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001712 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001713 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001714 gigaset_bchannel_down(bcs);
1715 return 0;
1716 }
1717
Tilman Schmidt73a88812006-04-22 02:35:30 -07001718 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001719 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001720 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1721 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001722 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001723 bcs->channel + 1);
1724
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001725 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001726 return ret;
1727}
1728
1729/* Device Operations */
1730/* ================= */
1731
1732/* complete_cb
1733 * unqueue first command buffer from queue, waking any sleepers
1734 * must be called with cs->cmdlock held
1735 * parameter:
1736 * cs controller state structure
1737 */
1738static void complete_cb(struct cardstate *cs)
1739{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001740 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001741
1742 /* unqueue completed buffer */
1743 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001744 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001745 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001746 if (cb->next != NULL) {
1747 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001748 cs->cmdbuf->prev = NULL;
1749 cs->curlen = cs->cmdbuf->len;
1750 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001751 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001752 cs->lastcmdbuf = NULL;
1753 cs->curlen = 0;
1754 }
1755
1756 if (cb->wake_tasklet)
1757 tasklet_schedule(cb->wake_tasklet);
1758
1759 kfree(cb);
1760}
1761
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001762/* write_command_callback
1763 * USB completion handler for AT command transmission
1764 * called by the USB subsystem in interrupt context
1765 * parameter:
1766 * urb USB request block of completed request
1767 * urb->context = controller state structure
1768 */
David Howells7d12e782006-10-05 14:55:46 +01001769static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001770{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001771 struct cardstate *cs = urb->context;
1772 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001773 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001774 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001775
Tilman Schmidt73a88812006-04-22 02:35:30 -07001776 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001777 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001778
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001779 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001780 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001781 case 0: /* normal completion */
1782 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001783 case -ENOENT: /* cancelled */
1784 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001785 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001786 case -ENODEV: /* device removed */
1787 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001788 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001789 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001790 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001791 return;
1792 default: /* any failure */
1793 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001794 dev_warn(cs->dev,
1795 "command write: %s, "
1796 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001797 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001798 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799 break;
1800 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001801 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001802 dev_warn(cs->dev,
1803 "command write: %s, "
1804 "won't retry - suspend requested\n",
1805 get_usb_statmsg(status));
1806 break;
1807 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001808 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001809 dev_warn(cs->dev,
1810 "command write: %s, "
1811 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001812 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001813 break;
1814 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001815 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001816 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001817 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1818 /* resubmitted - bypass regular exit block */
1819 return;
1820 /* command send failed, assume base still waiting */
1821 update_basstate(ucs, BS_ATREADY, 0);
1822 }
1823
1824 spin_lock_irqsave(&cs->cmdlock, flags);
1825 if (cs->cmdbuf != NULL)
1826 complete_cb(cs);
1827 spin_unlock_irqrestore(&cs->cmdlock, flags);
1828}
1829
1830/* atrdy_timeout
1831 * timeout routine for AT command transmission
1832 * argument:
1833 * controller state structure
1834 */
1835static void atrdy_timeout(unsigned long data)
1836{
1837 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001838 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001839
Tilman Schmidt784d5852006-04-10 22:55:04 -07001840 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001841
1842 /* fake the missing signal - what else can I do? */
1843 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1844 start_cbsend(cs);
1845}
1846
1847/* atwrite_submit
1848 * submit an HD_WRITE_ATMESSAGE command URB
1849 * parameters:
1850 * cs controller state structure
1851 * buf buffer containing command to send
1852 * len length of command to send
1853 * return value:
1854 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001855 * -EBUSY if another request is pending
1856 * any URB submission error code
1857 */
1858static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1859{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001860 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001861 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001862
Tilman Schmidt784d5852006-04-10 22:55:04 -07001863 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001864
Tilman Schmidt73a88812006-04-22 02:35:30 -07001865 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001866 dev_err(cs->dev,
1867 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001868 return -EBUSY;
1869 }
1870
1871 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1872 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1873 ucs->dr_cmd_out.wValue = 0;
1874 ucs->dr_cmd_out.wIndex = 0;
1875 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1876 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1877 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001878 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001879 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001880 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001881 if (unlikely(rc)) {
1882 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001883 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001884 get_usb_rcmsg(rc));
1885 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001886 }
1887
Tilman Schmidt73a88812006-04-22 02:35:30 -07001888 /* submitted successfully, start timeout if necessary */
1889 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001890 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1891 ATRDY_TIMEOUT);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001892 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001893 }
1894 return 0;
1895}
1896
1897/* start_cbsend
1898 * start transmission of AT command queue if necessary
1899 * parameter:
1900 * cs controller state structure
1901 * return value:
1902 * 0 on success
1903 * error code < 0 on error
1904 */
1905static int start_cbsend(struct cardstate *cs)
1906{
1907 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001908 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001909 unsigned long flags;
1910 int rc;
1911 int retval = 0;
1912
Tilman Schmidt024fd292008-02-06 01:38:26 -08001913 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001914 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001915 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001916 return -EHOSTUNREACH;
1917 }
1918
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001919 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001920 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001921 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001922 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1923 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001924 /* flush command queue */
1925 spin_lock_irqsave(&cs->cmdlock, flags);
1926 while (cs->cmdbuf != NULL)
1927 complete_cb(cs);
1928 spin_unlock_irqrestore(&cs->cmdlock, flags);
1929 }
1930 return rc;
1931 }
1932
1933 /* try to send first command in queue */
1934 spin_lock_irqsave(&cs->cmdlock, flags);
1935
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001936 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001937 ucs->retry_cmd_out = 0;
1938 rc = atwrite_submit(cs, cb->buf, cb->len);
1939 if (unlikely(rc)) {
1940 retval = rc;
1941 complete_cb(cs);
1942 }
1943 }
1944
1945 spin_unlock_irqrestore(&cs->cmdlock, flags);
1946 return retval;
1947}
1948
1949/* gigaset_write_cmd
1950 * This function is called by the device independent part of the driver
1951 * to transmit an AT command string to the Gigaset device.
1952 * It encapsulates the device specific method for transmission over the
1953 * direct USB connection to the base.
1954 * The command string is added to the queue of commands to send, and
1955 * USB transmission is started if necessary.
1956 * parameters:
1957 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001958 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001959 * return value:
1960 * number of bytes queued on success
1961 * error code < 0 on error
1962 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001963static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001964{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001965 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001966 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001967
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001968 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Joe Perches475be4d2012-02-19 19:52:38 -08001969 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001970 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001971
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001972 /* translate "+++" escape sequence sent as a single separate command
1973 * into "close AT channel" command for error recovery
1974 * The next command will reopen the AT channel automatically.
1975 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001976 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidt4cb5e422010-09-30 13:35:31 +00001977 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1978 * because of an error, the base never sends another one.
1979 * The response channel is thus effectively blocked.
1980 * Closing and reopening the AT channel does *not* clear
1981 * this condition.
1982 * As a stopgap measure, submit a zero-length AT read
1983 * before closing the AT channel. This has the undocumented
1984 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1985 * from the base if necessary.
1986 * The subsequent AT channel close then discards any pending
1987 * messages.
1988 */
1989 spin_lock_irqsave(&cs->lock, flags);
1990 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1991 kfree(cs->hw.bas->rcvbuf);
1992 cs->hw.bas->rcvbuf = NULL;
1993 cs->hw.bas->rcvbuf_size = 0;
1994 cs->hw.bas->retry_cmd_in = 0;
1995 atread_submit(cs, 0);
1996 }
1997 spin_unlock_irqrestore(&cs->lock, flags);
1998
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001999 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002000 if (cb->wake_tasklet)
2001 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00002002 if (!rc)
2003 rc = cb->len;
2004 kfree(cb);
2005 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00002006 }
2007
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002008 spin_lock_irqsave(&cs->cmdlock, flags);
2009 cb->prev = cs->lastcmdbuf;
2010 if (cs->lastcmdbuf)
2011 cs->lastcmdbuf->next = cb;
2012 else {
2013 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002014 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002015 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002016 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002017 cs->lastcmdbuf = cb;
2018 spin_unlock_irqrestore(&cs->cmdlock, flags);
2019
Tilman Schmidt73a88812006-04-22 02:35:30 -07002020 spin_lock_irqsave(&cs->lock, flags);
2021 if (unlikely(!cs->connected)) {
2022 spin_unlock_irqrestore(&cs->lock, flags);
2023 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002024 /* flush command queue */
2025 spin_lock_irqsave(&cs->cmdlock, flags);
2026 while (cs->cmdbuf != NULL)
2027 complete_cb(cs);
2028 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002029 return -ENODEV;
2030 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002031 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002032 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00002033 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002034}
2035
2036/* gigaset_write_room
2037 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07002038 * return number of characters the driver will accept to be written via
2039 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002040 * parameter:
2041 * controller state structure
2042 * return value:
2043 * number of characters
2044 */
2045static int gigaset_write_room(struct cardstate *cs)
2046{
2047 return IF_WRITEBUF;
2048}
2049
2050/* gigaset_chars_in_buffer
2051 * tty_driver.chars_in_buffer interface routine
2052 * return number of characters waiting to be sent
2053 * parameter:
2054 * controller state structure
2055 * return value:
2056 * number of characters
2057 */
2058static int gigaset_chars_in_buffer(struct cardstate *cs)
2059{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002060 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002061}
2062
2063/* gigaset_brkchars
2064 * implementation of ioctl(GIGASET_BRKCHARS)
2065 * parameter:
2066 * controller state structure
2067 * return value:
2068 * -EINVAL (unimplemented function)
2069 */
2070static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2071{
2072 return -EINVAL;
2073}
2074
2075
2076/* Device Initialization/Shutdown */
2077/* ============================== */
2078
2079/* Free hardware dependent part of the B channel structure
2080 * parameter:
2081 * bcs B channel structure
2082 * return value:
2083 * !=0 on success
2084 */
2085static int gigaset_freebcshw(struct bc_state *bcs)
2086{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002087 struct bas_bc_state *ubc = bcs->hw.bas;
2088 int i;
2089
2090 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002091 return 0;
2092
Tilman Schmidt73a88812006-04-22 02:35:30 -07002093 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002094 ubc->running = 0;
Tilman Schmidtbb7196d2010-09-30 13:35:52 +00002095 gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002096 for (i = 0; i < BAS_OUTURBS; ++i) {
2097 usb_kill_urb(ubc->isoouturbs[i].urb);
2098 usb_free_urb(ubc->isoouturbs[i].urb);
2099 }
2100 for (i = 0; i < BAS_INURBS; ++i) {
2101 usb_kill_urb(ubc->isoinurbs[i]);
2102 usb_free_urb(ubc->isoinurbs[i]);
2103 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002104 tasklet_kill(&ubc->sent_tasklet);
2105 tasklet_kill(&ubc->rcvd_tasklet);
2106 kfree(ubc->isooutbuf);
2107 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002108 bcs->hw.bas = NULL;
2109 return 1;
2110}
2111
2112/* Initialize hardware dependent part of the B channel structure
2113 * parameter:
2114 * bcs B channel structure
2115 * return value:
2116 * !=0 on success
2117 */
2118static int gigaset_initbcshw(struct bc_state *bcs)
2119{
2120 int i;
2121 struct bas_bc_state *ubc;
2122
2123 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2124 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002125 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002126 return 0;
2127 }
2128
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002129 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002130 atomic_set(&ubc->corrbytes, 0);
2131 spin_lock_init(&ubc->isooutlock);
2132 for (i = 0; i < BAS_OUTURBS; ++i) {
2133 ubc->isoouturbs[i].urb = NULL;
2134 ubc->isoouturbs[i].bcs = bcs;
2135 }
2136 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2137 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002138 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2139 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002140 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002141 kfree(ubc);
2142 bcs->hw.bas = NULL;
2143 return 0;
2144 }
2145 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002146 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002147
2148 spin_lock_init(&ubc->isoinlock);
2149 for (i = 0; i < BAS_INURBS; ++i)
2150 ubc->isoinurbs[i] = NULL;
2151 ubc->isoindone = NULL;
2152 ubc->loststatus = -EINPROGRESS;
2153 ubc->isoinlost = 0;
2154 ubc->seqlen = 0;
2155 ubc->inbyte = 0;
2156 ubc->inbits = 0;
2157 ubc->goodbytes = 0;
2158 ubc->alignerrs = 0;
2159 ubc->fcserrs = 0;
2160 ubc->frameerrs = 0;
2161 ubc->giants = 0;
2162 ubc->runts = 0;
2163 ubc->aborts = 0;
2164 ubc->shared0s = 0;
2165 ubc->stolen0s = 0;
2166 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002167 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002168 return 1;
2169}
2170
2171static void gigaset_reinitbcshw(struct bc_state *bcs)
2172{
2173 struct bas_bc_state *ubc = bcs->hw.bas;
2174
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002175 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002176 atomic_set(&bcs->hw.bas->corrbytes, 0);
2177 bcs->hw.bas->numsub = 0;
2178 spin_lock_init(&ubc->isooutlock);
2179 spin_lock_init(&ubc->isoinlock);
2180 ubc->loststatus = -EINPROGRESS;
2181}
2182
2183static void gigaset_freecshw(struct cardstate *cs)
2184{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002185 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002186 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002187 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002188 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002189}
2190
2191static int gigaset_initcshw(struct cardstate *cs)
2192{
2193 struct bas_cardstate *ucs;
2194
2195 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002196 if (!ucs) {
2197 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002198 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002199 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002200 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2201 if (!ucs->int_in_buf) {
2202 kfree(ucs);
2203 pr_err("out of memory\n");
2204 return 0;
2205 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206
2207 ucs->urb_cmd_in = NULL;
2208 ucs->urb_cmd_out = NULL;
2209 ucs->rcvbuf = NULL;
2210 ucs->rcvbuf_size = 0;
2211
2212 spin_lock_init(&ucs->lock);
2213 ucs->pending = 0;
2214
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002215 ucs->basstate = 0;
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002216 setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
2217 setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
2218 setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002219 setup_timer(&ucs->timer_int_in, int_in_resubmit, (unsigned long) cs);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002220 init_waitqueue_head(&ucs->waitqueue);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002221 INIT_WORK(&ucs->int_in_wq, int_in_work);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002222
2223 return 1;
2224}
2225
2226/* freeurbs
2227 * unlink and deallocate all URBs unconditionally
2228 * caller must make sure that no commands are still in progress
2229 * parameter:
2230 * cs controller state structure
2231 */
2232static void freeurbs(struct cardstate *cs)
2233{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002234 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002235 struct bas_bc_state *ubc;
2236 int i, j;
2237
Tilman Schmidt39f07222006-12-13 00:33:52 -08002238 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002239 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002240 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002241 for (i = 0; i < BAS_OUTURBS; ++i) {
2242 usb_kill_urb(ubc->isoouturbs[i].urb);
2243 usb_free_urb(ubc->isoouturbs[i].urb);
2244 ubc->isoouturbs[i].urb = NULL;
2245 }
2246 for (i = 0; i < BAS_INURBS; ++i) {
2247 usb_kill_urb(ubc->isoinurbs[i]);
2248 usb_free_urb(ubc->isoinurbs[i]);
2249 ubc->isoinurbs[i] = NULL;
2250 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002251 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002252 usb_kill_urb(ucs->urb_int_in);
2253 usb_free_urb(ucs->urb_int_in);
2254 ucs->urb_int_in = NULL;
2255 usb_kill_urb(ucs->urb_cmd_out);
2256 usb_free_urb(ucs->urb_cmd_out);
2257 ucs->urb_cmd_out = NULL;
2258 usb_kill_urb(ucs->urb_cmd_in);
2259 usb_free_urb(ucs->urb_cmd_in);
2260 ucs->urb_cmd_in = NULL;
2261 usb_kill_urb(ucs->urb_ctrl);
2262 usb_free_urb(ucs->urb_ctrl);
2263 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002264}
2265
2266/* gigaset_probe
2267 * This function is called when a new USB device is connected.
2268 * It checks whether the new device is handled by this driver.
2269 */
2270static int gigaset_probe(struct usb_interface *interface,
2271 const struct usb_device_id *id)
2272{
2273 struct usb_host_interface *hostif;
2274 struct usb_device *udev = interface_to_usbdev(interface);
2275 struct cardstate *cs = NULL;
2276 struct bas_cardstate *ucs = NULL;
2277 struct bas_bc_state *ubc;
2278 struct usb_endpoint_descriptor *endpoint;
2279 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002280 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002281
Tilman Schmidt1528b182010-02-22 13:09:52 +00002282 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002283 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2284 __func__, le16_to_cpu(udev->descriptor.idVendor),
2285 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002286
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002287 /* set required alternate setting */
2288 hostif = interface->cur_altsetting;
2289 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002290 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002291 "%s: wrong alternate setting %d - trying to switch",
2292 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002293 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2294 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002295 dev_warn(&udev->dev, "usb_set_interface failed, "
2296 "device %d interface %d altsetting %d\n",
2297 udev->devnum, hostif->desc.bInterfaceNumber,
2298 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002299 return -ENODEV;
2300 }
2301 hostif = interface->cur_altsetting;
2302 }
2303
2304 /* Reject application specific interfaces
2305 */
2306 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002307 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2308 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002309 return -ENODEV;
2310 }
2311
Tilman Schmidt784d5852006-04-10 22:55:04 -07002312 dev_info(&udev->dev,
2313 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2314 __func__, le16_to_cpu(udev->descriptor.idVendor),
2315 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002316
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002317 /* allocate memory for our device state and initialize it */
Tilman Schmidte468c042008-02-06 01:38:29 -08002318 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2319 GIGASET_MODULENAME);
2320 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002321 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002322 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002323
2324 /* save off device structure ptrs for later use */
2325 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002326 ucs->udev = udev;
2327 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002328 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002329
2330 /* allocate URBs:
2331 * - one for the interrupt pipe
2332 * - three for the different uses of the default control pipe
2333 * - three for each isochronous pipe
2334 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002335 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2336 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2337 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2338 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002339 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002340
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002341 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002342 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002343 for (i = 0; i < BAS_OUTURBS; ++i)
2344 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002345 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002346 goto allocerr;
2347 for (i = 0; i < BAS_INURBS; ++i)
2348 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002349 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002350 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002351 }
2352
2353 ucs->rcvbuf = NULL;
2354 ucs->rcvbuf_size = 0;
2355
2356 /* Fill the interrupt urb and send it to the core */
2357 endpoint = &hostif->endpoint[0].desc;
2358 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002359 usb_rcvintpipe(udev,
2360 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002361 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002362 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002363 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2364 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002365 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002366 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002367 goto error;
2368 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002369 ucs->retry_int_in = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002370
2371 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002372 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2373 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002374 goto error;
2375
2376 /* tell common part that the device is ready */
2377 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002378 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002379
2380 /* save address of controller structure */
2381 usb_set_intfdata(interface, cs);
2382
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002383 if (!gigaset_start(cs))
2384 goto error;
2385
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002386 return 0;
2387
Tilman Schmidt73a88812006-04-22 02:35:30 -07002388allocerr:
2389 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002390error:
2391 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002392 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002393 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002394 return -ENODEV;
2395}
2396
2397/* gigaset_disconnect
2398 * This function is called when the Gigaset base is unplugged.
2399 */
2400static void gigaset_disconnect(struct usb_interface *interface)
2401{
2402 struct cardstate *cs;
2403 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002404 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002405
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002406 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002407
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002408 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002409
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002410 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002411
2412 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002413 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002414
2415 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002416 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002417 gigaset_bchannel_down(cs->bcs + j);
2418
2419 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002420 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002421
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002422 /* stop delayed work and URBs, free ressources */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002423 del_timer_sync(&ucs->timer_ctrl);
2424 del_timer_sync(&ucs->timer_atrdy);
2425 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002426 del_timer_sync(&ucs->timer_int_in);
2427 cancel_work_sync(&ucs->int_in_wq);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002428 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002429 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002430 kfree(ucs->rcvbuf);
2431 ucs->rcvbuf = NULL;
2432 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002433 usb_put_dev(ucs->udev);
2434 ucs->interface = NULL;
2435 ucs->udev = NULL;
2436 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002437 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002438}
2439
Tilman Schmidt024fd292008-02-06 01:38:26 -08002440/* gigaset_suspend
2441 * This function is called before the USB connection is suspended.
2442 */
2443static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2444{
2445 struct cardstate *cs = usb_get_intfdata(intf);
2446 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002447 int rc;
2448
2449 /* set suspend flag; this stops AT command/response traffic */
2450 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2451 gig_dbg(DEBUG_SUSPEND, "already suspended");
2452 return 0;
2453 }
2454
2455 /* wait a bit for blocking conditions to go away */
2456 rc = wait_event_timeout(ucs->waitqueue,
Joe Perches475be4d2012-02-19 19:52:38 -08002457 !(ucs->basstate &
2458 (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)),
2459 BAS_TIMEOUT * HZ / 10);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002460 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2461
2462 /* check for conditions preventing suspend */
Joe Perches475be4d2012-02-19 19:52:38 -08002463 if (ucs->basstate & (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002464 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002465 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002466 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002467 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002468 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002469 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002470 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002471 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002472 dev_warn(cs->dev, " sending AT command\n");
2473 update_basstate(ucs, 0, BS_SUSPEND);
2474 return -EBUSY;
2475 }
2476
2477 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002478 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002479 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2480 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2481 if (rc) {
2482 update_basstate(ucs, 0, BS_SUSPEND);
2483 return rc;
2484 }
2485 wait_event_timeout(ucs->waitqueue, !ucs->pending,
Joe Perches475be4d2012-02-19 19:52:38 -08002486 BAS_TIMEOUT * HZ / 10);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002487 /* in case of timeout, proceed anyway */
2488 }
2489
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002490 /* kill all URBs and delayed work that might still be pending */
Tilman Schmidt024fd292008-02-06 01:38:26 -08002491 usb_kill_urb(ucs->urb_ctrl);
2492 usb_kill_urb(ucs->urb_int_in);
2493 del_timer_sync(&ucs->timer_ctrl);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002494 del_timer_sync(&ucs->timer_atrdy);
2495 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002496 del_timer_sync(&ucs->timer_int_in);
2497 cancel_work_sync(&ucs->int_in_wq);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002498
2499 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2500 return 0;
2501}
2502
2503/* gigaset_resume
2504 * This function is called after the USB connection has been resumed.
2505 */
2506static int gigaset_resume(struct usb_interface *intf)
2507{
2508 struct cardstate *cs = usb_get_intfdata(intf);
2509 struct bas_cardstate *ucs = cs->hw.bas;
2510 int rc;
2511
2512 /* resubmit interrupt URB for spontaneous messages from base */
2513 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2514 if (rc) {
2515 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2516 get_usb_rcmsg(rc));
2517 return rc;
2518 }
Tilman Schmidtc9c0c302010-09-30 13:35:42 +00002519 ucs->retry_int_in = 0;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002520
2521 /* clear suspend flag to reallow activity */
2522 update_basstate(ucs, 0, BS_SUSPEND);
2523
2524 gig_dbg(DEBUG_SUSPEND, "resume complete");
2525 return 0;
2526}
2527
2528/* gigaset_pre_reset
2529 * This function is called before the USB connection is reset.
2530 */
2531static int gigaset_pre_reset(struct usb_interface *intf)
2532{
2533 /* handle just like suspend */
2534 return gigaset_suspend(intf, PMSG_ON);
2535}
2536
2537/* gigaset_post_reset
2538 * This function is called after the USB connection has been reset.
2539 */
2540static int gigaset_post_reset(struct usb_interface *intf)
2541{
2542 /* FIXME: send HD_DEVICE_INIT_ACK? */
2543
2544 /* resume operations */
2545 return gigaset_resume(intf);
2546}
2547
2548
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002549static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002550 gigaset_write_cmd,
2551 gigaset_write_room,
2552 gigaset_chars_in_buffer,
2553 gigaset_brkchars,
2554 gigaset_init_bchannel,
2555 gigaset_close_bchannel,
2556 gigaset_initbcshw,
2557 gigaset_freebcshw,
2558 gigaset_reinitbcshw,
2559 gigaset_initcshw,
2560 gigaset_freecshw,
2561 gigaset_set_modem_ctrl,
2562 gigaset_baud_rate,
2563 gigaset_set_line_ctrl,
2564 gigaset_isoc_send_skb,
2565 gigaset_isoc_input,
2566};
2567
2568/* bas_gigaset_init
2569 * This function is called after the kernel module is loaded.
2570 */
2571static int __init bas_gigaset_init(void)
2572{
2573 int result;
2574
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002575 /* allocate memory for our driver state and initialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002576 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2577 GIGASET_MODULENAME, GIGASET_DEVNAME,
2578 &gigops, THIS_MODULE);
2579 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002580 goto error;
2581
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002582 /* register this driver with the USB subsystem */
2583 result = usb_register(&gigaset_usb_driver);
2584 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002585 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002586 goto error;
2587 }
2588
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002589 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002590 return 0;
2591
Tilman Schmidte468c042008-02-06 01:38:29 -08002592error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002593 if (driver)
2594 gigaset_freedriver(driver);
2595 driver = NULL;
2596 return -1;
2597}
2598
2599/* bas_gigaset_exit
2600 * This function is called before the kernel module is unloaded.
2601 */
2602static void __exit bas_gigaset_exit(void)
2603{
Tilman Schmidte468c042008-02-06 01:38:29 -08002604 struct bas_cardstate *ucs;
2605 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002606
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002607 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002608 * => no gigaset_start any more
2609 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002610
Tilman Schmidte468c042008-02-06 01:38:29 -08002611 /* stop all connected devices */
2612 for (i = 0; i < driver->minors; i++) {
2613 if (gigaset_shutdown(driver->cs + i) < 0)
2614 continue; /* no device */
2615 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002616
Tilman Schmidte468c042008-02-06 01:38:29 -08002617 /* close all still open channels */
2618 ucs = driver->cs[i].hw.bas;
2619 if (ucs->basstate & BS_B1OPEN) {
2620 gig_dbg(DEBUG_INIT, "closing B1 channel");
2621 usb_control_msg(ucs->udev,
2622 usb_sndctrlpipe(ucs->udev, 0),
2623 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2624 0, 0, NULL, 0, BAS_TIMEOUT);
2625 }
2626 if (ucs->basstate & BS_B2OPEN) {
2627 gig_dbg(DEBUG_INIT, "closing B2 channel");
2628 usb_control_msg(ucs->udev,
2629 usb_sndctrlpipe(ucs->udev, 0),
2630 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2631 0, 0, NULL, 0, BAS_TIMEOUT);
2632 }
2633 if (ucs->basstate & BS_ATOPEN) {
2634 gig_dbg(DEBUG_INIT, "closing AT channel");
2635 usb_control_msg(ucs->udev,
2636 usb_sndctrlpipe(ucs->udev, 0),
2637 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2638 0, 0, NULL, 0, BAS_TIMEOUT);
2639 }
2640 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002641 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002642
2643 /* deregister this driver with the USB subsystem */
2644 usb_deregister(&gigaset_usb_driver);
2645 /* this will call the disconnect-callback */
2646 /* from now on, no disconnect/probe callback should be running */
2647
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002648 gigaset_freedriver(driver);
2649 driver = NULL;
2650}
2651
2652
2653module_init(bas_gigaset_init);
2654module_exit(bas_gigaset_exit);
2655
2656MODULE_AUTHOR(DRIVER_AUTHOR);
2657MODULE_DESCRIPTION(DRIVER_DESC);
2658MODULE_LICENSE("GPL");