blob: 1c401b3f88e19e3ae6db9d2320ee0284f38ee9bd [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"
17
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/timer.h>
22#include <linux/usb.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25
26/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070027#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080028#define DRIVER_DESC "USB Driver for Gigaset 307x"
29
30
31/* Module parameters */
32
33static int startmode = SM_ISDN;
34static int cidmode = 1;
35
36module_param(startmode, int, S_IRUGO);
37module_param(cidmode, int, S_IRUGO);
38MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41#define GIGASET_MINORS 1
42#define GIGASET_MINOR 16
43#define GIGASET_MODULENAME "bas_gigaset"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080044#define GIGASET_DEVNAME "ttyGB"
45
Tilman Schmidt73a88812006-04-22 02:35:30 -070046/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080048
49/* Values for the Gigaset 307x */
50#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070051#define USB_3070_PRODUCT_ID 0x0001
52#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080053#define USB_SX303_PRODUCT_ID 0x0021
54#define USB_SX353_PRODUCT_ID 0x0022
55
56/* table of devices that work with this driver */
Tilman Schmidt35dc8452007-03-29 01:20:34 -070057static const struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080060 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
62 { } /* Terminating entry */
63};
64
65MODULE_DEVICE_TABLE(usb, gigaset_table);
66
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068
Tilman Schmidt06163f82006-06-26 00:25:33 -070069/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080070static int gigaset_probe(struct usb_interface *interface,
71 const struct usb_device_id *id);
72
73/* Function will be called if the device is unplugged */
74static void gigaset_disconnect(struct usb_interface *interface);
75
Tilman Schmidt024fd292008-02-06 01:38:26 -080076/* functions called before/after suspend */
77static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
78static int gigaset_resume(struct usb_interface *intf);
79
80/* functions called before/after device reset */
81static int gigaset_pre_reset(struct usb_interface *intf);
82static int gigaset_post_reset(struct usb_interface *intf);
83
Tilman Schmidt06163f82006-06-26 00:25:33 -070084static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070085static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070086static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070087static int atwrite_submit(struct cardstate *, unsigned char *, int);
88static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089
Tilman Schmidt06163f82006-06-26 00:25:33 -070090/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080091
92struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070093 struct usb_device *udev; /* USB device pointer */
94 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080095 unsigned char minor; /* starting minor number */
96
Tilman Schmidt784d5852006-04-10 22:55:04 -070097 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080098 struct usb_ctrlrequest dr_ctrl;
99 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700100 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101
102 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700103 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800104 struct usb_ctrlrequest dr_cmd_out;
105 int retry_cmd_out;
106
Tilman Schmidt784d5852006-04-10 22:55:04 -0700107 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800108 struct usb_ctrlrequest dr_cmd_in;
109 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800111
Tilman Schmidt784d5852006-04-10 22:55:04 -0700112 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800113 unsigned char int_in_buf[3];
114
115 spinlock_t lock; /* locks all following */
116 atomic_t basstate; /* bitmap (BS_*) */
117 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800118 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800119 int rcvbuf_size; /* size of AT receive buffer */
120 /* 0: no receive in progress */
121 int retry_cmd_in; /* receive req retry count */
122};
123
124/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700125#define BS_ATOPEN 0x001 /* AT channel open */
126#define BS_B1OPEN 0x002 /* B channel 1 open */
127#define BS_B2OPEN 0x004 /* B channel 2 open */
128#define BS_ATREADY 0x008 /* base ready for AT command */
129#define BS_INIT 0x010 /* base has signalled INIT_OK */
130#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
131#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
132#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800133#define BS_SUSPEND 0x100 /* USB port suspended */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800134
135
136static struct gigaset_driver *driver = NULL;
137static struct cardstate *cardstate = NULL;
138
139/* usb specific object needed to register this driver with the usb subsystem */
140static struct usb_driver gigaset_usb_driver = {
141 .name = GIGASET_MODULENAME,
142 .probe = gigaset_probe,
143 .disconnect = gigaset_disconnect,
144 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800145 .suspend = gigaset_suspend,
146 .resume = gigaset_resume,
147 .reset_resume = gigaset_post_reset,
148 .pre_reset = gigaset_pre_reset,
149 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800150};
151
Tilman Schmidt73a88812006-04-22 02:35:30 -0700152/* get message text for usb_submit_urb return code
153 */
154static char *get_usb_rcmsg(int rc)
155{
156 static char unkmsg[28];
157
158 switch (rc) {
159 case 0:
160 return "success";
161 case -ENOMEM:
162 return "out of memory";
163 case -ENODEV:
164 return "device not present";
165 case -ENOENT:
166 return "endpoint not present";
167 case -ENXIO:
168 return "URB type not supported";
169 case -EINVAL:
170 return "invalid argument";
171 case -EAGAIN:
172 return "start frame too early or too much scheduled";
173 case -EFBIG:
174 return "too many isochronous frames requested";
175 case -EPIPE:
176 return "endpoint stalled";
177 case -EMSGSIZE:
178 return "invalid packet size";
179 case -ENOSPC:
180 return "would overcommit USB bandwidth";
181 case -ESHUTDOWN:
182 return "device shut down";
183 case -EPERM:
184 return "reject flag set";
185 case -EHOSTUNREACH:
186 return "device suspended";
187 default:
188 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
189 return unkmsg;
190 }
191}
192
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800193/* get message text for USB status code
194 */
195static char *get_usb_statmsg(int status)
196{
197 static char unkmsg[28];
198
199 switch (status) {
200 case 0:
201 return "success";
202 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700203 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800204 case -EINPROGRESS:
205 return "pending";
206 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800208 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700209 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700210 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800211 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700212 case -EPIPE:
213 return "endpoint stalled";
214 case -ECOMM:
215 return "IN buffer overrun";
216 case -ENOSR:
217 return "OUT buffer underrun";
218 case -EOVERFLOW:
219 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800220 case -EREMOTEIO:
221 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700222 case -ENODEV:
223 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800224 case -EXDEV:
225 return "partial isochronous transfer";
226 case -EINVAL:
227 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700228 case -ECONNRESET:
229 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800230 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700233 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800234 return unkmsg;
235 }
236}
237
238/* usb_pipetype_str
239 * retrieve string representation of USB pipe type
240 */
241static inline char *usb_pipetype_str(int pipe)
242{
243 if (usb_pipeisoc(pipe))
244 return "Isoc";
245 if (usb_pipeint(pipe))
246 return "Int";
247 if (usb_pipecontrol(pipe))
248 return "Ctrl";
249 if (usb_pipebulk(pipe))
250 return "Bulk";
251 return "?";
252}
253
254/* dump_urb
255 * write content of URB to syslog for debugging
256 */
257static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700258 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800259{
260#ifdef CONFIG_GIGASET_DEBUG
261 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800263 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700264 gig_dbg(level,
265 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800266 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700267 (unsigned long) urb->dev,
268 usb_pipetype_str(urb->pipe),
269 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
270 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800271 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700272 urb->transfer_flags);
273 gig_dbg(level,
274 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800275 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700276 (unsigned long) urb->transfer_buffer,
277 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800278 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700279 gig_dbg(level,
280 " start_frame=%d, number_of_packets=%d, interval=%d, "
281 "error_count=%d,",
282 urb->start_frame, urb->number_of_packets, urb->interval,
283 urb->error_count);
284 gig_dbg(level,
285 " context=0x%08lx, complete=0x%08lx, "
286 "iso_frame_desc[]={",
287 (unsigned long) urb->context,
288 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800289 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700290 struct usb_iso_packet_descriptor *pifd
291 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700292 gig_dbg(level,
293 " {offset=%u, length=%u, actual_length=%u, "
294 "status=%u}",
295 pifd->offset, pifd->length, pifd->actual_length,
296 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800297 }
298 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700299 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800300#endif
301}
302
303/* read/set modem control bits etc. (m10x only) */
304static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700305 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800306{
307 return -EINVAL;
308}
309
310static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
311{
312 return -EINVAL;
313}
314
315static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
316{
317 return -EINVAL;
318}
319
320/* error_hangup
321 * hang up any existing connection because of an unrecoverable error
322 * This function may be called from any context and takes care of scheduling
323 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700324 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800325 * argument:
326 * B channel control structure
327 */
328static inline void error_hangup(struct bc_state *bcs)
329{
330 struct cardstate *cs = bcs->cs;
331
Tilman Schmidt784d5852006-04-10 22:55:04 -0700332 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
333 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800334
Tilman Schmidt73a88812006-04-22 02:35:30 -0700335 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
336 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800337
338 gigaset_schedule_event(cs);
339}
340
341/* error_reset
342 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700343 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700344 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700345 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800346 * argument:
347 * controller state structure
348 */
349static inline void error_reset(struct cardstate *cs)
350{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700351 /* close AT command channel to recover (ignore errors) */
352 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
353
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800354 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700355 dev_err(cs->dev,
356 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800357}
358
359/* check_pending
360 * check for completion of pending control request
361 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700362 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800363 */
364static void check_pending(struct bas_cardstate *ucs)
365{
366 unsigned long flags;
367
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800368 spin_lock_irqsave(&ucs->lock, flags);
369 switch (ucs->pending) {
370 case 0:
371 break;
372 case HD_OPEN_ATCHANNEL:
373 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
374 ucs->pending = 0;
375 break;
376 case HD_OPEN_B1CHANNEL:
377 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
378 ucs->pending = 0;
379 break;
380 case HD_OPEN_B2CHANNEL:
381 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
382 ucs->pending = 0;
383 break;
384 case HD_CLOSE_ATCHANNEL:
385 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
386 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800387 break;
388 case HD_CLOSE_B1CHANNEL:
389 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
390 ucs->pending = 0;
391 break;
392 case HD_CLOSE_B2CHANNEL:
393 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
394 ucs->pending = 0;
395 break;
396 case HD_DEVICE_INIT_ACK: /* no reply expected */
397 ucs->pending = 0;
398 break;
399 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
400 * are handled separately and should never end up here
401 */
402 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700403 dev_warn(&ucs->interface->dev,
404 "unknown pending request 0x%02x cleared\n",
405 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800406 ucs->pending = 0;
407 }
408
409 if (!ucs->pending)
410 del_timer(&ucs->timer_ctrl);
411
412 spin_unlock_irqrestore(&ucs->lock, flags);
413}
414
415/* cmd_in_timeout
416 * timeout routine for command input request
417 * argument:
418 * controller state structure
419 */
420static void cmd_in_timeout(unsigned long data)
421{
422 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700423 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700424 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800425
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800426 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700427 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800428 return;
429 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800430
Tilman Schmidt06163f82006-06-26 00:25:33 -0700431 if (ucs->retry_cmd_in++ < BAS_RETRY) {
432 dev_notice(cs->dev, "control read: timeout, retry %d\n",
433 ucs->retry_cmd_in);
434 rc = atread_submit(cs, BAS_TIMEOUT);
435 if (rc >= 0 || rc == -ENODEV)
436 /* resubmitted or disconnected */
437 /* - bypass regular exit block */
438 return;
439 } else {
440 dev_err(cs->dev,
441 "control read: timeout, giving up after %d tries\n",
442 ucs->retry_cmd_in);
443 }
444 kfree(ucs->rcvbuf);
445 ucs->rcvbuf = NULL;
446 ucs->rcvbuf_size = 0;
447 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800448}
449
Tilman Schmidt73a88812006-04-22 02:35:30 -0700450/* set/clear bits in base connection state, return previous state
451 */
452inline static int update_basstate(struct bas_cardstate *ucs,
453 int set, int clear)
454{
455 unsigned long flags;
456 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800457
Tilman Schmidt73a88812006-04-22 02:35:30 -0700458 spin_lock_irqsave(&ucs->lock, flags);
459 state = atomic_read(&ucs->basstate);
460 atomic_set(&ucs->basstate, (state & ~clear) | set);
461 spin_unlock_irqrestore(&ucs->lock, flags);
462 return state;
463}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800464
Tilman Schmidt06163f82006-06-26 00:25:33 -0700465/* read_ctrl_callback
466 * USB completion handler for control pipe input
467 * called by the USB subsystem in interrupt context
468 * parameter:
469 * urb USB request block
470 * urb->context = inbuf structure for controller state
471 */
David Howells7d12e782006-10-05 14:55:46 +0100472static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700473{
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800477 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700478 int have_data = 0;
479 unsigned numbytes;
480 int rc;
481
482 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800483 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700484
485 if (!ucs->rcvbuf_size) {
486 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
487 return;
488 }
489
490 del_timer(&ucs->timer_cmd_in);
491
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800492 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700493 case 0: /* normal completion */
494 numbytes = urb->actual_length;
495 if (unlikely(numbytes != ucs->rcvbuf_size)) {
496 dev_warn(cs->dev,
497 "control read: received %d chars, expected %d\n",
498 numbytes, ucs->rcvbuf_size);
499 if (numbytes > ucs->rcvbuf_size)
500 numbytes = ucs->rcvbuf_size;
501 }
502
503 /* copy received bytes to inbuf */
504 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
505
506 if (unlikely(numbytes < ucs->rcvbuf_size)) {
507 /* incomplete - resubmit for remaining bytes */
508 ucs->rcvbuf_size -= numbytes;
509 ucs->retry_cmd_in = 0;
510 rc = atread_submit(cs, BAS_TIMEOUT);
511 if (rc >= 0 || rc == -ENODEV)
512 /* resubmitted or disconnected */
513 /* - bypass regular exit block */
514 return;
515 error_reset(cs);
516 }
517 break;
518
519 case -ENOENT: /* cancelled */
520 case -ECONNRESET: /* cancelled (async) */
521 case -EINPROGRESS: /* pending */
522 case -ENODEV: /* device removed */
523 case -ESHUTDOWN: /* device shut down */
524 /* no action necessary */
525 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800526 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700527 break;
528
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800531 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 if (ucs->retry_cmd_in++ < BAS_RETRY) {
533 dev_notice(cs->dev, "control read: retry %d\n",
534 ucs->retry_cmd_in);
535 rc = atread_submit(cs, BAS_TIMEOUT);
536 if (rc >= 0 || rc == -ENODEV)
537 /* resubmitted or disconnected */
538 /* - bypass regular exit block */
539 return;
540 } else {
541 dev_err(cs->dev,
542 "control read: giving up after %d tries\n",
543 ucs->retry_cmd_in);
544 }
545 error_reset(cs);
546 }
547
548 kfree(ucs->rcvbuf);
549 ucs->rcvbuf = NULL;
550 ucs->rcvbuf_size = 0;
551 if (have_data) {
552 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
553 gigaset_schedule_event(cs);
554 }
555}
556
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800557/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559 * parameters:
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
562 * return value:
563 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 * -EBUSY if another request is pending
565 * any URB submission error code
566 */
567static int atread_submit(struct cardstate *cs, int timeout)
568{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700569 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800570 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800571 int ret;
572
Tilman Schmidt784d5852006-04-10 22:55:04 -0700573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
574 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575
Tilman Schmidt024fd292008-02-06 01:38:26 -0800576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_err(cs->dev,
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 return -EBUSY;
581 }
582
Tilman Schmidt024fd292008-02-06 01:38:26 -0800583 if (basstate & BS_SUSPEND) {
584 dev_notice(cs->dev,
585 "HD_READ_ATMESSAGE not submitted, "
586 "suspend in progress\n");
587 update_basstate(ucs, 0, BS_ATRDPEND);
588 /* treat like disconnect */
589 return -ENODEV;
590 }
591
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
593 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
594 ucs->dr_cmd_in.wValue = 0;
595 ucs->dr_cmd_in.wIndex = 0;
596 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
597 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 usb_rcvctrlpipe(ucs->udev, 0),
599 (unsigned char*) & ucs->dr_cmd_in,
600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800602
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800603 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700604 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700605 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700606 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607 return ret;
608 }
609
610 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700611 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800612 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
613 ucs->timer_cmd_in.data = (unsigned long) cs;
614 ucs->timer_cmd_in.function = cmd_in_timeout;
615 add_timer(&ucs->timer_cmd_in);
616 }
617 return 0;
618}
619
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800620/* read_int_callback
621 * USB completion handler for interrupt pipe input
622 * called by the USB subsystem in interrupt context
623 * parameter:
624 * urb USB request block
625 * urb->context = controller state structure
626 */
David Howells7d12e782006-10-05 14:55:46 +0100627static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800628{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700629 struct cardstate *cs = urb->context;
630 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800631 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800632 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700634 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800635 unsigned l;
636 int channel;
637
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800638 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800639 case 0: /* success */
640 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700641 case -ENOENT: /* cancelled */
642 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800643 case -EINPROGRESS: /* pending */
644 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700645 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800646 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800647 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700648 case -ENODEV: /* device removed */
649 case -ESHUTDOWN: /* device shut down */
650 //FIXME use this as disconnect indicator?
651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
652 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800653 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700654 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800655 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800656 //FIXME corrective action? resubmission always ok?
657 goto resubmit;
658 }
659
Tilman Schmidt73a88812006-04-22 02:35:30 -0700660 /* drop incomplete packets even if the missing bytes wouldn't matter */
661 if (unlikely(urb->actual_length < 3)) {
662 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
663 urb->actual_length);
664 goto resubmit;
665 }
666
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800667 l = (unsigned) ucs->int_in_buf[1] +
668 (((unsigned) ucs->int_in_buf[2]) << 8);
669
Tilman Schmidt784d5852006-04-10 22:55:04 -0700670 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
671 urb->actual_length, (int)ucs->int_in_buf[0], l,
672 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800673
674 channel = 0;
675
676 switch (ucs->int_in_buf[0]) {
677 case HD_DEVICE_INIT_OK:
678 update_basstate(ucs, BS_INIT, 0);
679 break;
680
681 case HD_READY_SEND_ATDATA:
682 del_timer(&ucs->timer_atrdy);
683 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
684 start_cbsend(cs);
685 break;
686
687 case HD_OPEN_B2CHANNEL_ACK:
688 ++channel;
689 case HD_OPEN_B1CHANNEL_ACK:
690 bcs = cs->bcs + channel;
691 update_basstate(ucs, BS_B1OPEN << channel, 0);
692 gigaset_bchannel_up(bcs);
693 break;
694
695 case HD_OPEN_ATCHANNEL_ACK:
696 update_basstate(ucs, BS_ATOPEN, 0);
697 start_cbsend(cs);
698 break;
699
700 case HD_CLOSE_B2CHANNEL_ACK:
701 ++channel;
702 case HD_CLOSE_B1CHANNEL_ACK:
703 bcs = cs->bcs + channel;
704 update_basstate(ucs, 0, BS_B1OPEN << channel);
705 stopurbs(bcs->hw.bas);
706 gigaset_bchannel_down(bcs);
707 break;
708
709 case HD_CLOSE_ATCHANNEL_ACK:
710 update_basstate(ucs, 0, BS_ATOPEN);
711 break;
712
713 case HD_B2_FLOW_CONTROL:
714 ++channel;
715 case HD_B1_FLOW_CONTROL:
716 bcs = cs->bcs + channel;
717 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700718 &bcs->hw.bas->corrbytes);
719 gig_dbg(DEBUG_ISO,
720 "Flow control (channel %d, sub %d): 0x%02x => %d",
721 channel, bcs->hw.bas->numsub, l,
722 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800723 break;
724
725 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
726 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700727 dev_warn(cs->dev,
728 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800729 break;
730 }
731 spin_lock_irqsave(&cs->lock, flags);
732 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700733 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700734 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700735 "receive AT data overrun, %d bytes lost\n",
736 ucs->rcvbuf_size);
737 kfree(ucs->rcvbuf);
738 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800739 }
740 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
741 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700742 dev_err(cs->dev, "out of memory receiving AT data\n");
743 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800744 break;
745 }
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700748 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800749 kfree(ucs->rcvbuf);
750 ucs->rcvbuf = NULL;
751 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700752 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700753 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700754 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700755 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700756 break;
757 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800758 }
759 spin_unlock_irqrestore(&cs->lock, flags);
760 break;
761
762 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700763 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764 break;
765
766 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700767 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800768 break;
769
770 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700771 dev_warn(cs->dev,
772 "unknown Gigaset signal 0x%02x (%u) ignored\n",
773 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774 }
775
776 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800777 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800778
779resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800780 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700781 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700782 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700783 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784 error_reset(cs);
785 }
786}
787
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800788/* read_iso_callback
789 * USB completion handler for B channel isochronous input
790 * called by the USB subsystem in interrupt context
791 * parameter:
792 * urb USB request block of completed request
793 * urb->context = bc_state structure
794 */
David Howells7d12e782006-10-05 14:55:46 +0100795static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800796{
797 struct bc_state *bcs;
798 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800799 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800800 unsigned long flags;
801 int i, rc;
802
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800803 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800804 if (unlikely(status == -ENOENT ||
805 status == -ECONNRESET ||
806 status == -EINPROGRESS ||
807 status == -ENODEV ||
808 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700809 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800810 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800811 return;
812 }
813
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700814 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816
817 spin_lock_irqsave(&ubc->isoinlock, flags);
818 if (likely(ubc->isoindone == NULL)) {
819 /* pass URB to tasklet */
820 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800821 ubc->isoinstatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800822 tasklet_schedule(&ubc->rcvd_tasklet);
823 } else {
824 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800825 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800826 for (i = 0; i < BAS_NUMFRAMES; i++) {
827 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
828 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700829 urb->iso_frame_desc[i].status !=
830 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800831 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800832 urb->iso_frame_desc[i].status = 0;
833 urb->iso_frame_desc[i].actual_length = 0;
834 }
835 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700836 /* urb->dev is clobbered by USB subsystem */
837 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800838 urb->transfer_flags = URB_ISO_ASAP;
839 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700840 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
841 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800842 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700843 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700844 dev_err(bcs->cs->dev,
845 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700846 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800847 dump_urb(DEBUG_ISO, "isoc read", urb);
848 error_hangup(bcs);
849 }
850 }
851 }
852 spin_unlock_irqrestore(&ubc->isoinlock, flags);
853}
854
855/* write_iso_callback
856 * USB completion handler for B channel isochronous output
857 * called by the USB subsystem in interrupt context
858 * parameter:
859 * urb USB request block of completed request
860 * urb->context = isow_urbctx_t structure
861 */
David Howells7d12e782006-10-05 14:55:46 +0100862static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800863{
864 struct isow_urbctx_t *ucx;
865 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800866 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800867 unsigned long flags;
868
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800870 if (unlikely(status == -ENOENT ||
871 status == -ECONNRESET ||
872 status == -EINPROGRESS ||
873 status == -ENODEV ||
874 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700875 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800876 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800877 return;
878 }
879
880 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700881 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800882 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800883 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800884
885 spin_lock_irqsave(&ubc->isooutlock, flags);
886 ubc->isooutovfl = ubc->isooutdone;
887 ubc->isooutdone = ucx;
888 spin_unlock_irqrestore(&ubc->isooutlock, flags);
889 tasklet_schedule(&ubc->sent_tasklet);
890}
891
892/* starturbs
893 * prepare and submit USB request blocks for isochronous input and output
894 * argument:
895 * B channel control structure
896 * return value:
897 * 0 on success
898 * < 0 on error (no URBs submitted)
899 */
900static int starturbs(struct bc_state *bcs)
901{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700902 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800903 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800904 int j, k;
905 int rc;
906
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800907 /* initialize L2 reception */
908 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
909 bcs->inputstate |= INS_flag_hunt;
910
911 /* submit all isochronous input URBs */
912 atomic_set(&ubc->running, 1);
913 for (k = 0; k < BAS_INURBS; k++) {
914 urb = ubc->isoinurbs[k];
915 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800916 rc = -EFAULT;
917 goto error;
918 }
919
920 urb->dev = bcs->cs->hw.bas->udev;
921 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
922 urb->transfer_flags = URB_ISO_ASAP;
923 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
924 urb->transfer_buffer_length = BAS_INBUFSIZE;
925 urb->number_of_packets = BAS_NUMFRAMES;
926 urb->interval = BAS_FRAMETIME;
927 urb->complete = read_iso_callback;
928 urb->context = bcs;
929 for (j = 0; j < BAS_NUMFRAMES; j++) {
930 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
931 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
932 urb->iso_frame_desc[j].status = 0;
933 urb->iso_frame_desc[j].actual_length = 0;
934 }
935
936 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800937 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800938 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800939 }
940
941 /* initialize L2 transmission */
942 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
943
944 /* set up isochronous output URBs for flag idling */
945 for (k = 0; k < BAS_OUTURBS; ++k) {
946 urb = ubc->isoouturbs[k].urb;
947 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800948 rc = -EFAULT;
949 goto error;
950 }
951 urb->dev = bcs->cs->hw.bas->udev;
952 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
953 urb->transfer_flags = URB_ISO_ASAP;
954 urb->transfer_buffer = ubc->isooutbuf->data;
955 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
956 urb->number_of_packets = BAS_NUMFRAMES;
957 urb->interval = BAS_FRAMETIME;
958 urb->complete = write_iso_callback;
959 urb->context = &ubc->isoouturbs[k];
960 for (j = 0; j < BAS_NUMFRAMES; ++j) {
961 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
962 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
963 urb->iso_frame_desc[j].status = 0;
964 urb->iso_frame_desc[j].actual_length = 0;
965 }
966 ubc->isoouturbs[k].limit = -1;
967 }
968
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800969 /* keep one URB free, submit the others */
970 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800971 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800972 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700973 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800975 }
976 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800977 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800978 ubc->isooutdone = ubc->isooutovfl = NULL;
979 return 0;
980 error:
981 stopurbs(ubc);
982 return rc;
983}
984
985/* stopurbs
986 * cancel the USB request blocks for isochronous input and output
987 * errors are silently ignored
988 * argument:
989 * B channel control structure
990 */
991static void stopurbs(struct bas_bc_state *ubc)
992{
993 int k, rc;
994
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800995 atomic_set(&ubc->running, 0);
996
997 for (k = 0; k < BAS_INURBS; ++k) {
998 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700999 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001000 "%s: isoc input URB %d unlinked, result = %s",
1001 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001002 }
1003
1004 for (k = 0; k < BAS_OUTURBS; ++k) {
1005 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001006 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001007 "%s: isoc output URB %d unlinked, result = %s",
1008 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001009 }
1010}
1011
1012/* Isochronous Write - Bottom Half */
1013/* =============================== */
1014
1015/* submit_iso_write_urb
1016 * fill and submit the next isochronous write URB
1017 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001018 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001019 * return value:
1020 * number of frames submitted in URB
1021 * 0 if URB not submitted because no data available (isooutbuf busy)
1022 * error code < 0 on error
1023 */
1024static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1025{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001026 struct urb *urb = ucx->urb;
1027 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001028 struct usb_iso_packet_descriptor *ifd;
1029 int corrbytes, nframe, rc;
1030
Tilman Schmidt784d5852006-04-10 22:55:04 -07001031 /* urb->dev is clobbered by USB subsystem */
1032 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001033 urb->transfer_flags = URB_ISO_ASAP;
1034 urb->transfer_buffer = ubc->isooutbuf->data;
1035 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1036
1037 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1038 ifd = &urb->iso_frame_desc[nframe];
1039
1040 /* compute frame length according to flow control */
1041 ifd->length = BAS_NORMFRAME;
1042 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001043 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1044 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001045 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1046 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1047 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1048 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1049 ifd->length += corrbytes;
1050 atomic_add(-corrbytes, &ubc->corrbytes);
1051 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001052
1053 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001054 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1055 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001056 if (ifd->offset < 0) {
1057 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001058 gig_dbg(DEBUG_ISO,
1059 "%s: buffer busy at frame %d",
1060 __func__, nframe);
1061 /* tasklet will be restarted from
1062 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001063 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001064 dev_err(ucx->bcs->cs->dev,
1065 "%s: buffer error %d at frame %d\n",
1066 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001067 return ifd->offset;
1068 }
1069 break;
1070 }
1071 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1072 ifd->status = 0;
1073 ifd->actual_length = 0;
1074 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001075 if (unlikely(nframe == 0))
1076 return 0; /* no data to send */
1077 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001078
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001079 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001080 if (unlikely(rc)) {
1081 if (rc == -ENODEV)
1082 /* device removed - give up silently */
1083 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1084 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001085 dev_err(ucx->bcs->cs->dev,
1086 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001087 get_usb_rcmsg(rc));
1088 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001089 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001090 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001091 return nframe;
1092}
1093
1094/* write_iso_tasklet
1095 * tasklet scheduled when an isochronous output URB from the Gigaset device
1096 * has completed
1097 * parameter:
1098 * data B channel state structure
1099 */
1100static void write_iso_tasklet(unsigned long data)
1101{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001102 struct bc_state *bcs = (struct bc_state *) data;
1103 struct bas_bc_state *ubc = bcs->hw.bas;
1104 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001105 struct isow_urbctx_t *done, *next, *ovfl;
1106 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001107 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001108 struct usb_iso_packet_descriptor *ifd;
1109 int offset;
1110 unsigned long flags;
1111 int i;
1112 struct sk_buff *skb;
1113 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001114 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001115
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001116 /* loop while completed URBs arrive in time */
1117 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001119 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001120 return;
1121 }
1122
1123 /* retrieve completed URBs */
1124 spin_lock_irqsave(&ubc->isooutlock, flags);
1125 done = ubc->isooutdone;
1126 ubc->isooutdone = NULL;
1127 ovfl = ubc->isooutovfl;
1128 ubc->isooutovfl = NULL;
1129 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1130 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001131 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001132 error_hangup(bcs);
1133 break;
1134 }
1135 if (!done)
1136 break;
1137
1138 /* submit free URB if available */
1139 spin_lock_irqsave(&ubc->isooutlock, flags);
1140 next = ubc->isooutfree;
1141 ubc->isooutfree = NULL;
1142 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1143 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001144 rc = submit_iso_write_urb(next);
1145 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001146 /* could not submit URB, put it back */
1147 spin_lock_irqsave(&ubc->isooutlock, flags);
1148 if (ubc->isooutfree == NULL) {
1149 ubc->isooutfree = next;
1150 next = NULL;
1151 }
1152 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1153 if (next) {
1154 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001155 dev_err(cs->dev,
1156 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 error_hangup(bcs);
1158 }
1159 }
1160 }
1161
1162 /* process completed URB */
1163 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001164 status = done->status;
1165 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001166 case -EXDEV: /* partial completion */
1167 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1168 __func__);
1169 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001170 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001171 /* inspect individual frames
1172 * assumptions (for lack of documentation):
1173 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001174 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001175 * - all following frames are not sent at all
1176 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001177 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001178 for (i = 0; i < BAS_NUMFRAMES; i++) {
1179 ifd = &urb->iso_frame_desc[i];
1180 if (ifd->status ||
1181 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001182 dev_warn(cs->dev,
1183 "isochronous write: frame %d: %s, "
1184 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001185 i, get_usb_statmsg(ifd->status),
1186 ifd->actual_length, ifd->length);
1187 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001188 ifd->actual_length)
1189 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001190 break;
1191 }
1192 }
1193#ifdef CONFIG_GIGASET_DEBUG
1194 /* check assumption on remaining frames */
1195 for (; i < BAS_NUMFRAMES; i++) {
1196 ifd = &urb->iso_frame_desc[i];
1197 if (ifd->status != -EINPROGRESS
1198 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001199 dev_warn(cs->dev,
1200 "isochronous write: frame %d: %s, "
1201 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001202 i, get_usb_statmsg(ifd->status),
1203 ifd->actual_length, ifd->length);
1204 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001205 ifd->actual_length)
1206 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001207 break;
1208 }
1209 }
1210#endif
1211 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001212 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001213 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 error_hangup(bcs);
1215 break;
1216 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001217 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001218 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001219 }
1220
1221 /* mark the write buffer area covered by this URB as free */
1222 if (done->limit >= 0)
1223 atomic_set(&ubc->isooutbuf->read, done->limit);
1224
1225 /* mark URB as free */
1226 spin_lock_irqsave(&ubc->isooutlock, flags);
1227 next = ubc->isooutfree;
1228 ubc->isooutfree = done;
1229 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1230 if (next) {
1231 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001232 rc = submit_iso_write_urb(next);
1233 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001234 /* couldn't submit */
1235 error_hangup(bcs);
1236 }
1237 }
1238 }
1239
1240 /* process queued SKBs */
1241 while ((skb = skb_dequeue(&bcs->squeue))) {
1242 /* copy to output buffer, doing L2 encapsulation */
1243 len = skb->len;
1244 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1245 /* insufficient buffer space, push back onto queue */
1246 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001247 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1248 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 break;
1250 }
1251 skb_pull(skb, len);
1252 gigaset_skb_sent(bcs, skb);
1253 dev_kfree_skb_any(skb);
1254 }
1255}
1256
1257/* Isochronous Read - Bottom Half */
1258/* ============================== */
1259
1260/* read_iso_tasklet
1261 * tasklet scheduled when an isochronous input URB from the Gigaset device
1262 * has completed
1263 * parameter:
1264 * data B channel state structure
1265 */
1266static void read_iso_tasklet(unsigned long data)
1267{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001268 struct bc_state *bcs = (struct bc_state *) data;
1269 struct bas_bc_state *ubc = bcs->hw.bas;
1270 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001271 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001272 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001273 char *rcvbuf;
1274 unsigned long flags;
1275 int totleft, numbytes, offset, frame, rc;
1276
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001277 /* loop while more completed URBs arrive in the meantime */
1278 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 /* retrieve URB */
1280 spin_lock_irqsave(&ubc->isoinlock, flags);
1281 if (!(urb = ubc->isoindone)) {
1282 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1283 return;
1284 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001285 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001286 ubc->isoindone = NULL;
1287 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001288 dev_warn(cs->dev,
1289 "isochronous read overrun, "
1290 "dropped URB with status: %s, %d bytes lost\n",
1291 get_usb_statmsg(ubc->loststatus),
1292 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001293 ubc->loststatus = -EINPROGRESS;
1294 }
1295 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1296
1297 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001298 gig_dbg(DEBUG_ISO,
1299 "%s: channel not running, "
1300 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001301 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001302 return;
1303 }
1304
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001305 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001306 case 0: /* normal completion */
1307 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001308 case -EXDEV: /* inspect individual frames
1309 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001310 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1311 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001312 break;
1313 case -ENOENT:
1314 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001315 case -EINPROGRESS:
1316 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001317 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001318 continue; /* -> skip */
1319 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001320 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 error_hangup(bcs);
1322 continue; /* -> skip */
1323 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001324 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001325 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001326 goto error;
1327 }
1328
1329 rcvbuf = urb->transfer_buffer;
1330 totleft = urb->actual_length;
1331 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1332 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001333 dev_warn(cs->dev,
1334 "isochronous read: frame %d: %s\n",
1335 frame,
1336 get_usb_statmsg(
1337 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001338 break;
1339 }
1340 numbytes = urb->iso_frame_desc[frame].actual_length;
1341 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001342 dev_warn(cs->dev,
1343 "isochronous read: frame %d: "
1344 "numbytes (%d) > BAS_MAXFRAME\n",
1345 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001346 break;
1347 }
1348 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001349 dev_warn(cs->dev,
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001353 break;
1354 }
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001357 dev_warn(cs->dev,
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001362 break;
1363 }
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1366 }
1367 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001368 dev_warn(cs->dev,
1369 "isochronous read: %d data bytes missing\n",
1370 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001371
1372 error:
1373 /* URB processed, resubmit */
1374 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1375 urb->iso_frame_desc[frame].status = 0;
1376 urb->iso_frame_desc[frame].actual_length = 0;
1377 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001382 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001383 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_err(cs->dev,
1385 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001386 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001387 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1388 error_hangup(bcs);
1389 }
1390 }
1391}
1392
1393/* Channel Operations */
1394/* ================== */
1395
1396/* req_timeout
1397 * timeout routine for control output request
1398 * argument:
1399 * B channel control structure
1400 */
1401static void req_timeout(unsigned long data)
1402{
1403 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001405 int pending;
1406 unsigned long flags;
1407
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001408 check_pending(ucs);
1409
1410 spin_lock_irqsave(&ucs->lock, flags);
1411 pending = ucs->pending;
1412 ucs->pending = 0;
1413 spin_unlock_irqrestore(&ucs->lock, flags);
1414
1415 switch (pending) {
1416 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 break;
1419
1420 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001422 error_reset(bcs->cs);
1423 break;
1424
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1428 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001429 error_hangup(bcs);
1430 break;
1431
1432 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001434 break;
1435
1436 case HD_CLOSE_B2CHANNEL:
1437 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001438 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1439 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001440 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001441 break;
1442
1443 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001444 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1445 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001447
1448 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001449}
1450
1451/* write_ctrl_callback
1452 * USB completion handler for control pipe output
1453 * called by the USB subsystem in interrupt context
1454 * parameter:
1455 * urb USB request block of completed request
1456 * urb->context = hardware specific controller state structure
1457 */
David Howells7d12e782006-10-05 14:55:46 +01001458static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001459{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001460 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001461 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001462 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001463 unsigned long flags;
1464
Tilman Schmidt06163f82006-06-26 00:25:33 -07001465 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001466 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001467 case 0: /* normal completion */
1468 spin_lock_irqsave(&ucs->lock, flags);
1469 switch (ucs->pending) {
1470 case HD_DEVICE_INIT_ACK: /* no reply expected */
1471 del_timer(&ucs->timer_ctrl);
1472 ucs->pending = 0;
1473 break;
1474 }
1475 spin_unlock_irqrestore(&ucs->lock, flags);
1476 return;
1477
1478 case -ENOENT: /* cancelled */
1479 case -ECONNRESET: /* cancelled (async) */
1480 case -EINPROGRESS: /* pending */
1481 case -ENODEV: /* device removed */
1482 case -ESHUTDOWN: /* device shut down */
1483 /* ignore silently */
1484 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001485 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001486 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001487
1488 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001489 /* don't retry if suspend requested */
1490 if (++ucs->retry_ctrl > BAS_RETRY ||
1491 (atomic_read(&ucs->basstate) & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001492 dev_err(&ucs->interface->dev,
1493 "control request 0x%02x failed: %s\n",
1494 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001495 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001496 break; /* give up */
1497 }
1498 dev_notice(&ucs->interface->dev,
1499 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001500 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001501 ucs->retry_ctrl);
1502 /* urb->dev is clobbered by USB subsystem */
1503 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001504 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001505 if (unlikely(rc)) {
1506 dev_err(&ucs->interface->dev,
1507 "could not resubmit request 0x%02x: %s\n",
1508 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1509 break;
1510 }
1511 /* resubmitted */
1512 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001513 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001514
1515 /* failed, clear pending request */
1516 spin_lock_irqsave(&ucs->lock, flags);
1517 del_timer(&ucs->timer_ctrl);
1518 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001519 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001520 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001521}
1522
1523/* req_submit
1524 * submit a control output request without message buffer to the Gigaset base
1525 * and optionally start a timeout
1526 * parameters:
1527 * bcs B channel control structure
1528 * req control request code (HD_*)
1529 * val control request parameter value (set to 0 if unused)
1530 * timeout timeout in seconds (0: no timeout)
1531 * return value:
1532 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001533 * -EBUSY if another request is pending
1534 * any URB submission error code
1535 */
1536static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1537{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001538 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001539 int ret;
1540 unsigned long flags;
1541
Tilman Schmidt784d5852006-04-10 22:55:04 -07001542 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001543
1544 spin_lock_irqsave(&ucs->lock, flags);
1545 if (ucs->pending) {
1546 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001547 dev_err(bcs->cs->dev,
1548 "submission of request 0x%02x failed: "
1549 "request 0x%02x still pending\n",
1550 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001551 return -EBUSY;
1552 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001553
1554 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1555 ucs->dr_ctrl.bRequest = req;
1556 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1557 ucs->dr_ctrl.wIndex = 0;
1558 ucs->dr_ctrl.wLength = 0;
1559 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001560 usb_sndctrlpipe(ucs->udev, 0),
1561 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1562 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001563 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001564 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001565 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001566 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001567 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001568 spin_unlock_irqrestore(&ucs->lock, flags);
1569 return ret;
1570 }
1571 ucs->pending = req;
1572
1573 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001574 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001575 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1576 ucs->timer_ctrl.data = (unsigned long) bcs;
1577 ucs->timer_ctrl.function = req_timeout;
1578 add_timer(&ucs->timer_ctrl);
1579 }
1580
1581 spin_unlock_irqrestore(&ucs->lock, flags);
1582 return 0;
1583}
1584
1585/* gigaset_init_bchannel
1586 * called by common.c to connect a B channel
1587 * initialize isochronous I/O and tell the Gigaset base to open the channel
1588 * argument:
1589 * B channel control structure
1590 * return value:
1591 * 0 on success, error code < 0 on error
1592 */
1593static int gigaset_init_bchannel(struct bc_state *bcs)
1594{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001595 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001596 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001597 unsigned long flags;
1598
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001599 spin_lock_irqsave(&cs->lock, flags);
1600 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001601 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001602 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001603 return -ENODEV;
1604 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001605
Tilman Schmidt024fd292008-02-06 01:38:26 -08001606 if (atomic_read(&cs->hw.bas->basstate) & BS_SUSPEND) {
1607 dev_notice(cs->dev,
1608 "not starting isochronous I/O, "
1609 "suspend in progress\n");
1610 spin_unlock_irqrestore(&cs->lock, flags);
1611 return -EHOSTUNREACH;
1612 }
1613
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001614 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001615 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001616 "could not start isochronous I/O for channel B%d: %s\n",
1617 bcs->channel + 1,
1618 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1619 if (ret != -ENODEV)
1620 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001621 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001622 return ret;
1623 }
1624
1625 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1626 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001627 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001628 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001629 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001630 if (ret != -ENODEV)
1631 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001632 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001633
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001634 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001635 return ret;
1636}
1637
1638/* gigaset_close_bchannel
1639 * called by common.c to disconnect a B channel
1640 * tell the Gigaset base to close the channel
1641 * stopping isochronous I/O and LL notification will be done when the
1642 * acknowledgement for the close arrives
1643 * argument:
1644 * B channel control structure
1645 * return value:
1646 * 0 on success, error code < 0 on error
1647 */
1648static int gigaset_close_bchannel(struct bc_state *bcs)
1649{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001650 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001651 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001652 unsigned long flags;
1653
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001654 spin_lock_irqsave(&cs->lock, flags);
1655 if (unlikely(!cs->connected)) {
1656 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001657 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1658 return -ENODEV;
1659 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001660
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001661 if (!(atomic_read(&cs->hw.bas->basstate) &
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001662 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1663 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001664 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001665 gigaset_bchannel_down(bcs);
1666 return 0;
1667 }
1668
Tilman Schmidt73a88812006-04-22 02:35:30 -07001669 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001670 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1671 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001672 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001673 bcs->channel + 1);
1674
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001675 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001676 return ret;
1677}
1678
1679/* Device Operations */
1680/* ================= */
1681
1682/* complete_cb
1683 * unqueue first command buffer from queue, waking any sleepers
1684 * must be called with cs->cmdlock held
1685 * parameter:
1686 * cs controller state structure
1687 */
1688static void complete_cb(struct cardstate *cs)
1689{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001690 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001691
1692 /* unqueue completed buffer */
1693 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001694 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1695 "write_command: sent %u bytes, %u left",
1696 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001697 if ((cs->cmdbuf = cb->next) != NULL) {
1698 cs->cmdbuf->prev = NULL;
1699 cs->curlen = cs->cmdbuf->len;
1700 } else {
1701 cs->lastcmdbuf = NULL;
1702 cs->curlen = 0;
1703 }
1704
1705 if (cb->wake_tasklet)
1706 tasklet_schedule(cb->wake_tasklet);
1707
1708 kfree(cb);
1709}
1710
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001711/* write_command_callback
1712 * USB completion handler for AT command transmission
1713 * called by the USB subsystem in interrupt context
1714 * parameter:
1715 * urb USB request block of completed request
1716 * urb->context = controller state structure
1717 */
David Howells7d12e782006-10-05 14:55:46 +01001718static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001719{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001720 struct cardstate *cs = urb->context;
1721 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001722 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001723 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001724
Tilman Schmidt73a88812006-04-22 02:35:30 -07001725 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001726 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001727
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001729 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001730 case 0: /* normal completion */
1731 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001732 case -ENOENT: /* cancelled */
1733 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001734 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001735 case -ENODEV: /* device removed */
1736 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001737 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001738 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001739 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001740 return;
1741 default: /* any failure */
1742 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001743 dev_warn(cs->dev,
1744 "command write: %s, "
1745 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001746 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001747 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001748 break;
1749 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001750 if (atomic_read(&ucs->basstate) & BS_SUSPEND) {
1751 dev_warn(cs->dev,
1752 "command write: %s, "
1753 "won't retry - suspend requested\n",
1754 get_usb_statmsg(status));
1755 break;
1756 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001757 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001758 dev_warn(cs->dev,
1759 "command write: %s, "
1760 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001761 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001762 break;
1763 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001764 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001765 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001766 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1767 /* resubmitted - bypass regular exit block */
1768 return;
1769 /* command send failed, assume base still waiting */
1770 update_basstate(ucs, BS_ATREADY, 0);
1771 }
1772
1773 spin_lock_irqsave(&cs->cmdlock, flags);
1774 if (cs->cmdbuf != NULL)
1775 complete_cb(cs);
1776 spin_unlock_irqrestore(&cs->cmdlock, flags);
1777}
1778
1779/* atrdy_timeout
1780 * timeout routine for AT command transmission
1781 * argument:
1782 * controller state structure
1783 */
1784static void atrdy_timeout(unsigned long data)
1785{
1786 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001787 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001788
Tilman Schmidt784d5852006-04-10 22:55:04 -07001789 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001790
1791 /* fake the missing signal - what else can I do? */
1792 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1793 start_cbsend(cs);
1794}
1795
1796/* atwrite_submit
1797 * submit an HD_WRITE_ATMESSAGE command URB
1798 * parameters:
1799 * cs controller state structure
1800 * buf buffer containing command to send
1801 * len length of command to send
1802 * return value:
1803 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001804 * -EBUSY if another request is pending
1805 * any URB submission error code
1806 */
1807static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1808{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001809 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001810 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001811
Tilman Schmidt784d5852006-04-10 22:55:04 -07001812 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001813
Tilman Schmidt73a88812006-04-22 02:35:30 -07001814 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001815 dev_err(cs->dev,
1816 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001817 return -EBUSY;
1818 }
1819
1820 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1821 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1822 ucs->dr_cmd_out.wValue = 0;
1823 ucs->dr_cmd_out.wIndex = 0;
1824 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1825 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1826 usb_sndctrlpipe(ucs->udev, 0),
1827 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1828 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001829 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001830 if (unlikely(rc)) {
1831 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001832 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001833 get_usb_rcmsg(rc));
1834 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001835 }
1836
Tilman Schmidt73a88812006-04-22 02:35:30 -07001837 /* submitted successfully, start timeout if necessary */
1838 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001839 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1840 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001841 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1842 ucs->timer_atrdy.data = (unsigned long) cs;
1843 ucs->timer_atrdy.function = atrdy_timeout;
1844 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001845 }
1846 return 0;
1847}
1848
1849/* start_cbsend
1850 * start transmission of AT command queue if necessary
1851 * parameter:
1852 * cs controller state structure
1853 * return value:
1854 * 0 on success
1855 * error code < 0 on error
1856 */
1857static int start_cbsend(struct cardstate *cs)
1858{
1859 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001860 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001861 unsigned long flags;
1862 int rc;
1863 int retval = 0;
1864
Tilman Schmidt024fd292008-02-06 01:38:26 -08001865 /* check if suspend requested */
1866 if (atomic_read(&ucs->basstate) & BS_SUSPEND) {
1867 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1868 return -EHOSTUNREACH;
1869 }
1870
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001871 /* check if AT channel is open */
1872 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001873 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001874 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1875 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001876 /* flush command queue */
1877 spin_lock_irqsave(&cs->cmdlock, flags);
1878 while (cs->cmdbuf != NULL)
1879 complete_cb(cs);
1880 spin_unlock_irqrestore(&cs->cmdlock, flags);
1881 }
1882 return rc;
1883 }
1884
1885 /* try to send first command in queue */
1886 spin_lock_irqsave(&cs->cmdlock, flags);
1887
1888 while ((cb = cs->cmdbuf) != NULL &&
1889 atomic_read(&ucs->basstate) & BS_ATREADY) {
1890 ucs->retry_cmd_out = 0;
1891 rc = atwrite_submit(cs, cb->buf, cb->len);
1892 if (unlikely(rc)) {
1893 retval = rc;
1894 complete_cb(cs);
1895 }
1896 }
1897
1898 spin_unlock_irqrestore(&cs->cmdlock, flags);
1899 return retval;
1900}
1901
1902/* gigaset_write_cmd
1903 * This function is called by the device independent part of the driver
1904 * to transmit an AT command string to the Gigaset device.
1905 * It encapsulates the device specific method for transmission over the
1906 * direct USB connection to the base.
1907 * The command string is added to the queue of commands to send, and
1908 * USB transmission is started if necessary.
1909 * parameters:
1910 * cs controller state structure
1911 * buf command string to send
1912 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001913 * wake_tasklet tasklet to run when transmission is completed
1914 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001915 * return value:
1916 * number of bytes queued on success
1917 * error code < 0 on error
1918 */
1919static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001920 const unsigned char *buf, int len,
1921 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001922{
1923 struct cmdbuf_t *cb;
1924 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001925 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001926
1927 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001928 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001929 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001930
Tilman Schmidt39f07222006-12-13 00:33:52 -08001931 if (len <= 0) {
1932 /* nothing to do */
1933 rc = 0;
1934 goto notqueued;
1935 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001936
1937 if (len > IF_WRITEBUF)
1938 len = IF_WRITEBUF;
1939 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001940 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001941 rc = -ENOMEM;
1942 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001943 }
1944
1945 memcpy(cb->buf, buf, len);
1946 cb->len = len;
1947 cb->offset = 0;
1948 cb->next = NULL;
1949 cb->wake_tasklet = wake_tasklet;
1950
1951 spin_lock_irqsave(&cs->cmdlock, flags);
1952 cb->prev = cs->lastcmdbuf;
1953 if (cs->lastcmdbuf)
1954 cs->lastcmdbuf->next = cb;
1955 else {
1956 cs->cmdbuf = cb;
1957 cs->curlen = len;
1958 }
1959 cs->cmdbytes += len;
1960 cs->lastcmdbuf = cb;
1961 spin_unlock_irqrestore(&cs->cmdlock, flags);
1962
Tilman Schmidt73a88812006-04-22 02:35:30 -07001963 spin_lock_irqsave(&cs->lock, flags);
1964 if (unlikely(!cs->connected)) {
1965 spin_unlock_irqrestore(&cs->lock, flags);
1966 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001967 /* flush command queue */
1968 spin_lock_irqsave(&cs->cmdlock, flags);
1969 while (cs->cmdbuf != NULL)
1970 complete_cb(cs);
1971 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001972 return -ENODEV;
1973 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001974 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001975 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001976 return rc < 0 ? rc : len;
1977
1978notqueued: /* request handled without queuing */
1979 if (wake_tasklet)
1980 tasklet_schedule(wake_tasklet);
1981 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001982}
1983
1984/* gigaset_write_room
1985 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001986 * return number of characters the driver will accept to be written via
1987 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001988 * parameter:
1989 * controller state structure
1990 * return value:
1991 * number of characters
1992 */
1993static int gigaset_write_room(struct cardstate *cs)
1994{
1995 return IF_WRITEBUF;
1996}
1997
1998/* gigaset_chars_in_buffer
1999 * tty_driver.chars_in_buffer interface routine
2000 * return number of characters waiting to be sent
2001 * parameter:
2002 * controller state structure
2003 * return value:
2004 * number of characters
2005 */
2006static int gigaset_chars_in_buffer(struct cardstate *cs)
2007{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002008 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002009}
2010
2011/* gigaset_brkchars
2012 * implementation of ioctl(GIGASET_BRKCHARS)
2013 * parameter:
2014 * controller state structure
2015 * return value:
2016 * -EINVAL (unimplemented function)
2017 */
2018static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2019{
2020 return -EINVAL;
2021}
2022
2023
2024/* Device Initialization/Shutdown */
2025/* ============================== */
2026
2027/* Free hardware dependent part of the B channel structure
2028 * parameter:
2029 * bcs B channel structure
2030 * return value:
2031 * !=0 on success
2032 */
2033static int gigaset_freebcshw(struct bc_state *bcs)
2034{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002035 struct bas_bc_state *ubc = bcs->hw.bas;
2036 int i;
2037
2038 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002039 return 0;
2040
Tilman Schmidt73a88812006-04-22 02:35:30 -07002041 /* kill URBs and tasklets before freeing - better safe than sorry */
2042 atomic_set(&ubc->running, 0);
Tilman Schmidt39f07222006-12-13 00:33:52 -08002043 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2044 for (i = 0; i < BAS_OUTURBS; ++i) {
2045 usb_kill_urb(ubc->isoouturbs[i].urb);
2046 usb_free_urb(ubc->isoouturbs[i].urb);
2047 }
2048 for (i = 0; i < BAS_INURBS; ++i) {
2049 usb_kill_urb(ubc->isoinurbs[i]);
2050 usb_free_urb(ubc->isoinurbs[i]);
2051 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002052 tasklet_kill(&ubc->sent_tasklet);
2053 tasklet_kill(&ubc->rcvd_tasklet);
2054 kfree(ubc->isooutbuf);
2055 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002056 bcs->hw.bas = NULL;
2057 return 1;
2058}
2059
2060/* Initialize hardware dependent part of the B channel structure
2061 * parameter:
2062 * bcs B channel structure
2063 * return value:
2064 * !=0 on success
2065 */
2066static int gigaset_initbcshw(struct bc_state *bcs)
2067{
2068 int i;
2069 struct bas_bc_state *ubc;
2070
2071 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2072 if (!ubc) {
2073 err("could not allocate bas_bc_state");
2074 return 0;
2075 }
2076
2077 atomic_set(&ubc->running, 0);
2078 atomic_set(&ubc->corrbytes, 0);
2079 spin_lock_init(&ubc->isooutlock);
2080 for (i = 0; i < BAS_OUTURBS; ++i) {
2081 ubc->isoouturbs[i].urb = NULL;
2082 ubc->isoouturbs[i].bcs = bcs;
2083 }
2084 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2085 ubc->numsub = 0;
2086 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2087 err("could not allocate isochronous output buffer");
2088 kfree(ubc);
2089 bcs->hw.bas = NULL;
2090 return 0;
2091 }
2092 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002093 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002094
2095 spin_lock_init(&ubc->isoinlock);
2096 for (i = 0; i < BAS_INURBS; ++i)
2097 ubc->isoinurbs[i] = NULL;
2098 ubc->isoindone = NULL;
2099 ubc->loststatus = -EINPROGRESS;
2100 ubc->isoinlost = 0;
2101 ubc->seqlen = 0;
2102 ubc->inbyte = 0;
2103 ubc->inbits = 0;
2104 ubc->goodbytes = 0;
2105 ubc->alignerrs = 0;
2106 ubc->fcserrs = 0;
2107 ubc->frameerrs = 0;
2108 ubc->giants = 0;
2109 ubc->runts = 0;
2110 ubc->aborts = 0;
2111 ubc->shared0s = 0;
2112 ubc->stolen0s = 0;
2113 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002114 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002115 return 1;
2116}
2117
2118static void gigaset_reinitbcshw(struct bc_state *bcs)
2119{
2120 struct bas_bc_state *ubc = bcs->hw.bas;
2121
2122 atomic_set(&bcs->hw.bas->running, 0);
2123 atomic_set(&bcs->hw.bas->corrbytes, 0);
2124 bcs->hw.bas->numsub = 0;
2125 spin_lock_init(&ubc->isooutlock);
2126 spin_lock_init(&ubc->isoinlock);
2127 ubc->loststatus = -EINPROGRESS;
2128}
2129
2130static void gigaset_freecshw(struct cardstate *cs)
2131{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002132 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002133 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002134 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002135}
2136
2137static int gigaset_initcshw(struct cardstate *cs)
2138{
2139 struct bas_cardstate *ucs;
2140
2141 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2142 if (!ucs)
2143 return 0;
2144
2145 ucs->urb_cmd_in = NULL;
2146 ucs->urb_cmd_out = NULL;
2147 ucs->rcvbuf = NULL;
2148 ucs->rcvbuf_size = 0;
2149
2150 spin_lock_init(&ucs->lock);
2151 ucs->pending = 0;
2152
2153 atomic_set(&ucs->basstate, 0);
2154 init_timer(&ucs->timer_ctrl);
2155 init_timer(&ucs->timer_atrdy);
2156 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002157 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002158
2159 return 1;
2160}
2161
2162/* freeurbs
2163 * unlink and deallocate all URBs unconditionally
2164 * caller must make sure that no commands are still in progress
2165 * parameter:
2166 * cs controller state structure
2167 */
2168static void freeurbs(struct cardstate *cs)
2169{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002170 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002171 struct bas_bc_state *ubc;
2172 int i, j;
2173
Tilman Schmidt39f07222006-12-13 00:33:52 -08002174 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002175 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002176 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002177 for (i = 0; i < BAS_OUTURBS; ++i) {
2178 usb_kill_urb(ubc->isoouturbs[i].urb);
2179 usb_free_urb(ubc->isoouturbs[i].urb);
2180 ubc->isoouturbs[i].urb = NULL;
2181 }
2182 for (i = 0; i < BAS_INURBS; ++i) {
2183 usb_kill_urb(ubc->isoinurbs[i]);
2184 usb_free_urb(ubc->isoinurbs[i]);
2185 ubc->isoinurbs[i] = NULL;
2186 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002187 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002188 usb_kill_urb(ucs->urb_int_in);
2189 usb_free_urb(ucs->urb_int_in);
2190 ucs->urb_int_in = NULL;
2191 usb_kill_urb(ucs->urb_cmd_out);
2192 usb_free_urb(ucs->urb_cmd_out);
2193 ucs->urb_cmd_out = NULL;
2194 usb_kill_urb(ucs->urb_cmd_in);
2195 usb_free_urb(ucs->urb_cmd_in);
2196 ucs->urb_cmd_in = NULL;
2197 usb_kill_urb(ucs->urb_ctrl);
2198 usb_free_urb(ucs->urb_ctrl);
2199 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002200}
2201
2202/* gigaset_probe
2203 * This function is called when a new USB device is connected.
2204 * It checks whether the new device is handled by this driver.
2205 */
2206static int gigaset_probe(struct usb_interface *interface,
2207 const struct usb_device_id *id)
2208{
2209 struct usb_host_interface *hostif;
2210 struct usb_device *udev = interface_to_usbdev(interface);
2211 struct cardstate *cs = NULL;
2212 struct bas_cardstate *ucs = NULL;
2213 struct bas_bc_state *ubc;
2214 struct usb_endpoint_descriptor *endpoint;
2215 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002216 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002217
Tilman Schmidt784d5852006-04-10 22:55:04 -07002218 gig_dbg(DEBUG_ANY,
2219 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2220 __func__, le16_to_cpu(udev->descriptor.idVendor),
2221 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002222
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002223 /* set required alternate setting */
2224 hostif = interface->cur_altsetting;
2225 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002226 gig_dbg(DEBUG_ANY,
2227 "%s: wrong alternate setting %d - trying to switch",
2228 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002229 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002230 dev_warn(&udev->dev, "usb_set_interface failed, "
2231 "device %d interface %d altsetting %d\n",
2232 udev->devnum, hostif->desc.bInterfaceNumber,
2233 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002234 return -ENODEV;
2235 }
2236 hostif = interface->cur_altsetting;
2237 }
2238
2239 /* Reject application specific interfaces
2240 */
2241 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002242 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2243 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002244 return -ENODEV;
2245 }
2246
Tilman Schmidt784d5852006-04-10 22:55:04 -07002247 dev_info(&udev->dev,
2248 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2249 __func__, le16_to_cpu(udev->descriptor.idVendor),
2250 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002251
2252 cs = gigaset_getunassignedcs(driver);
2253 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002254 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002255 return -ENODEV;
2256 }
2257 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002258
2259 /* save off device structure ptrs for later use */
2260 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002261 ucs->udev = udev;
2262 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002263 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002264
2265 /* allocate URBs:
2266 * - one for the interrupt pipe
2267 * - three for the different uses of the default control pipe
2268 * - three for each isochronous pipe
2269 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002270 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2271 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2272 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2273 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002274 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002275
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002276 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002277 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002278 for (i = 0; i < BAS_OUTURBS; ++i)
2279 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002280 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002281 goto allocerr;
2282 for (i = 0; i < BAS_INURBS; ++i)
2283 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002284 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002285 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002286 }
2287
2288 ucs->rcvbuf = NULL;
2289 ucs->rcvbuf_size = 0;
2290
2291 /* Fill the interrupt urb and send it to the core */
2292 endpoint = &hostif->endpoint[0].desc;
2293 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002294 usb_rcvintpipe(udev,
2295 (endpoint->bEndpointAddress) & 0x0f),
2296 ucs->int_in_buf, 3, read_int_callback, cs,
2297 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002298 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002299 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002300 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301 goto error;
2302 }
2303
2304 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002305 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002306 goto error;
2307
2308 /* tell common part that the device is ready */
2309 if (startmode == SM_LOCKED)
2310 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002311
2312 /* save address of controller structure */
2313 usb_set_intfdata(interface, cs);
2314
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002315 if (!gigaset_start(cs))
2316 goto error;
2317
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002318 return 0;
2319
Tilman Schmidt73a88812006-04-22 02:35:30 -07002320allocerr:
2321 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002322error:
2323 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002324 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002325 gigaset_unassign(cs);
2326 return -ENODEV;
2327}
2328
2329/* gigaset_disconnect
2330 * This function is called when the Gigaset base is unplugged.
2331 */
2332static void gigaset_disconnect(struct usb_interface *interface)
2333{
2334 struct cardstate *cs;
2335 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002336 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002337
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002338 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002339
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002340 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002341
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002342 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002343
2344 /* mark base as not ready, all channels disconnected */
2345 atomic_set(&ucs->basstate, 0);
2346
2347 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002348 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002349 gigaset_bchannel_down(cs->bcs + j);
2350
2351 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002352 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002353
2354 /* stop timers and URBs, free ressources */
2355 del_timer_sync(&ucs->timer_ctrl);
2356 del_timer_sync(&ucs->timer_atrdy);
2357 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002358 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002359 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002360 kfree(ucs->rcvbuf);
2361 ucs->rcvbuf = NULL;
2362 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002363 usb_put_dev(ucs->udev);
2364 ucs->interface = NULL;
2365 ucs->udev = NULL;
2366 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002367 gigaset_unassign(cs);
2368}
2369
Tilman Schmidt024fd292008-02-06 01:38:26 -08002370/* gigaset_suspend
2371 * This function is called before the USB connection is suspended.
2372 */
2373static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2374{
2375 struct cardstate *cs = usb_get_intfdata(intf);
2376 struct bas_cardstate *ucs = cs->hw.bas;
2377 int basstate;
2378 int rc;
2379
2380 /* set suspend flag; this stops AT command/response traffic */
2381 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2382 gig_dbg(DEBUG_SUSPEND, "already suspended");
2383 return 0;
2384 }
2385
2386 /* wait a bit for blocking conditions to go away */
2387 rc = wait_event_timeout(ucs->waitqueue,
2388 !(atomic_read(&ucs->basstate) &
2389 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2390 BAS_TIMEOUT*HZ/10);
2391 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2392
2393 /* check for conditions preventing suspend */
2394 basstate = atomic_read(&ucs->basstate);
2395 if (basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
2396 dev_warn(cs->dev, "cannot suspend:\n");
2397 if (basstate & BS_B1OPEN)
2398 dev_warn(cs->dev, " B channel 1 open\n");
2399 if (basstate & BS_B2OPEN)
2400 dev_warn(cs->dev, " B channel 2 open\n");
2401 if (basstate & BS_ATRDPEND)
2402 dev_warn(cs->dev, " receiving AT reply\n");
2403 if (basstate & BS_ATWRPEND)
2404 dev_warn(cs->dev, " sending AT command\n");
2405 update_basstate(ucs, 0, BS_SUSPEND);
2406 return -EBUSY;
2407 }
2408
2409 /* close AT channel if open */
2410 if (basstate & BS_ATOPEN) {
2411 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2412 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2413 if (rc) {
2414 update_basstate(ucs, 0, BS_SUSPEND);
2415 return rc;
2416 }
2417 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2418 BAS_TIMEOUT*HZ/10);
2419 /* in case of timeout, proceed anyway */
2420 }
2421
2422 /* kill all URBs and timers that might still be pending */
2423 usb_kill_urb(ucs->urb_ctrl);
2424 usb_kill_urb(ucs->urb_int_in);
2425 del_timer_sync(&ucs->timer_ctrl);
2426
2427 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2428 return 0;
2429}
2430
2431/* gigaset_resume
2432 * This function is called after the USB connection has been resumed.
2433 */
2434static int gigaset_resume(struct usb_interface *intf)
2435{
2436 struct cardstate *cs = usb_get_intfdata(intf);
2437 struct bas_cardstate *ucs = cs->hw.bas;
2438 int rc;
2439
2440 /* resubmit interrupt URB for spontaneous messages from base */
2441 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2442 if (rc) {
2443 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2444 get_usb_rcmsg(rc));
2445 return rc;
2446 }
2447
2448 /* clear suspend flag to reallow activity */
2449 update_basstate(ucs, 0, BS_SUSPEND);
2450
2451 gig_dbg(DEBUG_SUSPEND, "resume complete");
2452 return 0;
2453}
2454
2455/* gigaset_pre_reset
2456 * This function is called before the USB connection is reset.
2457 */
2458static int gigaset_pre_reset(struct usb_interface *intf)
2459{
2460 /* handle just like suspend */
2461 return gigaset_suspend(intf, PMSG_ON);
2462}
2463
2464/* gigaset_post_reset
2465 * This function is called after the USB connection has been reset.
2466 */
2467static int gigaset_post_reset(struct usb_interface *intf)
2468{
2469 /* FIXME: send HD_DEVICE_INIT_ACK? */
2470
2471 /* resume operations */
2472 return gigaset_resume(intf);
2473}
2474
2475
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002476static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002477 gigaset_write_cmd,
2478 gigaset_write_room,
2479 gigaset_chars_in_buffer,
2480 gigaset_brkchars,
2481 gigaset_init_bchannel,
2482 gigaset_close_bchannel,
2483 gigaset_initbcshw,
2484 gigaset_freebcshw,
2485 gigaset_reinitbcshw,
2486 gigaset_initcshw,
2487 gigaset_freecshw,
2488 gigaset_set_modem_ctrl,
2489 gigaset_baud_rate,
2490 gigaset_set_line_ctrl,
2491 gigaset_isoc_send_skb,
2492 gigaset_isoc_input,
2493};
2494
2495/* bas_gigaset_init
2496 * This function is called after the kernel module is loaded.
2497 */
2498static int __init bas_gigaset_init(void)
2499{
2500 int result;
2501
2502 /* allocate memory for our driver state and intialize it */
2503 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002504 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002505 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002506 goto error;
2507
2508 /* allocate memory for our device state and intialize it */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002509 cardstate = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
Tilman Schmidt917f5082006-04-10 22:55:00 -07002510 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002511 if (!cardstate)
2512 goto error;
2513
2514 /* register this driver with the USB subsystem */
2515 result = usb_register(&gigaset_usb_driver);
2516 if (result < 0) {
2517 err("usb_register failed (error %d)", -result);
2518 goto error;
2519 }
2520
2521 info(DRIVER_AUTHOR);
2522 info(DRIVER_DESC);
2523 return 0;
2524
2525error: if (cardstate)
2526 gigaset_freecs(cardstate);
2527 cardstate = NULL;
2528 if (driver)
2529 gigaset_freedriver(driver);
2530 driver = NULL;
2531 return -1;
2532}
2533
2534/* bas_gigaset_exit
2535 * This function is called before the kernel module is unloaded.
2536 */
2537static void __exit bas_gigaset_exit(void)
2538{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002539 struct bas_cardstate *ucs = cardstate->hw.bas;
2540
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002541 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002542 * => no gigaset_start any more
2543 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002544
2545 gigaset_shutdown(cardstate);
2546 /* from now on, no isdn callback should be possible */
2547
Tilman Schmidt73a88812006-04-22 02:35:30 -07002548 /* close all still open channels */
2549 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2550 gig_dbg(DEBUG_INIT, "closing B1 channel");
2551 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2552 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2553 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002554 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002555 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2556 gig_dbg(DEBUG_INIT, "closing B2 channel");
2557 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2558 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2559 NULL, 0, BAS_TIMEOUT);
2560 }
2561 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2562 gig_dbg(DEBUG_INIT, "closing AT channel");
2563 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2564 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2565 NULL, 0, BAS_TIMEOUT);
2566 }
2567 atomic_set(&ucs->basstate, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002568
2569 /* deregister this driver with the USB subsystem */
2570 usb_deregister(&gigaset_usb_driver);
2571 /* this will call the disconnect-callback */
2572 /* from now on, no disconnect/probe callback should be running */
2573
2574 gigaset_freecs(cardstate);
2575 cardstate = NULL;
2576 gigaset_freedriver(driver);
2577 driver = NULL;
2578}
2579
2580
2581module_init(bas_gigaset_init);
2582module_exit(bas_gigaset_exit);
2583
2584MODULE_AUTHOR(DRIVER_AUTHOR);
2585MODULE_DESCRIPTION(DRIVER_DESC);
2586MODULE_LICENSE("GPL");