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