Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / usb / atm / speedtch.c
1 /******************************************************************************
2  *  speedtch.c  -  Alcatel SpeedTouch USB xDSL modem driver
3  *
4  *  Copyright (C) 2001, Alcatel
5  *  Copyright (C) 2003, Duncan Sands
6  *  Copyright (C) 2004, David Woodhouse
7  *
8  *  Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the Free
12  *  Software Foundation; either version 2 of the License, or (at your option)
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  *  more details.
19  *
20  *  You should have received a copy of the GNU General Public License along with
21  *  this program; if not, write to the Free Software Foundation, Inc., 59
22  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  ******************************************************************************/
25
26 #include <asm/page.h>
27 #include <linux/device.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/gfp.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/slab.h>
36 #include <linux/stat.h>
37 #include <linux/timer.h>
38 #include <linux/types.h>
39 #include <linux/usb_ch9.h>
40 #include <linux/workqueue.h>
41
42 #include "usbatm.h"
43
44 #define DRIVER_AUTHOR   "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
45 #define DRIVER_VERSION  "1.10"
46 #define DRIVER_DESC     "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
47
48 static const char speedtch_driver_name[] = "speedtch";
49
50 #define CTRL_TIMEOUT 2000       /* milliseconds */
51 #define DATA_TIMEOUT 2000       /* milliseconds */
52
53 #define OFFSET_7        0               /* size 1 */
54 #define OFFSET_b        1               /* size 8 */
55 #define OFFSET_d        9               /* size 4 */
56 #define OFFSET_e        13              /* size 1 */
57 #define OFFSET_f        14              /* size 1 */
58 #define TOTAL           15
59
60 #define SIZE_7          1
61 #define SIZE_b          8
62 #define SIZE_d          4
63 #define SIZE_e          1
64 #define SIZE_f          1
65
66 #define MIN_POLL_DELAY          5000    /* milliseconds */
67 #define MAX_POLL_DELAY          60000   /* milliseconds */
68
69 #define RESUBMIT_DELAY          1000    /* milliseconds */
70
71 #define DEFAULT_BULK_ALTSETTING 1
72 #define DEFAULT_ISOC_ALTSETTING 3
73 #define DEFAULT_DL_512_FIRST    0
74 #define DEFAULT_ENABLE_ISOC     0
75 #define DEFAULT_SW_BUFFERING    0
76
77 static unsigned int altsetting = 0; /* zero means: use the default */
78 static int dl_512_first = DEFAULT_DL_512_FIRST;
79 static int enable_isoc = DEFAULT_ENABLE_ISOC;
80 static int sw_buffering = DEFAULT_SW_BUFFERING;
81
82 module_param(altsetting, uint, S_IRUGO | S_IWUSR);
83 MODULE_PARM_DESC(altsetting,
84                 "Alternative setting for data interface (bulk_default: "
85                 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
86                 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
87
88 module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
89 MODULE_PARM_DESC(dl_512_first,
90                  "Read 512 bytes before sending firmware (default: "
91                  __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
92
93 module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
94 MODULE_PARM_DESC(enable_isoc,
95                 "Use isochronous transfers if available (default: "
96                 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
97
98 module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
99 MODULE_PARM_DESC(sw_buffering,
100                  "Enable software buffering (default: "
101                  __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
102
103 #define INTERFACE_DATA          1
104 #define ENDPOINT_INT            0x81
105 #define ENDPOINT_BULK_DATA      0x07
106 #define ENDPOINT_ISOC_DATA      0x07
107 #define ENDPOINT_FIRMWARE       0x05
108
109 #define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
110
111 struct speedtch_instance_data {
112         struct usbatm_data *usbatm;
113
114         unsigned int altsetting;
115
116         struct work_struct status_checker;
117
118         unsigned char last_status;
119
120         int poll_delay; /* milliseconds */
121
122         struct timer_list resubmit_timer;
123         struct urb *int_urb;
124         unsigned char int_data[16];
125
126         unsigned char scratch_buffer[TOTAL];
127 };
128
129 /***************
130 **  firmware  **
131 ***************/
132
133 static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
134 {
135         struct usbatm_data *usbatm = instance->usbatm;
136         struct usb_device *usb_dev = usbatm->usb_dev;
137         int ret;
138
139         ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
140                               0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
141         if (ret < 0)
142                 usb_warn(usbatm,
143                          "%sabling SW buffering: usb_control_msg returned %d\n",
144                          state ? "En" : "Dis", ret);
145         else
146                 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
147 }
148
149 static void speedtch_test_sequence(struct speedtch_instance_data *instance)
150 {
151         struct usbatm_data *usbatm = instance->usbatm;
152         struct usb_device *usb_dev = usbatm->usb_dev;
153         unsigned char *buf = instance->scratch_buffer;
154         int ret;
155
156         /* URB 147 */
157         buf[0] = 0x1c;
158         buf[1] = 0x50;
159         ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
160                               0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
161         if (ret < 0)
162                 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
163
164         /* URB 148 */
165         buf[0] = 0x32;
166         buf[1] = 0x00;
167         ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
168                               0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
169         if (ret < 0)
170                 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
171
172         /* URB 149 */
173         buf[0] = 0x01;
174         buf[1] = 0x00;
175         buf[2] = 0x01;
176         ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
177                               0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
178         if (ret < 0)
179                 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
180
181         /* URB 150 */
182         buf[0] = 0x01;
183         buf[1] = 0x00;
184         buf[2] = 0x01;
185         ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
186                               0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
187         if (ret < 0)
188                 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
189 }
190
191 static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
192                                      const struct firmware *fw1,
193                                      const struct firmware *fw2)
194 {
195         unsigned char *buffer;
196         struct usbatm_data *usbatm = instance->usbatm;
197         struct usb_interface *intf;
198         struct usb_device *usb_dev = usbatm->usb_dev;
199         int actual_length;
200         int ret = 0;
201         int offset;
202
203         usb_dbg(usbatm, "%s entered\n", __func__);
204
205         if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
206                 ret = -ENOMEM;
207                 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
208                 goto out;
209         }
210
211         if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
212                 ret = -ENODEV;
213                 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
214                 goto out_free;
215         }
216
217         /* URB 7 */
218         if (dl_512_first) {     /* some modems need a read before writing the firmware */
219                 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
220                                    buffer, 0x200, &actual_length, 2000);
221
222                 if (ret < 0 && ret != -ETIMEDOUT)
223                         usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
224                 else
225                         usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
226         }
227
228         /* URB 8 : both leds are static green */
229         for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
230                 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
231                 memcpy(buffer, fw1->data + offset, thislen);
232
233                 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
234                                    buffer, thislen, &actual_length, DATA_TIMEOUT);
235
236                 if (ret < 0) {
237                         usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
238                         goto out_free;
239                 }
240                 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
241         }
242
243         /* USB led blinking green, ADSL led off */
244
245         /* URB 11 */
246         ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
247                            buffer, 0x200, &actual_length, DATA_TIMEOUT);
248
249         if (ret < 0) {
250                 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
251                 goto out_free;
252         }
253         usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
254
255         /* URBs 12 to 139 - USB led blinking green, ADSL led off */
256         for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
257                 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
258                 memcpy(buffer, fw2->data + offset, thislen);
259
260                 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
261                                    buffer, thislen, &actual_length, DATA_TIMEOUT);
262
263                 if (ret < 0) {
264                         usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
265                         goto out_free;
266                 }
267         }
268         usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
269
270         /* USB led static green, ADSL led static red */
271
272         /* URB 142 */
273         ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
274                            buffer, 0x200, &actual_length, DATA_TIMEOUT);
275
276         if (ret < 0) {
277                 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
278                 goto out_free;
279         }
280
281         /* success */
282         usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
283
284         /* Delay to allow firmware to start up. We can do this here
285            because we're in our own kernel thread anyway. */
286         msleep_interruptible(1000);
287
288         if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->altsetting)) < 0) {
289                 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->altsetting, ret);
290                 goto out_free;
291         }
292
293         /* Enable software buffering, if requested */
294         if (sw_buffering)
295                 speedtch_set_swbuff(instance, 1);
296
297         /* Magic spell; don't ask us what this does */
298         speedtch_test_sequence(instance);
299
300         ret = 0;
301
302 out_free:
303         free_page((unsigned long)buffer);
304 out:
305         return ret;
306 }
307
308 static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
309                                   int phase, const struct firmware **fw_p)
310 {
311         struct device *dev = &intf->dev;
312         const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
313         const u8 major_revision = bcdDevice >> 8;
314         const u8 minor_revision = bcdDevice & 0xff;
315         char buf[24];
316
317         sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
318         usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
319
320         if (request_firmware(fw_p, buf, dev)) {
321                 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
322                 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
323
324                 if (request_firmware(fw_p, buf, dev)) {
325                         sprintf(buf, "speedtch-%d.bin", phase);
326                         usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
327
328                         if (request_firmware(fw_p, buf, dev)) {
329                                 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
330                                 return -ENOENT;
331                         }
332                 }
333         }
334
335         usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
336
337         return 0;
338 }
339
340 static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
341 {
342         const struct firmware *fw1, *fw2;
343         struct speedtch_instance_data *instance = usbatm->driver_data;
344         int ret;
345
346         if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
347                 return ret;
348
349         if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
350                 release_firmware(fw1);
351                 return ret;
352         }
353
354         if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
355                 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
356
357         release_firmware(fw2);
358         release_firmware(fw1);
359
360         return ret;
361 }
362
363
364 /**********
365 **  ATM  **
366 **********/
367
368 static int speedtch_read_status(struct speedtch_instance_data *instance)
369 {
370         struct usbatm_data *usbatm = instance->usbatm;
371         struct usb_device *usb_dev = usbatm->usb_dev;
372         unsigned char *buf = instance->scratch_buffer;
373         int ret;
374
375         memset(buf, 0, TOTAL);
376
377         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
378                               0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
379                               CTRL_TIMEOUT);
380         if (ret < 0) {
381                 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
382                 return ret;
383         }
384
385         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
386                               0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
387                               CTRL_TIMEOUT);
388         if (ret < 0) {
389                 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
390                 return ret;
391         }
392
393         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
394                               0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
395                               CTRL_TIMEOUT);
396         if (ret < 0) {
397                 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
398                 return ret;
399         }
400
401         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
402                               0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
403                               CTRL_TIMEOUT);
404         if (ret < 0) {
405                 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
406                 return ret;
407         }
408
409         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
410                               0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
411                               CTRL_TIMEOUT);
412         if (ret < 0) {
413                 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
414                 return ret;
415         }
416
417         return 0;
418 }
419
420 static int speedtch_start_synchro(struct speedtch_instance_data *instance)
421 {
422         struct usbatm_data *usbatm = instance->usbatm;
423         struct usb_device *usb_dev = usbatm->usb_dev;
424         unsigned char *buf = instance->scratch_buffer;
425         int ret;
426
427         atm_dbg(usbatm, "%s entered\n", __func__);
428
429         memset(buf, 0, 2);
430
431         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
432                               0x12, 0xc0, 0x04, 0x00,
433                               buf, 2, CTRL_TIMEOUT);
434
435         if (ret < 0)
436                 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
437         else
438                 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
439                         __func__, ret, buf[0], buf[1]);
440
441         return ret;
442 }
443
444 static void speedtch_check_status(struct speedtch_instance_data *instance)
445 {
446         struct usbatm_data *usbatm = instance->usbatm;
447         struct atm_dev *atm_dev = usbatm->atm_dev;
448         unsigned char *buf = instance->scratch_buffer;
449         int down_speed, up_speed, ret;
450         unsigned char status;
451
452 #ifdef VERBOSE_DEBUG
453         atm_dbg(usbatm, "%s entered\n", __func__);
454 #endif
455
456         ret = speedtch_read_status(instance);
457         if (ret < 0) {
458                 atm_warn(usbatm, "error %d fetching device status\n", ret);
459                 instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
460                 return;
461         }
462
463         instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
464
465         status = buf[OFFSET_7];
466
467         if ((status != instance->last_status) || !status) {
468                 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
469
470                 switch (status) {
471                 case 0:
472                         atm_dev->signal = ATM_PHY_SIG_LOST;
473                         if (instance->last_status)
474                                 atm_info(usbatm, "ADSL line is down\n");
475                         /* It may never resync again unless we ask it to... */
476                         ret = speedtch_start_synchro(instance);
477                         break;
478
479                 case 0x08:
480                         atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
481                         atm_info(usbatm, "ADSL line is blocked?\n");
482                         break;
483
484                 case 0x10:
485                         atm_dev->signal = ATM_PHY_SIG_LOST;
486                         atm_info(usbatm, "ADSL line is synchronising\n");
487                         break;
488
489                 case 0x20:
490                         down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
491                                 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
492                         up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
493                                 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
494
495                         if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
496                                 down_speed >>= 16;
497                                 up_speed >>= 16;
498                         }
499
500                         atm_dev->link_rate = down_speed * 1000 / 424;
501                         atm_dev->signal = ATM_PHY_SIG_FOUND;
502
503                         atm_info(usbatm,
504                                  "ADSL line is up (%d kb/s down | %d kb/s up)\n",
505                                  down_speed, up_speed);
506                         break;
507
508                 default:
509                         atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
510                         atm_info(usbatm, "unknown line state %02x\n", status);
511                         break;
512                 }
513
514                 instance->last_status = status;
515         }
516 }
517
518 static void speedtch_status_poll(unsigned long data)
519 {
520         struct speedtch_instance_data *instance = (void *)data;
521
522         schedule_work(&instance->status_checker);
523
524         /* The following check is racy, but the race is harmless */
525         if (instance->poll_delay < MAX_POLL_DELAY)
526                 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
527         else
528                 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
529 }
530
531 static void speedtch_resubmit_int(unsigned long data)
532 {
533         struct speedtch_instance_data *instance = (void *)data;
534         struct urb *int_urb = instance->int_urb;
535         int ret;
536
537         atm_dbg(instance->usbatm, "%s entered\n", __func__);
538
539         if (int_urb) {
540                 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
541                 if (!ret)
542                         schedule_work(&instance->status_checker);
543                 else {
544                         atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
545                         mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
546                 }
547         }
548 }
549
550 static void speedtch_handle_int(struct urb *int_urb, struct pt_regs *regs)
551 {
552         struct speedtch_instance_data *instance = int_urb->context;
553         struct usbatm_data *usbatm = instance->usbatm;
554         unsigned int count = int_urb->actual_length;
555         int ret = int_urb->status;
556
557         /* The magic interrupt for "up state" */
558         static const unsigned char up_int[6]   = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
559         /* The magic interrupt for "down state" */
560         static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
561
562         atm_dbg(usbatm, "%s entered\n", __func__);
563
564         if (ret < 0) {
565                 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, ret);
566                 goto fail;
567         }
568
569         if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
570                 del_timer(&instance->status_checker.timer);
571                 atm_info(usbatm, "DSL line goes up\n");
572         } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
573                 atm_info(usbatm, "DSL line goes down\n");
574         } else {
575                 int i;
576
577                 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
578                 for (i = 0; i < count; i++)
579                         printk(" %02x", instance->int_data[i]);
580                 printk("\n");
581                 goto fail;
582         }
583
584         if ((int_urb = instance->int_urb)) {
585                 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
586                 schedule_work(&instance->status_checker);
587                 if (ret < 0) {
588                         atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
589                         goto fail;
590                 }
591         }
592
593         return;
594
595 fail:
596         if ((int_urb = instance->int_urb))
597                 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
598 }
599
600 static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
601 {
602         struct usb_device *usb_dev = usbatm->usb_dev;
603         struct speedtch_instance_data *instance = usbatm->driver_data;
604         int i, ret;
605         unsigned char mac_str[13];
606
607         atm_dbg(usbatm, "%s entered\n", __func__);
608
609         /* Set MAC address, it is stored in the serial number */
610         memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
611         if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
612                 for (i = 0; i < 6; i++)
613                         atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
614         }
615
616         /* Start modem synchronisation */
617         ret = speedtch_start_synchro(instance);
618
619         /* Set up interrupt endpoint */
620         if (instance->int_urb) {
621                 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
622                 if (ret < 0) {
623                         /* Doesn't matter; we'll poll anyway */
624                         atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
625                         usb_free_urb(instance->int_urb);
626                         instance->int_urb = NULL;
627                 }
628         }
629
630         /* Start status polling */
631         mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
632
633         return 0;
634 }
635
636 static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
637 {
638         struct speedtch_instance_data *instance = usbatm->driver_data;
639         struct urb *int_urb = instance->int_urb;
640
641         atm_dbg(usbatm, "%s entered\n", __func__);
642
643         del_timer_sync(&instance->status_checker.timer);
644
645         /*
646          * Since resubmit_timer and int_urb can schedule themselves and
647          * each other, shutting them down correctly takes some care
648          */
649         instance->int_urb = NULL; /* signal shutdown */
650         mb();
651         usb_kill_urb(int_urb);
652         del_timer_sync(&instance->resubmit_timer);
653         /*
654          * At this point, speedtch_handle_int and speedtch_resubmit_int
655          * can run or be running, but instance->int_urb == NULL means that
656          * they will not reschedule
657          */
658         usb_kill_urb(int_urb);
659         del_timer_sync(&instance->resubmit_timer);
660         usb_free_urb(int_urb);
661
662         flush_scheduled_work();
663 }
664
665
666 /**********
667 **  USB  **
668 **********/
669
670 static struct usb_device_id speedtch_usb_ids[] = {
671         {USB_DEVICE(0x06b9, 0x4061)},
672         {}
673 };
674
675 MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
676
677 static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
678
679 static struct usb_driver speedtch_usb_driver = {
680         .name           = speedtch_driver_name,
681         .probe          = speedtch_usb_probe,
682         .disconnect     = usbatm_usb_disconnect,
683         .id_table       = speedtch_usb_ids
684 };
685
686 static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) {
687         struct usb_interface *cur_intf;
688         int i;
689
690         for(i = 0; i < num_interfaces; i++)
691                 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
692                         usb_set_intfdata(cur_intf, NULL);
693                         usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
694                 }
695 }
696
697 static int speedtch_bind(struct usbatm_data *usbatm,
698                          struct usb_interface *intf,
699                          const struct usb_device_id *id)
700 {
701         struct usb_device *usb_dev = interface_to_usbdev(intf);
702         struct usb_interface *cur_intf, *data_intf;
703         struct speedtch_instance_data *instance;
704         int ifnum = intf->altsetting->desc.bInterfaceNumber;
705         int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
706         int i, ret;
707         int use_isoc;
708
709         usb_dbg(usbatm, "%s entered\n", __func__);
710
711         /* sanity checks */
712
713         if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
714                 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
715                 return -ENODEV;
716         }
717
718         if (!(data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA))) {
719                 usb_err(usbatm, "%s: data interface not found!\n", __func__);
720                 return -ENODEV;
721         }
722
723         /* claim all interfaces */
724
725         for (i=0; i < num_interfaces; i++) {
726                 cur_intf = usb_ifnum_to_if(usb_dev, i);
727
728                 if ((i != ifnum) && cur_intf) {
729                         ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
730
731                         if (ret < 0) {
732                                 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
733                                 speedtch_release_interfaces(usb_dev, i);
734                                 return ret;
735                         }
736                 }
737         }
738
739         instance = kzalloc(sizeof(*instance), GFP_KERNEL);
740
741         if (!instance) {
742                 usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
743                 ret = -ENOMEM;
744                 goto fail_release;
745         }
746
747         instance->usbatm = usbatm;
748
749         /* altsetting and enable_isoc may change at any moment, so take a snapshot */
750         instance->altsetting = altsetting;
751         use_isoc = enable_isoc;
752
753         if (instance->altsetting)
754                 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->altsetting)) < 0) {
755                         usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->altsetting, ret);
756                         instance->altsetting = 0; /* fall back to default */
757                 }
758
759         if (!instance->altsetting && use_isoc)
760                 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
761                         usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
762                         use_isoc = 0; /* fall back to bulk */
763                 }
764
765         if (use_isoc) {
766                 const struct usb_host_interface *desc = data_intf->cur_altsetting;
767                 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
768                 int i;
769
770                 use_isoc = 0; /* fall back to bulk if endpoint not found */
771
772                 for (i=0; i<desc->desc.bNumEndpoints; i++) {
773                         const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
774
775                         if ((endpoint_desc->bEndpointAddress == target_address)) {
776                                 use_isoc = (endpoint_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
777                                         USB_ENDPOINT_XFER_ISOC;
778                                 break;
779                         }
780                 }
781
782                 if (!use_isoc)
783                         usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
784         }
785
786         if (!use_isoc && !instance->altsetting)
787                 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
788                         usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
789                         goto fail_free;
790                 }
791
792         if (!instance->altsetting)
793                 instance->altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
794
795         usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
796
797         INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance);
798
799         instance->status_checker.timer.function = speedtch_status_poll;
800         instance->status_checker.timer.data = (unsigned long)instance;
801         instance->last_status = 0xff;
802         instance->poll_delay = MIN_POLL_DELAY;
803
804         init_timer(&instance->resubmit_timer);
805         instance->resubmit_timer.function = speedtch_resubmit_int;
806         instance->resubmit_timer.data = (unsigned long)instance;
807
808         instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
809
810         if (instance->int_urb)
811                 usb_fill_int_urb(instance->int_urb, usb_dev,
812                                  usb_rcvintpipe(usb_dev, ENDPOINT_INT),
813                                  instance->int_data, sizeof(instance->int_data),
814                                  speedtch_handle_int, instance, 50);
815         else
816                 usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
817
818         /* check whether the modem already seems to be alive */
819         ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
820                               0x12, 0xc0, 0x07, 0x00,
821                               instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
822
823         usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
824
825         usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
826
827         if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
828                 if ((ret = usb_reset_device(usb_dev)) < 0) {
829                         usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
830                         goto fail_free;
831                 }
832
833         usbatm->driver_data = instance;
834
835         return 0;
836
837 fail_free:
838         usb_free_urb(instance->int_urb);
839         kfree(instance);
840 fail_release:
841         speedtch_release_interfaces(usb_dev, num_interfaces);
842         return ret;
843 }
844
845 static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
846 {
847         struct usb_device *usb_dev = interface_to_usbdev(intf);
848         struct speedtch_instance_data *instance = usbatm->driver_data;
849
850         usb_dbg(usbatm, "%s entered\n", __func__);
851
852         speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
853         usb_free_urb(instance->int_urb);
854         kfree(instance);
855 }
856
857
858 /***********
859 **  init  **
860 ***********/
861
862 static struct usbatm_driver speedtch_usbatm_driver = {
863         .driver_name    = speedtch_driver_name,
864         .bind           = speedtch_bind,
865         .heavy_init     = speedtch_heavy_init,
866         .unbind         = speedtch_unbind,
867         .atm_start      = speedtch_atm_start,
868         .atm_stop       = speedtch_atm_stop,
869         .bulk_in        = ENDPOINT_BULK_DATA,
870         .bulk_out       = ENDPOINT_BULK_DATA,
871         .isoc_in        = ENDPOINT_ISOC_DATA
872 };
873
874 static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
875 {
876         return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
877 }
878
879 static int __init speedtch_usb_init(void)
880 {
881         dbg("%s: driver version %s", __func__, DRIVER_VERSION);
882
883         return usb_register(&speedtch_usb_driver);
884 }
885
886 static void __exit speedtch_usb_cleanup(void)
887 {
888         dbg("%s", __func__);
889
890         usb_deregister(&speedtch_usb_driver);
891 }
892
893 module_init(speedtch_usb_init);
894 module_exit(speedtch_usb_cleanup);
895
896 MODULE_AUTHOR(DRIVER_AUTHOR);
897 MODULE_DESCRIPTION(DRIVER_DESC);
898 MODULE_LICENSE("GPL");
899 MODULE_VERSION(DRIVER_VERSION);