blob: 9e7108a3109b77c295d5b4565b237b3c2489f1ed [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
Tilman Schmidt170ebf82009-03-18 23:44:23 -070049/* interrupt pipe message size according to ibid. ch. 2.2 */
50#define IP_MSGSIZE 3
51
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080052/* Values for the Gigaset 307x */
53#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070054#define USB_3070_PRODUCT_ID 0x0001
55#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080056#define USB_SX303_PRODUCT_ID 0x0021
57#define USB_SX353_PRODUCT_ID 0x0022
58
59/* table of devices that work with this driver */
Tilman Schmidt35dc8452007-03-29 01:20:34 -070060static const struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070061 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080063 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
65 { } /* Terminating entry */
66};
67
68MODULE_DEVICE_TABLE(usb, gigaset_table);
69
Tilman Schmidt06163f82006-06-26 00:25:33 -070070/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080071
Tilman Schmidt06163f82006-06-26 00:25:33 -070072/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080073static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
75
76/* Function will be called if the device is unplugged */
77static void gigaset_disconnect(struct usb_interface *interface);
78
Tilman Schmidt024fd292008-02-06 01:38:26 -080079/* functions called before/after suspend */
80static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
81static int gigaset_resume(struct usb_interface *intf);
82
83/* functions called before/after device reset */
84static int gigaset_pre_reset(struct usb_interface *intf);
85static int gigaset_post_reset(struct usb_interface *intf);
86
Tilman Schmidt06163f82006-06-26 00:25:33 -070087static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070088static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070089static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070090static int atwrite_submit(struct cardstate *, unsigned char *, int);
91static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080092
Tilman Schmidt06163f82006-06-26 00:25:33 -070093/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080094
95struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070096 struct usb_device *udev; /* USB device pointer */
97 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080098 unsigned char minor; /* starting minor number */
99
Tilman Schmidt784d5852006-04-10 22:55:04 -0700100 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101 struct usb_ctrlrequest dr_ctrl;
102 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700103 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800104
105 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700106 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800107 struct usb_ctrlrequest dr_cmd_out;
108 int retry_cmd_out;
109
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800111 struct usb_ctrlrequest dr_cmd_in;
112 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700113 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800114
Tilman Schmidt784d5852006-04-10 22:55:04 -0700115 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700116 unsigned char *int_in_buf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800117
118 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800119 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800120 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800121 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800122 int rcvbuf_size; /* size of AT receive buffer */
123 /* 0: no receive in progress */
124 int retry_cmd_in; /* receive req retry count */
125};
126
127/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700128#define BS_ATOPEN 0x001 /* AT channel open */
129#define BS_B1OPEN 0x002 /* B channel 1 open */
130#define BS_B2OPEN 0x004 /* B channel 2 open */
131#define BS_ATREADY 0x008 /* base ready for AT command */
132#define BS_INIT 0x010 /* base has signalled INIT_OK */
133#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
134#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
135#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800136#define BS_SUSPEND 0x100 /* USB port suspended */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800137
138
139static struct gigaset_driver *driver = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800140
141/* usb specific object needed to register this driver with the usb subsystem */
142static struct usb_driver gigaset_usb_driver = {
143 .name = GIGASET_MODULENAME,
144 .probe = gigaset_probe,
145 .disconnect = gigaset_disconnect,
146 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800147 .suspend = gigaset_suspend,
148 .resume = gigaset_resume,
149 .reset_resume = gigaset_post_reset,
150 .pre_reset = gigaset_pre_reset,
151 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800152};
153
Tilman Schmidt73a88812006-04-22 02:35:30 -0700154/* get message text for usb_submit_urb return code
155 */
156static char *get_usb_rcmsg(int rc)
157{
158 static char unkmsg[28];
159
160 switch (rc) {
161 case 0:
162 return "success";
163 case -ENOMEM:
164 return "out of memory";
165 case -ENODEV:
166 return "device not present";
167 case -ENOENT:
168 return "endpoint not present";
169 case -ENXIO:
170 return "URB type not supported";
171 case -EINVAL:
172 return "invalid argument";
173 case -EAGAIN:
174 return "start frame too early or too much scheduled";
175 case -EFBIG:
176 return "too many isochronous frames requested";
177 case -EPIPE:
178 return "endpoint stalled";
179 case -EMSGSIZE:
180 return "invalid packet size";
181 case -ENOSPC:
182 return "would overcommit USB bandwidth";
183 case -ESHUTDOWN:
184 return "device shut down";
185 case -EPERM:
186 return "reject flag set";
187 case -EHOSTUNREACH:
188 return "device suspended";
189 default:
190 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
191 return unkmsg;
192 }
193}
194
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800195/* get message text for USB status code
196 */
197static char *get_usb_statmsg(int status)
198{
199 static char unkmsg[28];
200
201 switch (status) {
202 case 0:
203 return "success";
204 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700205 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800206 case -EINPROGRESS:
207 return "pending";
208 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700209 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800210 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700211 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700212 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800213 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700214 case -EPIPE:
215 return "endpoint stalled";
216 case -ECOMM:
217 return "IN buffer overrun";
218 case -ENOSR:
219 return "OUT buffer underrun";
220 case -EOVERFLOW:
221 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800222 case -EREMOTEIO:
223 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700224 case -ENODEV:
225 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800226 case -EXDEV:
227 return "partial isochronous transfer";
228 case -EINVAL:
229 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700230 case -ECONNRESET:
231 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700233 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800234 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700235 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800236 return unkmsg;
237 }
238}
239
240/* usb_pipetype_str
241 * retrieve string representation of USB pipe type
242 */
243static inline char *usb_pipetype_str(int pipe)
244{
245 if (usb_pipeisoc(pipe))
246 return "Isoc";
247 if (usb_pipeint(pipe))
248 return "Int";
249 if (usb_pipecontrol(pipe))
250 return "Ctrl";
251 if (usb_pipebulk(pipe))
252 return "Bulk";
253 return "?";
254}
255
256/* dump_urb
257 * write content of URB to syslog for debugging
258 */
259static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700260 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800261{
262#ifdef CONFIG_GIGASET_DEBUG
263 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700264 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800265 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700266 gig_dbg(level,
267 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800268 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700269 (unsigned long) urb->dev,
270 usb_pipetype_str(urb->pipe),
271 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
272 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800273 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700274 urb->transfer_flags);
275 gig_dbg(level,
276 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800277 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 (unsigned long) urb->transfer_buffer,
279 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800280 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700281 gig_dbg(level,
282 " start_frame=%d, number_of_packets=%d, interval=%d, "
283 "error_count=%d,",
284 urb->start_frame, urb->number_of_packets, urb->interval,
285 urb->error_count);
286 gig_dbg(level,
287 " context=0x%08lx, complete=0x%08lx, "
288 "iso_frame_desc[]={",
289 (unsigned long) urb->context,
290 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800291 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700292 struct usb_iso_packet_descriptor *pifd
293 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700294 gig_dbg(level,
295 " {offset=%u, length=%u, actual_length=%u, "
296 "status=%u}",
297 pifd->offset, pifd->length, pifd->actual_length,
298 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800299 }
300 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700301 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800302#endif
303}
304
305/* read/set modem control bits etc. (m10x only) */
306static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700307 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800308{
309 return -EINVAL;
310}
311
312static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
313{
314 return -EINVAL;
315}
316
317static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
318{
319 return -EINVAL;
320}
321
322/* error_hangup
323 * hang up any existing connection because of an unrecoverable error
324 * This function may be called from any context and takes care of scheduling
325 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700326 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800327 * argument:
328 * B channel control structure
329 */
330static inline void error_hangup(struct bc_state *bcs)
331{
332 struct cardstate *cs = bcs->cs;
333
Tilman Schmidt784d5852006-04-10 22:55:04 -0700334 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
335 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800336
Tilman Schmidt73a88812006-04-22 02:35:30 -0700337 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
338 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800339
340 gigaset_schedule_event(cs);
341}
342
343/* error_reset
344 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700345 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700346 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700347 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800348 * argument:
349 * controller state structure
350 */
351static inline void error_reset(struct cardstate *cs)
352{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700353 /* close AT command channel to recover (ignore errors) */
354 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
355
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800356 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700357 dev_err(cs->dev,
358 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800359}
360
361/* check_pending
362 * check for completion of pending control request
363 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700364 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800365 */
366static void check_pending(struct bas_cardstate *ucs)
367{
368 unsigned long flags;
369
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800370 spin_lock_irqsave(&ucs->lock, flags);
371 switch (ucs->pending) {
372 case 0:
373 break;
374 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800375 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800376 ucs->pending = 0;
377 break;
378 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800379 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800380 ucs->pending = 0;
381 break;
382 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800383 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800384 ucs->pending = 0;
385 break;
386 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800387 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800388 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800389 break;
390 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800391 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 ucs->pending = 0;
393 break;
394 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800395 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800396 ucs->pending = 0;
397 break;
398 case HD_DEVICE_INIT_ACK: /* no reply expected */
399 ucs->pending = 0;
400 break;
401 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
402 * are handled separately and should never end up here
403 */
404 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700405 dev_warn(&ucs->interface->dev,
406 "unknown pending request 0x%02x cleared\n",
407 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800408 ucs->pending = 0;
409 }
410
411 if (!ucs->pending)
412 del_timer(&ucs->timer_ctrl);
413
414 spin_unlock_irqrestore(&ucs->lock, flags);
415}
416
417/* cmd_in_timeout
418 * timeout routine for command input request
419 * argument:
420 * controller state structure
421 */
422static void cmd_in_timeout(unsigned long data)
423{
424 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700425 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700426 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800427
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800428 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700429 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800430 return;
431 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800432
Tilman Schmidt06163f82006-06-26 00:25:33 -0700433 if (ucs->retry_cmd_in++ < BAS_RETRY) {
434 dev_notice(cs->dev, "control read: timeout, retry %d\n",
435 ucs->retry_cmd_in);
436 rc = atread_submit(cs, BAS_TIMEOUT);
437 if (rc >= 0 || rc == -ENODEV)
438 /* resubmitted or disconnected */
439 /* - bypass regular exit block */
440 return;
441 } else {
442 dev_err(cs->dev,
443 "control read: timeout, giving up after %d tries\n",
444 ucs->retry_cmd_in);
445 }
446 kfree(ucs->rcvbuf);
447 ucs->rcvbuf = NULL;
448 ucs->rcvbuf_size = 0;
449 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800450}
451
Tilman Schmidt73a88812006-04-22 02:35:30 -0700452/* set/clear bits in base connection state, return previous state
453 */
454inline static int update_basstate(struct bas_cardstate *ucs,
455 int set, int clear)
456{
457 unsigned long flags;
458 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800459
Tilman Schmidt73a88812006-04-22 02:35:30 -0700460 spin_lock_irqsave(&ucs->lock, flags);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800461 state = ucs->basstate;
462 ucs->basstate = (state & ~clear) | set;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700463 spin_unlock_irqrestore(&ucs->lock, flags);
464 return state;
465}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800466
Tilman Schmidt06163f82006-06-26 00:25:33 -0700467/* read_ctrl_callback
468 * USB completion handler for control pipe input
469 * called by the USB subsystem in interrupt context
470 * parameter:
471 * urb USB request block
472 * urb->context = inbuf structure for controller state
473 */
David Howells7d12e782006-10-05 14:55:46 +0100474static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700475{
476 struct inbuf_t *inbuf = urb->context;
477 struct cardstate *cs = inbuf->cs;
478 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800479 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700480 int have_data = 0;
481 unsigned numbytes;
482 int rc;
483
484 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800485 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700486
487 if (!ucs->rcvbuf_size) {
488 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
489 return;
490 }
491
492 del_timer(&ucs->timer_cmd_in);
493
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800494 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700495 case 0: /* normal completion */
496 numbytes = urb->actual_length;
497 if (unlikely(numbytes != ucs->rcvbuf_size)) {
498 dev_warn(cs->dev,
499 "control read: received %d chars, expected %d\n",
500 numbytes, ucs->rcvbuf_size);
501 if (numbytes > ucs->rcvbuf_size)
502 numbytes = ucs->rcvbuf_size;
503 }
504
505 /* copy received bytes to inbuf */
506 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
507
508 if (unlikely(numbytes < ucs->rcvbuf_size)) {
509 /* incomplete - resubmit for remaining bytes */
510 ucs->rcvbuf_size -= numbytes;
511 ucs->retry_cmd_in = 0;
512 rc = atread_submit(cs, BAS_TIMEOUT);
513 if (rc >= 0 || rc == -ENODEV)
514 /* resubmitted or disconnected */
515 /* - bypass regular exit block */
516 return;
517 error_reset(cs);
518 }
519 break;
520
521 case -ENOENT: /* cancelled */
522 case -ECONNRESET: /* cancelled (async) */
523 case -EINPROGRESS: /* pending */
524 case -ENODEV: /* device removed */
525 case -ESHUTDOWN: /* device shut down */
526 /* no action necessary */
527 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800528 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700529 break;
530
531 default: /* severe trouble */
532 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800533 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700534 if (ucs->retry_cmd_in++ < BAS_RETRY) {
535 dev_notice(cs->dev, "control read: retry %d\n",
536 ucs->retry_cmd_in);
537 rc = atread_submit(cs, BAS_TIMEOUT);
538 if (rc >= 0 || rc == -ENODEV)
539 /* resubmitted or disconnected */
540 /* - bypass regular exit block */
541 return;
542 } else {
543 dev_err(cs->dev,
544 "control read: giving up after %d tries\n",
545 ucs->retry_cmd_in);
546 }
547 error_reset(cs);
548 }
549
550 kfree(ucs->rcvbuf);
551 ucs->rcvbuf = NULL;
552 ucs->rcvbuf_size = 0;
553 if (have_data) {
554 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
555 gigaset_schedule_event(cs);
556 }
557}
558
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700560 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800561 * parameters:
562 * cs controller state structure
563 * timeout timeout in 1/10 sec., 0: none
564 * return value:
565 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800566 * -EBUSY if another request is pending
567 * any URB submission error code
568 */
569static int atread_submit(struct cardstate *cs, int timeout)
570{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700571 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800572 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800573 int ret;
574
Tilman Schmidt784d5852006-04-10 22:55:04 -0700575 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
576 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800577
Tilman Schmidt024fd292008-02-06 01:38:26 -0800578 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
579 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700580 dev_err(cs->dev,
581 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800582 return -EBUSY;
583 }
584
Tilman Schmidt024fd292008-02-06 01:38:26 -0800585 if (basstate & BS_SUSPEND) {
586 dev_notice(cs->dev,
587 "HD_READ_ATMESSAGE not submitted, "
588 "suspend in progress\n");
589 update_basstate(ucs, 0, BS_ATRDPEND);
590 /* treat like disconnect */
591 return -ENODEV;
592 }
593
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800594 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
595 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
596 ucs->dr_cmd_in.wValue = 0;
597 ucs->dr_cmd_in.wIndex = 0;
598 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
599 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700600 usb_rcvctrlpipe(ucs->udev, 0),
601 (unsigned char*) & ucs->dr_cmd_in,
602 ucs->rcvbuf, ucs->rcvbuf_size,
603 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800604
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800605 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700606 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700607 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700608 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800609 return ret;
610 }
611
612 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700613 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800614 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
615 ucs->timer_cmd_in.data = (unsigned long) cs;
616 ucs->timer_cmd_in.function = cmd_in_timeout;
617 add_timer(&ucs->timer_cmd_in);
618 }
619 return 0;
620}
621
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800622/* read_int_callback
623 * USB completion handler for interrupt pipe input
624 * called by the USB subsystem in interrupt context
625 * parameter:
626 * urb USB request block
627 * urb->context = controller state structure
628 */
David Howells7d12e782006-10-05 14:55:46 +0100629static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800630{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700631 struct cardstate *cs = urb->context;
632 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800634 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800635 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700636 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800637 unsigned l;
638 int channel;
639
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800640 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800641 case 0: /* success */
642 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700643 case -ENOENT: /* cancelled */
644 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800645 case -EINPROGRESS: /* pending */
646 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700647 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800648 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800649 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700650 case -ENODEV: /* device removed */
651 case -ESHUTDOWN: /* device shut down */
652 //FIXME use this as disconnect indicator?
653 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
654 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800655 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700656 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800657 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800658 //FIXME corrective action? resubmission always ok?
659 goto resubmit;
660 }
661
Tilman Schmidt73a88812006-04-22 02:35:30 -0700662 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700663 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700664 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
665 urb->actual_length);
666 goto resubmit;
667 }
668
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800669 l = (unsigned) ucs->int_in_buf[1] +
670 (((unsigned) ucs->int_in_buf[2]) << 8);
671
Tilman Schmidt784d5852006-04-10 22:55:04 -0700672 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
673 urb->actual_length, (int)ucs->int_in_buf[0], l,
674 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800675
676 channel = 0;
677
678 switch (ucs->int_in_buf[0]) {
679 case HD_DEVICE_INIT_OK:
680 update_basstate(ucs, BS_INIT, 0);
681 break;
682
683 case HD_READY_SEND_ATDATA:
684 del_timer(&ucs->timer_atrdy);
685 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
686 start_cbsend(cs);
687 break;
688
689 case HD_OPEN_B2CHANNEL_ACK:
690 ++channel;
691 case HD_OPEN_B1CHANNEL_ACK:
692 bcs = cs->bcs + channel;
693 update_basstate(ucs, BS_B1OPEN << channel, 0);
694 gigaset_bchannel_up(bcs);
695 break;
696
697 case HD_OPEN_ATCHANNEL_ACK:
698 update_basstate(ucs, BS_ATOPEN, 0);
699 start_cbsend(cs);
700 break;
701
702 case HD_CLOSE_B2CHANNEL_ACK:
703 ++channel;
704 case HD_CLOSE_B1CHANNEL_ACK:
705 bcs = cs->bcs + channel;
706 update_basstate(ucs, 0, BS_B1OPEN << channel);
707 stopurbs(bcs->hw.bas);
708 gigaset_bchannel_down(bcs);
709 break;
710
711 case HD_CLOSE_ATCHANNEL_ACK:
712 update_basstate(ucs, 0, BS_ATOPEN);
713 break;
714
715 case HD_B2_FLOW_CONTROL:
716 ++channel;
717 case HD_B1_FLOW_CONTROL:
718 bcs = cs->bcs + channel;
719 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700720 &bcs->hw.bas->corrbytes);
721 gig_dbg(DEBUG_ISO,
722 "Flow control (channel %d, sub %d): 0x%02x => %d",
723 channel, bcs->hw.bas->numsub, l,
724 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800725 break;
726
727 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
728 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700729 dev_warn(cs->dev,
730 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800731 break;
732 }
733 spin_lock_irqsave(&cs->lock, flags);
734 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700735 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700736 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700737 "receive AT data overrun, %d bytes lost\n",
738 ucs->rcvbuf_size);
739 kfree(ucs->rcvbuf);
740 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800741 }
742 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
743 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700744 dev_err(cs->dev, "out of memory receiving AT data\n");
745 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800746 break;
747 }
748 ucs->rcvbuf_size = l;
749 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700750 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800751 kfree(ucs->rcvbuf);
752 ucs->rcvbuf = NULL;
753 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700754 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700755 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700756 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700757 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700758 break;
759 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800760 }
761 spin_unlock_irqrestore(&cs->lock, flags);
762 break;
763
764 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700765 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800766 break;
767
768 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700769 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800770 break;
771
772 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700773 dev_warn(cs->dev,
774 "unknown Gigaset signal 0x%02x (%u) ignored\n",
775 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800776 }
777
778 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800779 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800780
781resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800782 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700783 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700784 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700785 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800786 error_reset(cs);
787 }
788}
789
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800790/* read_iso_callback
791 * USB completion handler for B channel isochronous input
792 * called by the USB subsystem in interrupt context
793 * parameter:
794 * urb USB request block of completed request
795 * urb->context = bc_state structure
796 */
David Howells7d12e782006-10-05 14:55:46 +0100797static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800798{
799 struct bc_state *bcs;
800 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800801 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802 unsigned long flags;
803 int i, rc;
804
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800805 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800806 if (unlikely(status == -ENOENT ||
807 status == -ECONNRESET ||
808 status == -EINPROGRESS ||
809 status == -ENODEV ||
810 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700811 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800812 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800813 return;
814 }
815
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700816 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800817 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800818
819 spin_lock_irqsave(&ubc->isoinlock, flags);
820 if (likely(ubc->isoindone == NULL)) {
821 /* pass URB to tasklet */
822 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800823 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000824 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800825 } else {
826 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800827 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800828 for (i = 0; i < BAS_NUMFRAMES; i++) {
829 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
830 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700831 urb->iso_frame_desc[i].status !=
832 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800833 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800834 urb->iso_frame_desc[i].status = 0;
835 urb->iso_frame_desc[i].actual_length = 0;
836 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800837 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700838 /* urb->dev is clobbered by USB subsystem */
839 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800840 urb->transfer_flags = URB_ISO_ASAP;
841 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700842 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
843 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800844 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700845 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700846 dev_err(bcs->cs->dev,
847 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700848 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800849 dump_urb(DEBUG_ISO, "isoc read", urb);
850 error_hangup(bcs);
851 }
852 }
853 }
854 spin_unlock_irqrestore(&ubc->isoinlock, flags);
855}
856
857/* write_iso_callback
858 * USB completion handler for B channel isochronous output
859 * called by the USB subsystem in interrupt context
860 * parameter:
861 * urb USB request block of completed request
862 * urb->context = isow_urbctx_t structure
863 */
David Howells7d12e782006-10-05 14:55:46 +0100864static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800865{
866 struct isow_urbctx_t *ucx;
867 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800868 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869 unsigned long flags;
870
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800871 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800872 if (unlikely(status == -ENOENT ||
873 status == -ECONNRESET ||
874 status == -EINPROGRESS ||
875 status == -ENODEV ||
876 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700877 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800878 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800879 return;
880 }
881
882 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700883 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800884 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800885 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800886
887 spin_lock_irqsave(&ubc->isooutlock, flags);
888 ubc->isooutovfl = ubc->isooutdone;
889 ubc->isooutdone = ucx;
890 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000891 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800892}
893
894/* starturbs
895 * prepare and submit USB request blocks for isochronous input and output
896 * argument:
897 * B channel control structure
898 * return value:
899 * 0 on success
900 * < 0 on error (no URBs submitted)
901 */
902static int starturbs(struct bc_state *bcs)
903{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700904 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800905 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800906 int j, k;
907 int rc;
908
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800909 /* initialize L2 reception */
910 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
911 bcs->inputstate |= INS_flag_hunt;
912
913 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800914 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800915 for (k = 0; k < BAS_INURBS; k++) {
916 urb = ubc->isoinurbs[k];
917 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800918 rc = -EFAULT;
919 goto error;
920 }
921
922 urb->dev = bcs->cs->hw.bas->udev;
923 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
924 urb->transfer_flags = URB_ISO_ASAP;
925 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
926 urb->transfer_buffer_length = BAS_INBUFSIZE;
927 urb->number_of_packets = BAS_NUMFRAMES;
928 urb->interval = BAS_FRAMETIME;
929 urb->complete = read_iso_callback;
930 urb->context = bcs;
931 for (j = 0; j < BAS_NUMFRAMES; j++) {
932 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
933 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
934 urb->iso_frame_desc[j].status = 0;
935 urb->iso_frame_desc[j].actual_length = 0;
936 }
937
938 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800939 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800941 }
942
943 /* initialize L2 transmission */
944 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
945
946 /* set up isochronous output URBs for flag idling */
947 for (k = 0; k < BAS_OUTURBS; ++k) {
948 urb = ubc->isoouturbs[k].urb;
949 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800950 rc = -EFAULT;
951 goto error;
952 }
953 urb->dev = bcs->cs->hw.bas->udev;
954 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
958 urb->number_of_packets = BAS_NUMFRAMES;
959 urb->interval = BAS_FRAMETIME;
960 urb->complete = write_iso_callback;
961 urb->context = &ubc->isoouturbs[k];
962 for (j = 0; j < BAS_NUMFRAMES; ++j) {
963 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
964 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
965 urb->iso_frame_desc[j].status = 0;
966 urb->iso_frame_desc[j].actual_length = 0;
967 }
968 ubc->isoouturbs[k].limit = -1;
969 }
970
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800971 /* keep one URB free, submit the others */
972 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800973 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800974 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700975 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800976 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800977 }
978 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800979 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800980 ubc->isooutdone = ubc->isooutovfl = NULL;
981 return 0;
982 error:
983 stopurbs(ubc);
984 return rc;
985}
986
987/* stopurbs
988 * cancel the USB request blocks for isochronous input and output
989 * errors are silently ignored
990 * argument:
991 * B channel control structure
992 */
993static void stopurbs(struct bas_bc_state *ubc)
994{
995 int k, rc;
996
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800997 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800998
999 for (k = 0; k < BAS_INURBS; ++k) {
1000 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001001 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001002 "%s: isoc input URB %d unlinked, result = %s",
1003 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001004 }
1005
1006 for (k = 0; k < BAS_OUTURBS; ++k) {
1007 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001008 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001009 "%s: isoc output URB %d unlinked, result = %s",
1010 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001011 }
1012}
1013
1014/* Isochronous Write - Bottom Half */
1015/* =============================== */
1016
1017/* submit_iso_write_urb
1018 * fill and submit the next isochronous write URB
1019 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001020 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001021 * return value:
1022 * number of frames submitted in URB
1023 * 0 if URB not submitted because no data available (isooutbuf busy)
1024 * error code < 0 on error
1025 */
1026static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1027{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001028 struct urb *urb = ucx->urb;
1029 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030 struct usb_iso_packet_descriptor *ifd;
1031 int corrbytes, nframe, rc;
1032
Tilman Schmidt784d5852006-04-10 22:55:04 -07001033 /* urb->dev is clobbered by USB subsystem */
1034 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001035 urb->transfer_flags = URB_ISO_ASAP;
1036 urb->transfer_buffer = ubc->isooutbuf->data;
1037 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1038
1039 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1040 ifd = &urb->iso_frame_desc[nframe];
1041
1042 /* compute frame length according to flow control */
1043 ifd->length = BAS_NORMFRAME;
1044 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001045 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1046 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001047 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1048 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1049 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1050 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1051 ifd->length += corrbytes;
1052 atomic_add(-corrbytes, &ubc->corrbytes);
1053 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001054
1055 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001056 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1057 if (rc < 0) {
1058 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001059 gig_dbg(DEBUG_ISO,
1060 "%s: buffer busy at frame %d",
1061 __func__, nframe);
1062 /* tasklet will be restarted from
1063 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001064 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001065 dev_err(ucx->bcs->cs->dev,
1066 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001067 __func__, rc, nframe);
1068 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001069 }
1070 break;
1071 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001072 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001073 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001074 ifd->status = 0;
1075 ifd->actual_length = 0;
1076 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001077 if (unlikely(nframe == 0))
1078 return 0; /* no data to send */
1079 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001080
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001081 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001082 if (unlikely(rc)) {
1083 if (rc == -ENODEV)
1084 /* device removed - give up silently */
1085 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1086 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001087 dev_err(ucx->bcs->cs->dev,
1088 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001089 get_usb_rcmsg(rc));
1090 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001091 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001092 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001093 return nframe;
1094}
1095
1096/* write_iso_tasklet
1097 * tasklet scheduled when an isochronous output URB from the Gigaset device
1098 * has completed
1099 * parameter:
1100 * data B channel state structure
1101 */
1102static void write_iso_tasklet(unsigned long data)
1103{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001104 struct bc_state *bcs = (struct bc_state *) data;
1105 struct bas_bc_state *ubc = bcs->hw.bas;
1106 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001107 struct isow_urbctx_t *done, *next, *ovfl;
1108 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001109 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001110 struct usb_iso_packet_descriptor *ifd;
1111 int offset;
1112 unsigned long flags;
1113 int i;
1114 struct sk_buff *skb;
1115 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001116 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001117
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 /* loop while completed URBs arrive in time */
1119 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001120 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001121 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001122 return;
1123 }
1124
1125 /* retrieve completed URBs */
1126 spin_lock_irqsave(&ubc->isooutlock, flags);
1127 done = ubc->isooutdone;
1128 ubc->isooutdone = NULL;
1129 ovfl = ubc->isooutovfl;
1130 ubc->isooutovfl = NULL;
1131 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1132 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001133 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001134 error_hangup(bcs);
1135 break;
1136 }
1137 if (!done)
1138 break;
1139
1140 /* submit free URB if available */
1141 spin_lock_irqsave(&ubc->isooutlock, flags);
1142 next = ubc->isooutfree;
1143 ubc->isooutfree = NULL;
1144 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1145 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001146 rc = submit_iso_write_urb(next);
1147 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001148 /* could not submit URB, put it back */
1149 spin_lock_irqsave(&ubc->isooutlock, flags);
1150 if (ubc->isooutfree == NULL) {
1151 ubc->isooutfree = next;
1152 next = NULL;
1153 }
1154 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1155 if (next) {
1156 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001157 dev_err(cs->dev,
1158 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001159 error_hangup(bcs);
1160 }
1161 }
1162 }
1163
1164 /* process completed URB */
1165 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001166 status = done->status;
1167 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001168 case -EXDEV: /* partial completion */
1169 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1170 __func__);
1171 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001172 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001173 /* inspect individual frames
1174 * assumptions (for lack of documentation):
1175 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001176 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001177 * - all following frames are not sent at all
1178 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001179 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001180 for (i = 0; i < BAS_NUMFRAMES; i++) {
1181 ifd = &urb->iso_frame_desc[i];
1182 if (ifd->status ||
1183 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001184 dev_warn(cs->dev,
1185 "isochronous write: frame %d: %s, "
1186 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001187 i, get_usb_statmsg(ifd->status),
1188 ifd->actual_length, ifd->length);
1189 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001190 ifd->actual_length)
1191 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001192 break;
1193 }
1194 }
1195#ifdef CONFIG_GIGASET_DEBUG
1196 /* check assumption on remaining frames */
1197 for (; i < BAS_NUMFRAMES; i++) {
1198 ifd = &urb->iso_frame_desc[i];
1199 if (ifd->status != -EINPROGRESS
1200 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001201 dev_warn(cs->dev,
1202 "isochronous write: frame %d: %s, "
1203 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001204 i, get_usb_statmsg(ifd->status),
1205 ifd->actual_length, ifd->length);
1206 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001207 ifd->actual_length)
1208 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001209 break;
1210 }
1211 }
1212#endif
1213 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001214 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001215 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001216 error_hangup(bcs);
1217 break;
1218 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001219 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001220 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001221 }
1222
1223 /* mark the write buffer area covered by this URB as free */
1224 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001225 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001226
1227 /* mark URB as free */
1228 spin_lock_irqsave(&ubc->isooutlock, flags);
1229 next = ubc->isooutfree;
1230 ubc->isooutfree = done;
1231 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1232 if (next) {
1233 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001234 rc = submit_iso_write_urb(next);
1235 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001236 /* couldn't submit */
1237 error_hangup(bcs);
1238 }
1239 }
1240 }
1241
1242 /* process queued SKBs */
1243 while ((skb = skb_dequeue(&bcs->squeue))) {
1244 /* copy to output buffer, doing L2 encapsulation */
1245 len = skb->len;
1246 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1247 /* insufficient buffer space, push back onto queue */
1248 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001249 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1250 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001251 break;
1252 }
1253 skb_pull(skb, len);
1254 gigaset_skb_sent(bcs, skb);
1255 dev_kfree_skb_any(skb);
1256 }
1257}
1258
1259/* Isochronous Read - Bottom Half */
1260/* ============================== */
1261
1262/* read_iso_tasklet
1263 * tasklet scheduled when an isochronous input URB from the Gigaset device
1264 * has completed
1265 * parameter:
1266 * data B channel state structure
1267 */
1268static void read_iso_tasklet(unsigned long data)
1269{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001270 struct bc_state *bcs = (struct bc_state *) data;
1271 struct bas_bc_state *ubc = bcs->hw.bas;
1272 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001273 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001274 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001275 char *rcvbuf;
1276 unsigned long flags;
1277 int totleft, numbytes, offset, frame, rc;
1278
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 /* loop while more completed URBs arrive in the meantime */
1280 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001281 /* retrieve URB */
1282 spin_lock_irqsave(&ubc->isoinlock, flags);
1283 if (!(urb = ubc->isoindone)) {
1284 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1285 return;
1286 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001287 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001288 ubc->isoindone = NULL;
1289 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001290 dev_warn(cs->dev,
1291 "isochronous read overrun, "
1292 "dropped URB with status: %s, %d bytes lost\n",
1293 get_usb_statmsg(ubc->loststatus),
1294 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001295 ubc->loststatus = -EINPROGRESS;
1296 }
1297 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1298
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001299 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001300 gig_dbg(DEBUG_ISO,
1301 "%s: channel not running, "
1302 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001303 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001304 return;
1305 }
1306
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001307 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001308 case 0: /* normal completion */
1309 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001310 case -EXDEV: /* inspect individual frames
1311 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001312 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1313 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001314 break;
1315 case -ENOENT:
1316 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001317 case -EINPROGRESS:
1318 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001319 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001320 continue; /* -> skip */
1321 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001322 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001323 error_hangup(bcs);
1324 continue; /* -> skip */
1325 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001326 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001327 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001328 goto error;
1329 }
1330
1331 rcvbuf = urb->transfer_buffer;
1332 totleft = urb->actual_length;
1333 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001334 numbytes = urb->iso_frame_desc[frame].actual_length;
1335 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001336 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001337 "isochronous read: frame %d[%d]: %s\n",
1338 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001339 get_usb_statmsg(
1340 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001341 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 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001347 dev_warn(cs->dev,
1348 "isochronous read: frame %d: "
1349 "numbytes (%d) > totleft (%d)\n",
1350 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001351 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001352 }
1353 offset = urb->iso_frame_desc[frame].offset;
1354 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001355 dev_warn(cs->dev,
1356 "isochronous read: frame %d: "
1357 "offset (%d) + numbytes (%d) "
1358 "> BAS_INBUFSIZE\n",
1359 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001360 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001361 }
1362 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1363 totleft -= numbytes;
1364 }
1365 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001366 dev_warn(cs->dev,
1367 "isochronous read: %d data bytes missing\n",
1368 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001369
1370 error:
1371 /* URB processed, resubmit */
1372 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1373 urb->iso_frame_desc[frame].status = 0;
1374 urb->iso_frame_desc[frame].actual_length = 0;
1375 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001376 /* urb->dev is clobbered by USB subsystem */
1377 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001378 urb->transfer_flags = URB_ISO_ASAP;
1379 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001380 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001381 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001382 dev_err(cs->dev,
1383 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001384 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001385 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1386 error_hangup(bcs);
1387 }
1388 }
1389}
1390
1391/* Channel Operations */
1392/* ================== */
1393
1394/* req_timeout
1395 * timeout routine for control output request
1396 * argument:
1397 * B channel control structure
1398 */
1399static void req_timeout(unsigned long data)
1400{
1401 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001402 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001403 int pending;
1404 unsigned long flags;
1405
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001406 check_pending(ucs);
1407
1408 spin_lock_irqsave(&ucs->lock, flags);
1409 pending = ucs->pending;
1410 ucs->pending = 0;
1411 spin_unlock_irqrestore(&ucs->lock, flags);
1412
1413 switch (pending) {
1414 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001415 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001416 break;
1417
1418 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001419 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001420 error_reset(bcs->cs);
1421 break;
1422
1423 case HD_OPEN_B2CHANNEL:
1424 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001425 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1426 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001427 error_hangup(bcs);
1428 break;
1429
1430 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001431 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001432 break;
1433
1434 case HD_CLOSE_B2CHANNEL:
1435 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001436 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1437 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001438 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001439 break;
1440
1441 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001442 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1443 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001444 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001445
1446 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001447}
1448
1449/* write_ctrl_callback
1450 * USB completion handler for control pipe output
1451 * called by the USB subsystem in interrupt context
1452 * parameter:
1453 * urb USB request block of completed request
1454 * urb->context = hardware specific controller state structure
1455 */
David Howells7d12e782006-10-05 14:55:46 +01001456static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001457{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001458 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001459 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001460 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001461 unsigned long flags;
1462
Tilman Schmidt06163f82006-06-26 00:25:33 -07001463 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001464 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001465 case 0: /* normal completion */
1466 spin_lock_irqsave(&ucs->lock, flags);
1467 switch (ucs->pending) {
1468 case HD_DEVICE_INIT_ACK: /* no reply expected */
1469 del_timer(&ucs->timer_ctrl);
1470 ucs->pending = 0;
1471 break;
1472 }
1473 spin_unlock_irqrestore(&ucs->lock, flags);
1474 return;
1475
1476 case -ENOENT: /* cancelled */
1477 case -ECONNRESET: /* cancelled (async) */
1478 case -EINPROGRESS: /* pending */
1479 case -ENODEV: /* device removed */
1480 case -ESHUTDOWN: /* device shut down */
1481 /* ignore silently */
1482 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001483 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001484 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001485
1486 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001487 /* don't retry if suspend requested */
1488 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001489 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001490 dev_err(&ucs->interface->dev,
1491 "control request 0x%02x failed: %s\n",
1492 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001493 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001494 break; /* give up */
1495 }
1496 dev_notice(&ucs->interface->dev,
1497 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001498 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001499 ucs->retry_ctrl);
1500 /* urb->dev is clobbered by USB subsystem */
1501 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001502 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001503 if (unlikely(rc)) {
1504 dev_err(&ucs->interface->dev,
1505 "could not resubmit request 0x%02x: %s\n",
1506 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1507 break;
1508 }
1509 /* resubmitted */
1510 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001511 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001512
1513 /* failed, clear pending request */
1514 spin_lock_irqsave(&ucs->lock, flags);
1515 del_timer(&ucs->timer_ctrl);
1516 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001517 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001518 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001519}
1520
1521/* req_submit
1522 * submit a control output request without message buffer to the Gigaset base
1523 * and optionally start a timeout
1524 * parameters:
1525 * bcs B channel control structure
1526 * req control request code (HD_*)
1527 * val control request parameter value (set to 0 if unused)
1528 * timeout timeout in seconds (0: no timeout)
1529 * return value:
1530 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001531 * -EBUSY if another request is pending
1532 * any URB submission error code
1533 */
1534static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1535{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001536 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001537 int ret;
1538 unsigned long flags;
1539
Tilman Schmidt784d5852006-04-10 22:55:04 -07001540 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001541
1542 spin_lock_irqsave(&ucs->lock, flags);
1543 if (ucs->pending) {
1544 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001545 dev_err(bcs->cs->dev,
1546 "submission of request 0x%02x failed: "
1547 "request 0x%02x still pending\n",
1548 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001549 return -EBUSY;
1550 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001551
1552 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1553 ucs->dr_ctrl.bRequest = req;
1554 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1555 ucs->dr_ctrl.wIndex = 0;
1556 ucs->dr_ctrl.wLength = 0;
1557 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001558 usb_sndctrlpipe(ucs->udev, 0),
1559 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1560 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001561 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001562 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001563 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001564 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001565 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001566 spin_unlock_irqrestore(&ucs->lock, flags);
1567 return ret;
1568 }
1569 ucs->pending = req;
1570
1571 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001572 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001573 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1574 ucs->timer_ctrl.data = (unsigned long) bcs;
1575 ucs->timer_ctrl.function = req_timeout;
1576 add_timer(&ucs->timer_ctrl);
1577 }
1578
1579 spin_unlock_irqrestore(&ucs->lock, flags);
1580 return 0;
1581}
1582
1583/* gigaset_init_bchannel
1584 * called by common.c to connect a B channel
1585 * initialize isochronous I/O and tell the Gigaset base to open the channel
1586 * argument:
1587 * B channel control structure
1588 * return value:
1589 * 0 on success, error code < 0 on error
1590 */
1591static int gigaset_init_bchannel(struct bc_state *bcs)
1592{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001593 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001594 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001595 unsigned long flags;
1596
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001597 spin_lock_irqsave(&cs->lock, flags);
1598 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001599 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001600 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001601 return -ENODEV;
1602 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001603
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001604 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001605 dev_notice(cs->dev,
1606 "not starting isochronous I/O, "
1607 "suspend in progress\n");
1608 spin_unlock_irqrestore(&cs->lock, flags);
1609 return -EHOSTUNREACH;
1610 }
1611
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001612 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001613 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001614 "could not start isochronous I/O for channel B%d: %s\n",
1615 bcs->channel + 1,
1616 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1617 if (ret != -ENODEV)
1618 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001619 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001620 return ret;
1621 }
1622
1623 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1624 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001625 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001626 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001627 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001628 if (ret != -ENODEV)
1629 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001630 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001631
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001632 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001633 return ret;
1634}
1635
1636/* gigaset_close_bchannel
1637 * called by common.c to disconnect a B channel
1638 * tell the Gigaset base to close the channel
1639 * stopping isochronous I/O and LL notification will be done when the
1640 * acknowledgement for the close arrives
1641 * argument:
1642 * B channel control structure
1643 * return value:
1644 * 0 on success, error code < 0 on error
1645 */
1646static int gigaset_close_bchannel(struct bc_state *bcs)
1647{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001648 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001649 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001650 unsigned long flags;
1651
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001652 spin_lock_irqsave(&cs->lock, flags);
1653 if (unlikely(!cs->connected)) {
1654 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001655 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1656 return -ENODEV;
1657 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001658
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001659 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001660 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001661 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001662 gigaset_bchannel_down(bcs);
1663 return 0;
1664 }
1665
Tilman Schmidt73a88812006-04-22 02:35:30 -07001666 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001667 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1668 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001669 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001670 bcs->channel + 1);
1671
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001672 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001673 return ret;
1674}
1675
1676/* Device Operations */
1677/* ================= */
1678
1679/* complete_cb
1680 * unqueue first command buffer from queue, waking any sleepers
1681 * must be called with cs->cmdlock held
1682 * parameter:
1683 * cs controller state structure
1684 */
1685static void complete_cb(struct cardstate *cs)
1686{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001687 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001688
1689 /* unqueue completed buffer */
1690 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001691 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1692 "write_command: sent %u bytes, %u left",
1693 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001694 if ((cs->cmdbuf = cb->next) != NULL) {
1695 cs->cmdbuf->prev = NULL;
1696 cs->curlen = cs->cmdbuf->len;
1697 } else {
1698 cs->lastcmdbuf = NULL;
1699 cs->curlen = 0;
1700 }
1701
1702 if (cb->wake_tasklet)
1703 tasklet_schedule(cb->wake_tasklet);
1704
1705 kfree(cb);
1706}
1707
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001708/* write_command_callback
1709 * USB completion handler for AT command transmission
1710 * called by the USB subsystem in interrupt context
1711 * parameter:
1712 * urb USB request block of completed request
1713 * urb->context = controller state structure
1714 */
David Howells7d12e782006-10-05 14:55:46 +01001715static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001716{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001717 struct cardstate *cs = urb->context;
1718 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001719 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001720 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001721
Tilman Schmidt73a88812006-04-22 02:35:30 -07001722 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001723 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001724
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001725 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001726 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001727 case 0: /* normal completion */
1728 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001729 case -ENOENT: /* cancelled */
1730 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001731 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001732 case -ENODEV: /* device removed */
1733 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001734 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001735 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001736 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001737 return;
1738 default: /* any failure */
1739 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001740 dev_warn(cs->dev,
1741 "command write: %s, "
1742 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001743 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001744 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001745 break;
1746 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001747 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001748 dev_warn(cs->dev,
1749 "command write: %s, "
1750 "won't retry - suspend requested\n",
1751 get_usb_statmsg(status));
1752 break;
1753 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001754 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001755 dev_warn(cs->dev,
1756 "command write: %s, "
1757 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001758 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001759 break;
1760 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001761 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001762 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001763 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1764 /* resubmitted - bypass regular exit block */
1765 return;
1766 /* command send failed, assume base still waiting */
1767 update_basstate(ucs, BS_ATREADY, 0);
1768 }
1769
1770 spin_lock_irqsave(&cs->cmdlock, flags);
1771 if (cs->cmdbuf != NULL)
1772 complete_cb(cs);
1773 spin_unlock_irqrestore(&cs->cmdlock, flags);
1774}
1775
1776/* atrdy_timeout
1777 * timeout routine for AT command transmission
1778 * argument:
1779 * controller state structure
1780 */
1781static void atrdy_timeout(unsigned long data)
1782{
1783 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001784 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001785
Tilman Schmidt784d5852006-04-10 22:55:04 -07001786 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001787
1788 /* fake the missing signal - what else can I do? */
1789 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1790 start_cbsend(cs);
1791}
1792
1793/* atwrite_submit
1794 * submit an HD_WRITE_ATMESSAGE command URB
1795 * parameters:
1796 * cs controller state structure
1797 * buf buffer containing command to send
1798 * len length of command to send
1799 * return value:
1800 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001801 * -EBUSY if another request is pending
1802 * any URB submission error code
1803 */
1804static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1805{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001806 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001807 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001808
Tilman Schmidt784d5852006-04-10 22:55:04 -07001809 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001810
Tilman Schmidt73a88812006-04-22 02:35:30 -07001811 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001812 dev_err(cs->dev,
1813 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001814 return -EBUSY;
1815 }
1816
1817 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1818 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1819 ucs->dr_cmd_out.wValue = 0;
1820 ucs->dr_cmd_out.wIndex = 0;
1821 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1822 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1823 usb_sndctrlpipe(ucs->udev, 0),
1824 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1825 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001826 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001827 if (unlikely(rc)) {
1828 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001829 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001830 get_usb_rcmsg(rc));
1831 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001832 }
1833
Tilman Schmidt73a88812006-04-22 02:35:30 -07001834 /* submitted successfully, start timeout if necessary */
1835 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001836 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1837 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001838 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1839 ucs->timer_atrdy.data = (unsigned long) cs;
1840 ucs->timer_atrdy.function = atrdy_timeout;
1841 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001842 }
1843 return 0;
1844}
1845
1846/* start_cbsend
1847 * start transmission of AT command queue if necessary
1848 * parameter:
1849 * cs controller state structure
1850 * return value:
1851 * 0 on success
1852 * error code < 0 on error
1853 */
1854static int start_cbsend(struct cardstate *cs)
1855{
1856 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001857 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001858 unsigned long flags;
1859 int rc;
1860 int retval = 0;
1861
Tilman Schmidt024fd292008-02-06 01:38:26 -08001862 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001863 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001864 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1865 return -EHOSTUNREACH;
1866 }
1867
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001868 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001869 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001870 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001871 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1872 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001873 /* flush command queue */
1874 spin_lock_irqsave(&cs->cmdlock, flags);
1875 while (cs->cmdbuf != NULL)
1876 complete_cb(cs);
1877 spin_unlock_irqrestore(&cs->cmdlock, flags);
1878 }
1879 return rc;
1880 }
1881
1882 /* try to send first command in queue */
1883 spin_lock_irqsave(&cs->cmdlock, flags);
1884
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001885 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001886 ucs->retry_cmd_out = 0;
1887 rc = atwrite_submit(cs, cb->buf, cb->len);
1888 if (unlikely(rc)) {
1889 retval = rc;
1890 complete_cb(cs);
1891 }
1892 }
1893
1894 spin_unlock_irqrestore(&cs->cmdlock, flags);
1895 return retval;
1896}
1897
1898/* gigaset_write_cmd
1899 * This function is called by the device independent part of the driver
1900 * to transmit an AT command string to the Gigaset device.
1901 * It encapsulates the device specific method for transmission over the
1902 * direct USB connection to the base.
1903 * The command string is added to the queue of commands to send, and
1904 * USB transmission is started if necessary.
1905 * parameters:
1906 * cs controller state structure
1907 * buf command string to send
1908 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001909 * wake_tasklet tasklet to run when transmission is completed
1910 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001911 * return value:
1912 * number of bytes queued on success
1913 * error code < 0 on error
1914 */
1915static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001916 const unsigned char *buf, int len,
1917 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001918{
1919 struct cmdbuf_t *cb;
1920 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001921 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001922
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001923 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001924 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001925 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001926
Tilman Schmidt39f07222006-12-13 00:33:52 -08001927 if (len <= 0) {
1928 /* nothing to do */
1929 rc = 0;
1930 goto notqueued;
1931 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001932
1933 if (len > IF_WRITEBUF)
1934 len = IF_WRITEBUF;
1935 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001936 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001937 rc = -ENOMEM;
1938 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001939 }
1940
1941 memcpy(cb->buf, buf, len);
1942 cb->len = len;
1943 cb->offset = 0;
1944 cb->next = NULL;
1945 cb->wake_tasklet = wake_tasklet;
1946
1947 spin_lock_irqsave(&cs->cmdlock, flags);
1948 cb->prev = cs->lastcmdbuf;
1949 if (cs->lastcmdbuf)
1950 cs->lastcmdbuf->next = cb;
1951 else {
1952 cs->cmdbuf = cb;
1953 cs->curlen = len;
1954 }
1955 cs->cmdbytes += len;
1956 cs->lastcmdbuf = cb;
1957 spin_unlock_irqrestore(&cs->cmdlock, flags);
1958
Tilman Schmidt73a88812006-04-22 02:35:30 -07001959 spin_lock_irqsave(&cs->lock, flags);
1960 if (unlikely(!cs->connected)) {
1961 spin_unlock_irqrestore(&cs->lock, flags);
1962 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001963 /* flush command queue */
1964 spin_lock_irqsave(&cs->cmdlock, flags);
1965 while (cs->cmdbuf != NULL)
1966 complete_cb(cs);
1967 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001968 return -ENODEV;
1969 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001970 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001971 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001972 return rc < 0 ? rc : len;
1973
1974notqueued: /* request handled without queuing */
1975 if (wake_tasklet)
1976 tasklet_schedule(wake_tasklet);
1977 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001978}
1979
1980/* gigaset_write_room
1981 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001982 * return number of characters the driver will accept to be written via
1983 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001984 * parameter:
1985 * controller state structure
1986 * return value:
1987 * number of characters
1988 */
1989static int gigaset_write_room(struct cardstate *cs)
1990{
1991 return IF_WRITEBUF;
1992}
1993
1994/* gigaset_chars_in_buffer
1995 * tty_driver.chars_in_buffer interface routine
1996 * return number of characters waiting to be sent
1997 * parameter:
1998 * controller state structure
1999 * return value:
2000 * number of characters
2001 */
2002static int gigaset_chars_in_buffer(struct cardstate *cs)
2003{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002004 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002005}
2006
2007/* gigaset_brkchars
2008 * implementation of ioctl(GIGASET_BRKCHARS)
2009 * parameter:
2010 * controller state structure
2011 * return value:
2012 * -EINVAL (unimplemented function)
2013 */
2014static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2015{
2016 return -EINVAL;
2017}
2018
2019
2020/* Device Initialization/Shutdown */
2021/* ============================== */
2022
2023/* Free hardware dependent part of the B channel structure
2024 * parameter:
2025 * bcs B channel structure
2026 * return value:
2027 * !=0 on success
2028 */
2029static int gigaset_freebcshw(struct bc_state *bcs)
2030{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002031 struct bas_bc_state *ubc = bcs->hw.bas;
2032 int i;
2033
2034 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002035 return 0;
2036
Tilman Schmidt73a88812006-04-22 02:35:30 -07002037 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002038 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002039 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2040 for (i = 0; i < BAS_OUTURBS; ++i) {
2041 usb_kill_urb(ubc->isoouturbs[i].urb);
2042 usb_free_urb(ubc->isoouturbs[i].urb);
2043 }
2044 for (i = 0; i < BAS_INURBS; ++i) {
2045 usb_kill_urb(ubc->isoinurbs[i]);
2046 usb_free_urb(ubc->isoinurbs[i]);
2047 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002048 tasklet_kill(&ubc->sent_tasklet);
2049 tasklet_kill(&ubc->rcvd_tasklet);
2050 kfree(ubc->isooutbuf);
2051 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002052 bcs->hw.bas = NULL;
2053 return 1;
2054}
2055
2056/* Initialize hardware dependent part of the B channel structure
2057 * parameter:
2058 * bcs B channel structure
2059 * return value:
2060 * !=0 on success
2061 */
2062static int gigaset_initbcshw(struct bc_state *bcs)
2063{
2064 int i;
2065 struct bas_bc_state *ubc;
2066
2067 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2068 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002069 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002070 return 0;
2071 }
2072
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002073 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002074 atomic_set(&ubc->corrbytes, 0);
2075 spin_lock_init(&ubc->isooutlock);
2076 for (i = 0; i < BAS_OUTURBS; ++i) {
2077 ubc->isoouturbs[i].urb = NULL;
2078 ubc->isoouturbs[i].bcs = bcs;
2079 }
2080 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2081 ubc->numsub = 0;
2082 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002083 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002084 kfree(ubc);
2085 bcs->hw.bas = NULL;
2086 return 0;
2087 }
2088 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002089 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002090
2091 spin_lock_init(&ubc->isoinlock);
2092 for (i = 0; i < BAS_INURBS; ++i)
2093 ubc->isoinurbs[i] = NULL;
2094 ubc->isoindone = NULL;
2095 ubc->loststatus = -EINPROGRESS;
2096 ubc->isoinlost = 0;
2097 ubc->seqlen = 0;
2098 ubc->inbyte = 0;
2099 ubc->inbits = 0;
2100 ubc->goodbytes = 0;
2101 ubc->alignerrs = 0;
2102 ubc->fcserrs = 0;
2103 ubc->frameerrs = 0;
2104 ubc->giants = 0;
2105 ubc->runts = 0;
2106 ubc->aborts = 0;
2107 ubc->shared0s = 0;
2108 ubc->stolen0s = 0;
2109 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002110 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111 return 1;
2112}
2113
2114static void gigaset_reinitbcshw(struct bc_state *bcs)
2115{
2116 struct bas_bc_state *ubc = bcs->hw.bas;
2117
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002118 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002119 atomic_set(&bcs->hw.bas->corrbytes, 0);
2120 bcs->hw.bas->numsub = 0;
2121 spin_lock_init(&ubc->isooutlock);
2122 spin_lock_init(&ubc->isoinlock);
2123 ubc->loststatus = -EINPROGRESS;
2124}
2125
2126static void gigaset_freecshw(struct cardstate *cs)
2127{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002128 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002129 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002130 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002131 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002132}
2133
2134static int gigaset_initcshw(struct cardstate *cs)
2135{
2136 struct bas_cardstate *ucs;
2137
2138 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002139 if (!ucs) {
2140 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002141 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002142 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002143 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2144 if (!ucs->int_in_buf) {
2145 kfree(ucs);
2146 pr_err("out of memory\n");
2147 return 0;
2148 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002149
2150 ucs->urb_cmd_in = NULL;
2151 ucs->urb_cmd_out = NULL;
2152 ucs->rcvbuf = NULL;
2153 ucs->rcvbuf_size = 0;
2154
2155 spin_lock_init(&ucs->lock);
2156 ucs->pending = 0;
2157
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002158 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002159 init_timer(&ucs->timer_ctrl);
2160 init_timer(&ucs->timer_atrdy);
2161 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002162 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002163
2164 return 1;
2165}
2166
2167/* freeurbs
2168 * unlink and deallocate all URBs unconditionally
2169 * caller must make sure that no commands are still in progress
2170 * parameter:
2171 * cs controller state structure
2172 */
2173static void freeurbs(struct cardstate *cs)
2174{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002175 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002176 struct bas_bc_state *ubc;
2177 int i, j;
2178
Tilman Schmidt39f07222006-12-13 00:33:52 -08002179 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002180 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002181 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002182 for (i = 0; i < BAS_OUTURBS; ++i) {
2183 usb_kill_urb(ubc->isoouturbs[i].urb);
2184 usb_free_urb(ubc->isoouturbs[i].urb);
2185 ubc->isoouturbs[i].urb = NULL;
2186 }
2187 for (i = 0; i < BAS_INURBS; ++i) {
2188 usb_kill_urb(ubc->isoinurbs[i]);
2189 usb_free_urb(ubc->isoinurbs[i]);
2190 ubc->isoinurbs[i] = NULL;
2191 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002192 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002193 usb_kill_urb(ucs->urb_int_in);
2194 usb_free_urb(ucs->urb_int_in);
2195 ucs->urb_int_in = NULL;
2196 usb_kill_urb(ucs->urb_cmd_out);
2197 usb_free_urb(ucs->urb_cmd_out);
2198 ucs->urb_cmd_out = NULL;
2199 usb_kill_urb(ucs->urb_cmd_in);
2200 usb_free_urb(ucs->urb_cmd_in);
2201 ucs->urb_cmd_in = NULL;
2202 usb_kill_urb(ucs->urb_ctrl);
2203 usb_free_urb(ucs->urb_ctrl);
2204 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002205}
2206
2207/* gigaset_probe
2208 * This function is called when a new USB device is connected.
2209 * It checks whether the new device is handled by this driver.
2210 */
2211static int gigaset_probe(struct usb_interface *interface,
2212 const struct usb_device_id *id)
2213{
2214 struct usb_host_interface *hostif;
2215 struct usb_device *udev = interface_to_usbdev(interface);
2216 struct cardstate *cs = NULL;
2217 struct bas_cardstate *ucs = NULL;
2218 struct bas_bc_state *ubc;
2219 struct usb_endpoint_descriptor *endpoint;
2220 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002221 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002222
Tilman Schmidt784d5852006-04-10 22:55:04 -07002223 gig_dbg(DEBUG_ANY,
2224 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2225 __func__, le16_to_cpu(udev->descriptor.idVendor),
2226 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002227
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002228 /* set required alternate setting */
2229 hostif = interface->cur_altsetting;
2230 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002231 gig_dbg(DEBUG_ANY,
2232 "%s: wrong alternate setting %d - trying to switch",
2233 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002234 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002235 dev_warn(&udev->dev, "usb_set_interface failed, "
2236 "device %d interface %d altsetting %d\n",
2237 udev->devnum, hostif->desc.bInterfaceNumber,
2238 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002239 return -ENODEV;
2240 }
2241 hostif = interface->cur_altsetting;
2242 }
2243
2244 /* Reject application specific interfaces
2245 */
2246 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002247 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2248 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002249 return -ENODEV;
2250 }
2251
Tilman Schmidt784d5852006-04-10 22:55:04 -07002252 dev_info(&udev->dev,
2253 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2254 __func__, le16_to_cpu(udev->descriptor.idVendor),
2255 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002256
Tilman Schmidte468c042008-02-06 01:38:29 -08002257 /* allocate memory for our device state and intialize it */
2258 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2259 GIGASET_MODULENAME);
2260 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002261 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002262 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002263
2264 /* save off device structure ptrs for later use */
2265 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002266 ucs->udev = udev;
2267 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002268 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002269
2270 /* allocate URBs:
2271 * - one for the interrupt pipe
2272 * - three for the different uses of the default control pipe
2273 * - three for each isochronous pipe
2274 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002275 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2276 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2277 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2278 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002279 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002280
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002281 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002282 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002283 for (i = 0; i < BAS_OUTURBS; ++i)
2284 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002285 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002286 goto allocerr;
2287 for (i = 0; i < BAS_INURBS; ++i)
2288 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002289 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002290 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002291 }
2292
2293 ucs->rcvbuf = NULL;
2294 ucs->rcvbuf_size = 0;
2295
2296 /* Fill the interrupt urb and send it to the core */
2297 endpoint = &hostif->endpoint[0].desc;
2298 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002299 usb_rcvintpipe(udev,
2300 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002301 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002302 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002303 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002304 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002305 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002306 goto error;
2307 }
2308
2309 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002310 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002311 goto error;
2312
2313 /* tell common part that the device is ready */
2314 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002315 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002316
2317 /* save address of controller structure */
2318 usb_set_intfdata(interface, cs);
2319
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002320 if (!gigaset_start(cs))
2321 goto error;
2322
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323 return 0;
2324
Tilman Schmidt73a88812006-04-22 02:35:30 -07002325allocerr:
2326 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002327error:
2328 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002329 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002330 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002331 return -ENODEV;
2332}
2333
2334/* gigaset_disconnect
2335 * This function is called when the Gigaset base is unplugged.
2336 */
2337static void gigaset_disconnect(struct usb_interface *interface)
2338{
2339 struct cardstate *cs;
2340 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002341 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002342
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002343 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002344
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002345 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002346
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002347 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002348
2349 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002350 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002351
2352 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002353 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002354 gigaset_bchannel_down(cs->bcs + j);
2355
2356 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002357 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002358
2359 /* stop timers and URBs, free ressources */
2360 del_timer_sync(&ucs->timer_ctrl);
2361 del_timer_sync(&ucs->timer_atrdy);
2362 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002363 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002364 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002365 kfree(ucs->rcvbuf);
2366 ucs->rcvbuf = NULL;
2367 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002368 usb_put_dev(ucs->udev);
2369 ucs->interface = NULL;
2370 ucs->udev = NULL;
2371 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002372 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002373}
2374
Tilman Schmidt024fd292008-02-06 01:38:26 -08002375/* gigaset_suspend
2376 * This function is called before the USB connection is suspended.
2377 */
2378static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2379{
2380 struct cardstate *cs = usb_get_intfdata(intf);
2381 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002382 int rc;
2383
2384 /* set suspend flag; this stops AT command/response traffic */
2385 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2386 gig_dbg(DEBUG_SUSPEND, "already suspended");
2387 return 0;
2388 }
2389
2390 /* wait a bit for blocking conditions to go away */
2391 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002392 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002393 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2394 BAS_TIMEOUT*HZ/10);
2395 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2396
2397 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002398 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002399 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002400 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002401 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002402 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002403 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002404 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002405 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002406 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002407 dev_warn(cs->dev, " sending AT command\n");
2408 update_basstate(ucs, 0, BS_SUSPEND);
2409 return -EBUSY;
2410 }
2411
2412 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002413 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002414 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2415 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2416 if (rc) {
2417 update_basstate(ucs, 0, BS_SUSPEND);
2418 return rc;
2419 }
2420 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2421 BAS_TIMEOUT*HZ/10);
2422 /* in case of timeout, proceed anyway */
2423 }
2424
2425 /* kill all URBs and timers that might still be pending */
2426 usb_kill_urb(ucs->urb_ctrl);
2427 usb_kill_urb(ucs->urb_int_in);
2428 del_timer_sync(&ucs->timer_ctrl);
2429
2430 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2431 return 0;
2432}
2433
2434/* gigaset_resume
2435 * This function is called after the USB connection has been resumed.
2436 */
2437static int gigaset_resume(struct usb_interface *intf)
2438{
2439 struct cardstate *cs = usb_get_intfdata(intf);
2440 struct bas_cardstate *ucs = cs->hw.bas;
2441 int rc;
2442
2443 /* resubmit interrupt URB for spontaneous messages from base */
2444 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2445 if (rc) {
2446 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2447 get_usb_rcmsg(rc));
2448 return rc;
2449 }
2450
2451 /* clear suspend flag to reallow activity */
2452 update_basstate(ucs, 0, BS_SUSPEND);
2453
2454 gig_dbg(DEBUG_SUSPEND, "resume complete");
2455 return 0;
2456}
2457
2458/* gigaset_pre_reset
2459 * This function is called before the USB connection is reset.
2460 */
2461static int gigaset_pre_reset(struct usb_interface *intf)
2462{
2463 /* handle just like suspend */
2464 return gigaset_suspend(intf, PMSG_ON);
2465}
2466
2467/* gigaset_post_reset
2468 * This function is called after the USB connection has been reset.
2469 */
2470static int gigaset_post_reset(struct usb_interface *intf)
2471{
2472 /* FIXME: send HD_DEVICE_INIT_ACK? */
2473
2474 /* resume operations */
2475 return gigaset_resume(intf);
2476}
2477
2478
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002479static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002480 gigaset_write_cmd,
2481 gigaset_write_room,
2482 gigaset_chars_in_buffer,
2483 gigaset_brkchars,
2484 gigaset_init_bchannel,
2485 gigaset_close_bchannel,
2486 gigaset_initbcshw,
2487 gigaset_freebcshw,
2488 gigaset_reinitbcshw,
2489 gigaset_initcshw,
2490 gigaset_freecshw,
2491 gigaset_set_modem_ctrl,
2492 gigaset_baud_rate,
2493 gigaset_set_line_ctrl,
2494 gigaset_isoc_send_skb,
2495 gigaset_isoc_input,
2496};
2497
2498/* bas_gigaset_init
2499 * This function is called after the kernel module is loaded.
2500 */
2501static int __init bas_gigaset_init(void)
2502{
2503 int result;
2504
2505 /* allocate memory for our driver state and intialize it */
2506 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002507 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002508 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002509 goto error;
2510
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002511 /* register this driver with the USB subsystem */
2512 result = usb_register(&gigaset_usb_driver);
2513 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002514 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002515 goto error;
2516 }
2517
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002518 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002519 return 0;
2520
Tilman Schmidte468c042008-02-06 01:38:29 -08002521error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002522 if (driver)
2523 gigaset_freedriver(driver);
2524 driver = NULL;
2525 return -1;
2526}
2527
2528/* bas_gigaset_exit
2529 * This function is called before the kernel module is unloaded.
2530 */
2531static void __exit bas_gigaset_exit(void)
2532{
Tilman Schmidte468c042008-02-06 01:38:29 -08002533 struct bas_cardstate *ucs;
2534 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002535
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002536 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002537 * => no gigaset_start any more
2538 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002539
Tilman Schmidte468c042008-02-06 01:38:29 -08002540 /* stop all connected devices */
2541 for (i = 0; i < driver->minors; i++) {
2542 if (gigaset_shutdown(driver->cs + i) < 0)
2543 continue; /* no device */
2544 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002545
Tilman Schmidte468c042008-02-06 01:38:29 -08002546 /* close all still open channels */
2547 ucs = driver->cs[i].hw.bas;
2548 if (ucs->basstate & BS_B1OPEN) {
2549 gig_dbg(DEBUG_INIT, "closing B1 channel");
2550 usb_control_msg(ucs->udev,
2551 usb_sndctrlpipe(ucs->udev, 0),
2552 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2553 0, 0, NULL, 0, BAS_TIMEOUT);
2554 }
2555 if (ucs->basstate & BS_B2OPEN) {
2556 gig_dbg(DEBUG_INIT, "closing B2 channel");
2557 usb_control_msg(ucs->udev,
2558 usb_sndctrlpipe(ucs->udev, 0),
2559 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2560 0, 0, NULL, 0, BAS_TIMEOUT);
2561 }
2562 if (ucs->basstate & BS_ATOPEN) {
2563 gig_dbg(DEBUG_INIT, "closing AT channel");
2564 usb_control_msg(ucs->udev,
2565 usb_sndctrlpipe(ucs->udev, 0),
2566 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2567 0, 0, NULL, 0, BAS_TIMEOUT);
2568 }
2569 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002570 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002571
2572 /* deregister this driver with the USB subsystem */
2573 usb_deregister(&gigaset_usb_driver);
2574 /* this will call the disconnect-callback */
2575 /* from now on, no disconnect/probe callback should be running */
2576
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002577 gigaset_freedriver(driver);
2578 driver = NULL;
2579}
2580
2581
2582module_init(bas_gigaset_init);
2583module_exit(bas_gigaset_exit);
2584
2585MODULE_AUTHOR(DRIVER_AUTHOR);
2586MODULE_DESCRIPTION(DRIVER_DESC);
2587MODULE_LICENSE("GPL");