blob: 5e3d7b9a78a7f3e7576c525cc70348e8d7b8e958 [file] [log] [blame]
Duncan Sands1b0e6142005-05-11 20:19:29 +02001/******************************************************************************
2 * cxacru.c - driver for USB ADSL modems based on
3 * Conexant AccessRunner chipset
4 *
5 * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan
6 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
Simon Arlott6a02c9962007-04-26 00:38:05 -07007 * Copyright (C) 2007 Simon Arlott
Duncan Sands1b0e6142005-05-11 20:19:29 +02008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 ******************************************************************************/
24
25/*
26 * Credit is due for Josep Comas, who created the original patch to speedtch.c
27 * to support the different padding used by the AccessRunner (now generalized
28 * into usbatm), and the userspace firmware loading utility.
29 */
30
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/kernel.h>
34#include <linux/timer.h>
35#include <linux/errno.h>
36#include <linux/slab.h>
37#include <linux/init.h>
Simon Arlottfa70fe42007-03-06 02:47:45 -080038#include <linux/device.h>
Duncan Sands1b0e6142005-05-11 20:19:29 +020039#include <linux/firmware.h>
Arjan van de Venab3c81f2006-01-13 15:52:55 +010040#include <linux/mutex.h>
Al Virofd05e722008-04-28 07:00:16 +010041#include <asm/unaligned.h>
Duncan Sands1b0e6142005-05-11 20:19:29 +020042
43#include "usbatm.h"
44
Simon Arlottfa70fe42007-03-06 02:47:45 -080045#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands, Simon Arlott"
46#define DRIVER_VERSION "0.3"
Duncan Sands1b0e6142005-05-11 20:19:29 +020047#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver"
48
49static const char cxacru_driver_name[] = "cxacru";
50
51#define CXACRU_EP_CMD 0x01 /* Bulk/interrupt in/out */
52#define CXACRU_EP_DATA 0x02 /* Bulk in/out */
53
54#define CMD_PACKET_SIZE 64 /* Should be maxpacket(ep)? */
55
56/* Addresses */
57#define PLLFCLK_ADDR 0x00350068
58#define PLLBCLK_ADDR 0x0035006c
59#define SDRAMEN_ADDR 0x00350010
60#define FW_ADDR 0x00801000
61#define BR_ADDR 0x00180600
62#define SIG_ADDR 0x00180500
63#define BR_STACK_ADDR 0x00187f10
64
65/* Values */
66#define SDRAM_ENA 0x1
67
68#define CMD_TIMEOUT 2000 /* msecs */
Simon Arlottfa70fe42007-03-06 02:47:45 -080069#define POLL_INTERVAL 1 /* secs */
Duncan Sands1b0e6142005-05-11 20:19:29 +020070
71/* commands for interaction with the modem through the control channel before
72 * firmware is loaded */
73enum cxacru_fw_request {
74 FW_CMD_ERR,
75 FW_GET_VER,
76 FW_READ_MEM,
77 FW_WRITE_MEM,
78 FW_RMW_MEM,
79 FW_CHECKSUM_MEM,
80 FW_GOTO_MEM,
81};
82
83/* commands for interaction with the modem through the control channel once
84 * firmware is loaded */
85enum cxacru_cm_request {
86 CM_REQUEST_UNDEFINED = 0x80,
87 CM_REQUEST_TEST,
88 CM_REQUEST_CHIP_GET_MAC_ADDRESS,
89 CM_REQUEST_CHIP_GET_DP_VERSIONS,
90 CM_REQUEST_CHIP_ADSL_LINE_START,
91 CM_REQUEST_CHIP_ADSL_LINE_STOP,
92 CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
93 CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
94 CM_REQUEST_CARD_INFO_GET,
95 CM_REQUEST_CARD_DATA_GET,
96 CM_REQUEST_CARD_DATA_SET,
97 CM_REQUEST_COMMAND_HW_IO,
98 CM_REQUEST_INTERFACE_HW_IO,
99 CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
100 CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
101 CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
102 CM_REQUEST_CARD_GET_STATUS,
103 CM_REQUEST_CARD_GET_MAC_ADDRESS,
104 CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
105 CM_REQUEST_MAX,
106};
107
Simon Arlottc68bb0d2009-11-21 15:12:21 +0000108/* commands for interaction with the flash memory
109 *
110 * read: response is the contents of the first 60 bytes of flash memory
111 * write: request contains the 60 bytes of data to write to flash memory
112 * response is the contents of the first 60 bytes of flash memory
113 *
114 * layout: PP PP VV VV MM MM MM MM MM MM ?? ?? SS SS SS SS SS SS SS SS
115 * SS SS SS SS SS SS SS SS 00 00 00 00 00 00 00 00 00 00 00 00
116 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
117 *
118 * P: le16 USB Product ID
119 * V: le16 USB Vendor ID
120 * M: be48 MAC Address
121 * S: le16 ASCII Serial Number
122 */
123enum cxacru_cm_flash {
124 CM_FLASH_READ = 0xa1,
125 CM_FLASH_WRITE = 0xa2
126};
127
Duncan Sands1b0e6142005-05-11 20:19:29 +0200128/* reply codes to the commands above */
129enum cxacru_cm_status {
130 CM_STATUS_UNDEFINED,
131 CM_STATUS_SUCCESS,
132 CM_STATUS_ERROR,
133 CM_STATUS_UNSUPPORTED,
134 CM_STATUS_UNIMPLEMENTED,
135 CM_STATUS_PARAMETER_ERROR,
136 CM_STATUS_DBG_LOOPBACK,
137 CM_STATUS_MAX,
138};
139
140/* indices into CARD_INFO_GET return array */
141enum cxacru_info_idx {
142 CXINF_DOWNSTREAM_RATE,
143 CXINF_UPSTREAM_RATE,
144 CXINF_LINK_STATUS,
145 CXINF_LINE_STATUS,
146 CXINF_MAC_ADDRESS_HIGH,
147 CXINF_MAC_ADDRESS_LOW,
148 CXINF_UPSTREAM_SNR_MARGIN,
149 CXINF_DOWNSTREAM_SNR_MARGIN,
150 CXINF_UPSTREAM_ATTENUATION,
151 CXINF_DOWNSTREAM_ATTENUATION,
152 CXINF_TRANSMITTER_POWER,
153 CXINF_UPSTREAM_BITS_PER_FRAME,
154 CXINF_DOWNSTREAM_BITS_PER_FRAME,
155 CXINF_STARTUP_ATTEMPTS,
156 CXINF_UPSTREAM_CRC_ERRORS,
157 CXINF_DOWNSTREAM_CRC_ERRORS,
158 CXINF_UPSTREAM_FEC_ERRORS,
159 CXINF_DOWNSTREAM_FEC_ERRORS,
160 CXINF_UPSTREAM_HEC_ERRORS,
161 CXINF_DOWNSTREAM_HEC_ERRORS,
162 CXINF_LINE_STARTABLE,
163 CXINF_MODULATION,
164 CXINF_ADSL_HEADEND,
165 CXINF_ADSL_HEADEND_ENVIRONMENT,
166 CXINF_CONTROLLER_VERSION,
167 /* dunno what the missing two mean */
168 CXINF_MAX = 0x1c,
169};
170
Simon Arlott6a02c9962007-04-26 00:38:05 -0700171enum cxacru_poll_state {
172 CXPOLL_STOPPING,
173 CXPOLL_STOPPED,
174 CXPOLL_POLLING,
175 CXPOLL_SHUTDOWN
176};
177
Duncan Sands1b0e6142005-05-11 20:19:29 +0200178struct cxacru_modem_type {
179 u32 pll_f_clk;
180 u32 pll_b_clk;
181 int boot_rom_patch;
182};
183
184struct cxacru_data {
185 struct usbatm_data *usbatm;
186
187 const struct cxacru_modem_type *modem_type;
188
189 int line_status;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700190 struct mutex adsl_state_serialize;
191 int adsl_status;
David Howellsc4028952006-11-22 14:57:56 +0000192 struct delayed_work poll_work;
Simon Arlottfa70fe42007-03-06 02:47:45 -0800193 u32 card_info[CXINF_MAX];
Simon Arlott6a02c9962007-04-26 00:38:05 -0700194 struct mutex poll_state_serialize;
Simon Arlott87e71b42007-05-10 23:04:11 -0700195 enum cxacru_poll_state poll_state;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200196
197 /* contol handles */
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100198 struct mutex cm_serialize;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200199 u8 *rcv_buf;
200 u8 *snd_buf;
201 struct urb *rcv_urb;
202 struct urb *snd_urb;
203 struct completion rcv_done;
204 struct completion snd_done;
205};
206
Simon Arlott6a02c9962007-04-26 00:38:05 -0700207static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
208 u8 *wdata, int wsize, u8 *rdata, int rsize);
209static void cxacru_poll_status(struct work_struct *work);
210
Simon Arlottfa70fe42007-03-06 02:47:45 -0800211/* Card info exported through sysfs */
212#define CXACRU__ATTR_INIT(_name) \
213static DEVICE_ATTR(_name, S_IRUGO, cxacru_sysfs_show_##_name, NULL)
214
Simon Arlott6a02c9962007-04-26 00:38:05 -0700215#define CXACRU_CMD_INIT(_name) \
216static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, \
217 cxacru_sysfs_show_##_name, cxacru_sysfs_store_##_name)
218
Simon Arlottfa70fe42007-03-06 02:47:45 -0800219#define CXACRU_ATTR_INIT(_value, _type, _name) \
220static ssize_t cxacru_sysfs_show_##_name(struct device *dev, \
221 struct device_attribute *attr, char *buf) \
222{ \
Simon Arlott9fc950d2009-11-21 15:33:51 +0000223 struct cxacru_data *instance = to_usbatm_driver_data(\
224 to_usb_interface(dev)); \
225\
226 if (instance == NULL) \
227 return -ENODEV; \
228\
Simon Arlottfa70fe42007-03-06 02:47:45 -0800229 return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
230} \
231CXACRU__ATTR_INIT(_name)
232
233#define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700234#define CXACRU_CMD_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800235#define CXACRU__ATTR_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
236
237#define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700238#define CXACRU_CMD_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800239#define CXACRU__ATTR_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
240
241static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
242{
243 return snprintf(buf, PAGE_SIZE, "%u\n", value);
244}
245
246static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
247{
248 return snprintf(buf, PAGE_SIZE, "%d\n", value);
249}
250
251static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
252{
Simon Arlott10107bd2009-04-23 18:19:02 +0100253 if (likely(value >= 0)) {
254 return snprintf(buf, PAGE_SIZE, "%u.%02u\n",
255 value / 100, value % 100);
256 } else {
257 value = -value;
258 return snprintf(buf, PAGE_SIZE, "-%u.%02u\n",
259 value / 100, value % 100);
260 }
Simon Arlottfa70fe42007-03-06 02:47:45 -0800261}
262
263static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
264{
Simon Arlott87e71b42007-05-10 23:04:11 -0700265 static char *str[] = { "no", "yes" };
266 if (unlikely(value >= ARRAY_SIZE(str)))
267 return snprintf(buf, PAGE_SIZE, "%u\n", value);
268 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800269}
270
271static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
272{
Simon Arlott87e71b42007-05-10 23:04:11 -0700273 static char *str[] = { NULL, "not connected", "connected", "lost" };
274 if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
275 return snprintf(buf, PAGE_SIZE, "%u\n", value);
276 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800277}
278
279static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
280{
Simon Arlott87e71b42007-05-10 23:04:11 -0700281 static char *str[] = { "down", "attempting to activate",
282 "training", "channel analysis", "exchange", "up",
283 "waiting", "initialising"
284 };
285 if (unlikely(value >= ARRAY_SIZE(str)))
286 return snprintf(buf, PAGE_SIZE, "%u\n", value);
287 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800288}
289
290static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
291{
Simon Arlott87e71b42007-05-10 23:04:11 -0700292 static char *str[] = {
Simon Arlott1bfbd282009-11-21 15:03:23 +0000293 "",
Simon Arlott87e71b42007-05-10 23:04:11 -0700294 "ANSI T1.413",
295 "ITU-T G.992.1 (G.DMT)",
296 "ITU-T G.992.2 (G.LITE)"
297 };
Simon Arlott1bfbd282009-11-21 15:03:23 +0000298 if (unlikely(value >= ARRAY_SIZE(str)))
Simon Arlott87e71b42007-05-10 23:04:11 -0700299 return snprintf(buf, PAGE_SIZE, "%u\n", value);
300 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800301}
302
303/*
304 * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
305 * this data is already in atm_dev there's no point.
306 *
307 * MAC_ADDRESS_HIGH = 0x????5544
308 * MAC_ADDRESS_LOW = 0x33221100
309 * Where 00-55 are bytes 0-5 of the MAC.
310 */
311static ssize_t cxacru_sysfs_show_mac_address(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000314 struct cxacru_data *instance = to_usbatm_driver_data(
315 to_usb_interface(dev));
Simon Arlottfa70fe42007-03-06 02:47:45 -0800316
Simon Arlott9fc950d2009-11-21 15:33:51 +0000317 if (instance == NULL || instance->usbatm->atm_dev == NULL)
318 return -ENODEV;
319
320 return snprintf(buf, PAGE_SIZE, "%pM\n",
321 instance->usbatm->atm_dev->esi);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800322}
323
Simon Arlott6a02c9962007-04-26 00:38:05 -0700324static ssize_t cxacru_sysfs_show_adsl_state(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
Simon Arlott87e71b42007-05-10 23:04:11 -0700327 static char *str[] = { "running", "stopped" };
Simon Arlott9fc950d2009-11-21 15:33:51 +0000328 struct cxacru_data *instance = to_usbatm_driver_data(
329 to_usb_interface(dev));
330 u32 value;
331
332 if (instance == NULL)
333 return -ENODEV;
334
335 value = instance->card_info[CXINF_LINE_STARTABLE];
Simon Arlott87e71b42007-05-10 23:04:11 -0700336 if (unlikely(value >= ARRAY_SIZE(str)))
337 return snprintf(buf, PAGE_SIZE, "%u\n", value);
338 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlott6a02c9962007-04-26 00:38:05 -0700339}
340
341static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t count)
343{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000344 struct cxacru_data *instance = to_usbatm_driver_data(
345 to_usb_interface(dev));
Simon Arlott6a02c9962007-04-26 00:38:05 -0700346 int ret;
347 int poll = -1;
348 char str_cmd[8];
349 int len = strlen(buf);
350
351 if (!capable(CAP_NET_ADMIN))
352 return -EACCES;
353
354 ret = sscanf(buf, "%7s", str_cmd);
355 if (ret != 1)
356 return -EINVAL;
357 ret = 0;
358
Simon Arlott9fc950d2009-11-21 15:33:51 +0000359 if (instance == NULL)
360 return -ENODEV;
361
Simon Arlott6a02c9962007-04-26 00:38:05 -0700362 if (mutex_lock_interruptible(&instance->adsl_state_serialize))
363 return -ERESTARTSYS;
364
365 if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
366 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
367 if (ret < 0) {
Simon Arlott9fc950d2009-11-21 15:33:51 +0000368 atm_err(instance->usbatm, "change adsl state:"
Simon Arlott6a02c9962007-04-26 00:38:05 -0700369 " CHIP_ADSL_LINE_STOP returned %d\n", ret);
370
371 ret = -EIO;
372 } else {
373 ret = len;
374 poll = CXPOLL_STOPPED;
375 }
376 }
377
378 /* Line status is only updated every second
379 * and the device appears to only react to
380 * START/STOP every second too. Wait 1.5s to
381 * be sure that restart will have an effect. */
382 if (!strcmp(str_cmd, "restart"))
383 msleep(1500);
384
385 if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
386 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
387 if (ret < 0) {
Simon Arlott9fc950d2009-11-21 15:33:51 +0000388 atm_err(instance->usbatm, "change adsl state:"
Simon Arlott6a02c9962007-04-26 00:38:05 -0700389 " CHIP_ADSL_LINE_START returned %d\n", ret);
390
391 ret = -EIO;
392 } else {
393 ret = len;
394 poll = CXPOLL_POLLING;
395 }
396 }
397
398 if (!strcmp(str_cmd, "poll")) {
399 ret = len;
400 poll = CXPOLL_POLLING;
401 }
402
403 if (ret == 0) {
404 ret = -EINVAL;
405 poll = -1;
406 }
407
408 if (poll == CXPOLL_POLLING) {
409 mutex_lock(&instance->poll_state_serialize);
410 switch (instance->poll_state) {
411 case CXPOLL_STOPPED:
412 /* start polling */
413 instance->poll_state = CXPOLL_POLLING;
414 break;
415
416 case CXPOLL_STOPPING:
417 /* abort stop request */
418 instance->poll_state = CXPOLL_POLLING;
419 case CXPOLL_POLLING:
420 case CXPOLL_SHUTDOWN:
421 /* don't start polling */
422 poll = -1;
423 }
424 mutex_unlock(&instance->poll_state_serialize);
425 } else if (poll == CXPOLL_STOPPED) {
426 mutex_lock(&instance->poll_state_serialize);
427 /* request stop */
428 if (instance->poll_state == CXPOLL_POLLING)
429 instance->poll_state = CXPOLL_STOPPING;
430 mutex_unlock(&instance->poll_state_serialize);
431 }
432
433 mutex_unlock(&instance->adsl_state_serialize);
434
435 if (poll == CXPOLL_POLLING)
436 cxacru_poll_status(&instance->poll_work.work);
437
438 return ret;
439}
440
Simon Arlottfa70fe42007-03-06 02:47:45 -0800441/*
442 * All device attributes are included in CXACRU_ALL_FILES
443 * so that the same list can be used multiple times:
444 * INIT (define the device attributes)
445 * CREATE (create all the device files)
446 * REMOVE (remove all the device files)
447 *
448 * With the last two being defined as needed in the functions
449 * they are used in before calling CXACRU_ALL_FILES()
450 */
451#define CXACRU_ALL_FILES(_action) \
452CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE, u32, downstream_rate); \
453CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE, u32, upstream_rate); \
454CXACRU_ATTR_##_action(CXINF_LINK_STATUS, LINK, link_status); \
455CXACRU_ATTR_##_action(CXINF_LINE_STATUS, LINE, line_status); \
456CXACRU__ATTR_##_action( mac_address); \
457CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN, dB, upstream_snr_margin); \
458CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN, dB, downstream_snr_margin); \
459CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION, dB, upstream_attenuation); \
460CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION, dB, downstream_attenuation); \
461CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER, s8, transmitter_power); \
462CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME, u32, upstream_bits_per_frame); \
463CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32, downstream_bits_per_frame); \
464CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS, u32, startup_attempts); \
465CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS, u32, upstream_crc_errors); \
466CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS, u32, downstream_crc_errors); \
467CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS, u32, upstream_fec_errors); \
468CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS, u32, downstream_fec_errors); \
469CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS, u32, upstream_hec_errors); \
470CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS, u32, downstream_hec_errors); \
471CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE, bool, line_startable); \
472CXACRU_ATTR_##_action(CXINF_MODULATION, MODU, modulation); \
473CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND, u32, adsl_headend); \
474CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT, u32, adsl_headend_environment); \
Simon Arlott6a02c9962007-04-26 00:38:05 -0700475CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION, u32, adsl_controller_version); \
476CXACRU_CMD_##_action( adsl_state);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800477
478CXACRU_ALL_FILES(INIT);
479
Duncan Sands1b0e6142005-05-11 20:19:29 +0200480/* the following three functions are stolen from drivers/usb/core/message.c */
David Howells7d12e782006-10-05 14:55:46 +0100481static void cxacru_blocking_completion(struct urb *urb)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200482{
Ming Leicdc97792008-02-24 18:41:47 +0800483 complete(urb->context);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200484}
485
486static void cxacru_timeout_kill(unsigned long data)
487{
488 usb_unlink_urb((struct urb *) data);
489}
490
491static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
492 int* actual_length)
493{
494 struct timer_list timer;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200495
496 init_timer(&timer);
497 timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
498 timer.data = (unsigned long) urb;
499 timer.function = cxacru_timeout_kill;
500 add_timer(&timer);
501 wait_for_completion(done);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200502 del_timer_sync(&timer);
503
504 if (actual_length)
505 *actual_length = urb->actual_length;
Oliver Neukum3b79cc22007-08-16 16:06:06 +0200506 return urb->status; /* must read status after completion */
Duncan Sands1b0e6142005-05-11 20:19:29 +0200507}
508
509static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
510 u8 *wdata, int wsize, u8 *rdata, int rsize)
511{
512 int ret, actlen;
513 int offb, offd;
514 const int stride = CMD_PACKET_SIZE - 4;
515 u8 *wbuf = instance->snd_buf;
516 u8 *rbuf = instance->rcv_buf;
517 int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
518 int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
519
520 if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100521 if (printk_ratelimit())
522 usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
523 wbuflen, rbuflen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200524 ret = -ENOMEM;
Jiri Slabyeeafa642009-03-11 21:47:36 +0100525 goto err;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200526 }
527
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100528 mutex_lock(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200529
530 /* submit reading urb before the writing one */
531 init_completion(&instance->rcv_done);
532 ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
533 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100534 if (printk_ratelimit())
535 usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
536 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200537 goto fail;
538 }
539
540 memset(wbuf, 0, wbuflen);
541 /* handle wsize == 0 */
542 wbuf[0] = cm;
543 for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
544 wbuf[offb] = cm;
545 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
546 }
547
548 instance->snd_urb->transfer_buffer_length = wbuflen;
549 init_completion(&instance->snd_done);
550 ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
551 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100552 if (printk_ratelimit())
553 usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
554 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200555 goto fail;
556 }
557
558 ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
559 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100560 if (printk_ratelimit())
561 usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200562 goto fail;
563 }
564
565 ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
566 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100567 if (printk_ratelimit())
568 usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200569 goto fail;
570 }
571 if (actlen % CMD_PACKET_SIZE || !actlen) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100572 if (printk_ratelimit())
573 usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
574 cm, actlen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200575 ret = -EIO;
576 goto fail;
577 }
578
579 /* check the return status and copy the data to the output buffer, if needed */
580 for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
581 if (rbuf[offb] != cm) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100582 if (printk_ratelimit())
583 usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
584 rbuf[offb], cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200585 ret = -EIO;
586 goto fail;
587 }
588 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100589 if (printk_ratelimit())
590 usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
591 cm, rbuf[offb + 1]);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200592 ret = -EIO;
593 goto fail;
594 }
595 if (offd >= rsize)
596 break;
597 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
598 offd += stride;
599 }
600
601 ret = offd;
602 dbg("cm %#x", cm);
603fail:
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100604 mutex_unlock(&instance->cm_serialize);
Jiri Slabyeeafa642009-03-11 21:47:36 +0100605err:
Duncan Sands1b0e6142005-05-11 20:19:29 +0200606 return ret;
607}
608
609static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
610 u32 *data, int size)
611{
612 int ret, len;
Al Virofd05e722008-04-28 07:00:16 +0100613 __le32 *buf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200614 int offb, offd;
615 const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
616 int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
617
618 buf = kmalloc(buflen, GFP_KERNEL);
619 if (!buf)
620 return -ENOMEM;
621
622 ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
623 if (ret < 0)
624 goto cleanup;
625
626 /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
627 len = ret / 4;
628 for (offb = 0; offb < len; ) {
629 int l = le32_to_cpu(buf[offb++]);
Simon Arlott5d0a9c72009-11-21 15:07:14 +0000630 if (l < 0 || l > stride || l > (len - offb) / 2) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100631 if (printk_ratelimit())
632 usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
633 cm, l);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200634 ret = -EIO;
635 goto cleanup;
636 }
637 while (l--) {
638 offd = le32_to_cpu(buf[offb++]);
639 if (offd >= size) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100640 if (printk_ratelimit())
Simon Arlott230ffc72008-07-12 22:19:48 +0100641 usb_err(instance->usbatm, "wrong index %#x in response to cm %#x\n",
Simon Arlott4ac07182007-09-25 20:20:10 +0100642 offd, cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200643 ret = -EIO;
644 goto cleanup;
645 }
646 data[offd] = le32_to_cpu(buf[offb++]);
647 }
648 }
649
650 ret = 0;
651
652cleanup:
653 kfree(buf);
654 return ret;
655}
656
657static int cxacru_card_status(struct cxacru_data *instance)
658{
659 int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
660 if (ret < 0) { /* firmware not loaded */
661 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
662 return ret;
663 }
664 return 0;
665}
666
Simon Arlottda1f82b52007-05-10 23:04:12 -0700667static void cxacru_remove_device_files(struct usbatm_data *usbatm_instance,
668 struct atm_dev *atm_dev)
669{
670 struct usb_interface *intf = usbatm_instance->usb_intf;
671
672 #define CXACRU_DEVICE_REMOVE_FILE(_name) \
673 device_remove_file(&intf->dev, &dev_attr_##_name);
674 CXACRU_ALL_FILES(REMOVE);
675 #undef CXACRU_DEVICE_REMOVE_FILE
676}
677
Duncan Sands1b0e6142005-05-11 20:19:29 +0200678static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
679 struct atm_dev *atm_dev)
680{
681 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700682 struct usb_interface *intf = usbatm_instance->usb_intf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200683 int ret;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700684 int start_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200685
686 dbg("cxacru_atm_start");
687
688 /* Read MAC address */
689 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
690 atm_dev->esi, sizeof(atm_dev->esi));
691 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100692 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200693 return ret;
694 }
695
Simon Arlottda1f82b52007-05-10 23:04:12 -0700696 #define CXACRU_DEVICE_CREATE_FILE(_name) \
697 ret = device_create_file(&intf->dev, &dev_attr_##_name); \
698 if (unlikely(ret)) \
699 goto fail_sysfs;
700 CXACRU_ALL_FILES(CREATE);
701 #undef CXACRU_DEVICE_CREATE_FILE
702
Duncan Sands1b0e6142005-05-11 20:19:29 +0200703 /* start ADSL */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700704 mutex_lock(&instance->adsl_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200705 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
Simon Arlottfd209e32007-05-10 23:04:13 -0700706 if (ret < 0)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100707 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200708
709 /* Start status polling */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700710 mutex_lock(&instance->poll_state_serialize);
711 switch (instance->poll_state) {
712 case CXPOLL_STOPPED:
713 /* start polling */
714 instance->poll_state = CXPOLL_POLLING;
715 break;
716
717 case CXPOLL_STOPPING:
718 /* abort stop request */
719 instance->poll_state = CXPOLL_POLLING;
720 case CXPOLL_POLLING:
721 case CXPOLL_SHUTDOWN:
722 /* don't start polling */
723 start_polling = 0;
724 }
725 mutex_unlock(&instance->poll_state_serialize);
726 mutex_unlock(&instance->adsl_state_serialize);
727
Simon Arlott885582c2009-11-21 15:12:56 +0000728 printk(KERN_INFO "%s%d: %s %pM\n", atm_dev->type, atm_dev->number,
729 usbatm_instance->description, atm_dev->esi);
730
Simon Arlott6a02c9962007-04-26 00:38:05 -0700731 if (start_polling)
732 cxacru_poll_status(&instance->poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200733 return 0;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700734
735fail_sysfs:
736 usb_err(usbatm_instance, "cxacru_atm_start: device_create_file failed (%d)\n", ret);
737 cxacru_remove_device_files(usbatm_instance, atm_dev);
738 return ret;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200739}
740
David Howellsc4028952006-11-22 14:57:56 +0000741static void cxacru_poll_status(struct work_struct *work)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200742{
David Howellsc4028952006-11-22 14:57:56 +0000743 struct cxacru_data *instance =
744 container_of(work, struct cxacru_data, poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200745 u32 buf[CXINF_MAX] = {};
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100746 struct usbatm_data *usbatm = instance->usbatm;
747 struct atm_dev *atm_dev = usbatm->atm_dev;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700748 int keep_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200749 int ret;
750
751 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
752 if (ret < 0) {
Simon Arlott6a02c9962007-04-26 00:38:05 -0700753 if (ret != -ESHUTDOWN)
754 atm_warn(usbatm, "poll status: error %d\n", ret);
755
756 mutex_lock(&instance->poll_state_serialize);
757 if (instance->poll_state != CXPOLL_SHUTDOWN) {
758 instance->poll_state = CXPOLL_STOPPED;
759
760 if (ret != -ESHUTDOWN)
761 atm_warn(usbatm, "polling disabled, set adsl_state"
762 " to 'start' or 'poll' to resume\n");
763 }
764 mutex_unlock(&instance->poll_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200765 goto reschedule;
766 }
767
Simon Arlottfa70fe42007-03-06 02:47:45 -0800768 memcpy(instance->card_info, buf, sizeof(instance->card_info));
769
Simon Arlott6a02c9962007-04-26 00:38:05 -0700770 if (instance->adsl_status != buf[CXINF_LINE_STARTABLE]) {
771 instance->adsl_status = buf[CXINF_LINE_STARTABLE];
772
773 switch (instance->adsl_status) {
774 case 0:
775 atm_printk(KERN_INFO, usbatm, "ADSL state: running\n");
776 break;
777
778 case 1:
779 atm_printk(KERN_INFO, usbatm, "ADSL state: stopped\n");
780 break;
781
782 default:
783 atm_printk(KERN_INFO, usbatm, "Unknown adsl status %02x\n", instance->adsl_status);
784 break;
785 }
786 }
787
Duncan Sands1b0e6142005-05-11 20:19:29 +0200788 if (instance->line_status == buf[CXINF_LINE_STATUS])
789 goto reschedule;
790
791 instance->line_status = buf[CXINF_LINE_STATUS];
792 switch (instance->line_status) {
793 case 0:
794 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100795 atm_info(usbatm, "ADSL line: down\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200796 break;
797
798 case 1:
799 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100800 atm_info(usbatm, "ADSL line: attempting to activate\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200801 break;
802
803 case 2:
804 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100805 atm_info(usbatm, "ADSL line: training\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200806 break;
807
808 case 3:
809 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100810 atm_info(usbatm, "ADSL line: channel analysis\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200811 break;
812
813 case 4:
814 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100815 atm_info(usbatm, "ADSL line: exchange\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200816 break;
817
818 case 5:
819 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
820 atm_dev->signal = ATM_PHY_SIG_FOUND;
821
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100822 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
Duncan Sands1b0e6142005-05-11 20:19:29 +0200823 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
824 break;
825
826 case 6:
827 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100828 atm_info(usbatm, "ADSL line: waiting\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200829 break;
830
831 case 7:
832 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100833 atm_info(usbatm, "ADSL line: initializing\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200834 break;
835
836 default:
837 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100838 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200839 break;
840 }
841reschedule:
Simon Arlott6a02c9962007-04-26 00:38:05 -0700842
843 mutex_lock(&instance->poll_state_serialize);
844 if (instance->poll_state == CXPOLL_STOPPING &&
845 instance->adsl_status == 1 && /* stopped */
846 instance->line_status == 0) /* down */
847 instance->poll_state = CXPOLL_STOPPED;
848
849 if (instance->poll_state == CXPOLL_STOPPED)
850 keep_polling = 0;
851 mutex_unlock(&instance->poll_state_serialize);
852
853 if (keep_polling)
854 schedule_delayed_work(&instance->poll_work,
855 round_jiffies_relative(POLL_INTERVAL*HZ));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200856}
857
858static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
David Woodhouse3b216d12008-05-24 00:05:28 +0100859 u8 code1, u8 code2, u32 addr, const u8 *data, int size)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200860{
861 int ret;
862 u8 *buf;
863 int offd, offb;
864 const int stride = CMD_PACKET_SIZE - 8;
865
866 buf = (u8 *) __get_free_page(GFP_KERNEL);
867 if (!buf)
868 return -ENOMEM;
869
870 offb = offd = 0;
871 do {
872 int l = min_t(int, stride, size - offd);
873 buf[offb++] = fw;
874 buf[offb++] = l;
875 buf[offb++] = code1;
876 buf[offb++] = code2;
Al Virofd05e722008-04-28 07:00:16 +0100877 put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200878 offb += 4;
879 addr += l;
880 if(l)
881 memcpy(buf + offb, data + offd, l);
882 if (l < stride)
883 memset(buf + offb + l, 0, stride - l);
884 offb += stride;
885 offd += stride;
886 if ((offb >= PAGE_SIZE) || (offd >= size)) {
887 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
888 buf, offb, NULL, CMD_TIMEOUT);
889 if (ret < 0) {
890 dbg("sending fw %#x failed", fw);
891 goto cleanup;
892 }
893 offb = 0;
894 }
895 } while(offd < size);
896 dbg("sent fw %#x", fw);
897
898 ret = 0;
899
900cleanup:
901 free_page((unsigned long) buf);
902 return ret;
903}
904
905static void cxacru_upload_firmware(struct cxacru_data *instance,
906 const struct firmware *fw,
907 const struct firmware *bp,
908 const struct firmware *cf)
909{
910 int ret;
911 int off;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100912 struct usbatm_data *usbatm = instance->usbatm;
913 struct usb_device *usb_dev = usbatm->usb_dev;
Al Virofd05e722008-04-28 07:00:16 +0100914 __le16 signature[] = { usb_dev->descriptor.idVendor,
915 usb_dev->descriptor.idProduct };
916 __le32 val;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200917
918 dbg("cxacru_upload_firmware");
919
920 /* FirmwarePllFClkValue */
921 val = cpu_to_le32(instance->modem_type->pll_f_clk);
922 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
923 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100924 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200925 return;
926 }
927
928 /* FirmwarePllBClkValue */
929 val = cpu_to_le32(instance->modem_type->pll_b_clk);
930 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
931 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100932 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200933 return;
934 }
935
936 /* Enable SDRAM */
937 val = cpu_to_le32(SDRAM_ENA);
938 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
939 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100940 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200941 return;
942 }
943
944 /* Firmware */
Simon Arlott885582c2009-11-21 15:12:56 +0000945 usb_info(usbatm, "loading firmware\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200946 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
947 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100948 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200949 return;
950 }
951
952 /* Boot ROM patch */
953 if (instance->modem_type->boot_rom_patch) {
Simon Arlott885582c2009-11-21 15:12:56 +0000954 usb_info(usbatm, "loading boot ROM patch\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200955 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
956 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100957 usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200958 return;
959 }
960 }
961
962 /* Signature */
963 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
964 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100965 usb_err(usbatm, "Signature storing failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200966 return;
967 }
968
Simon Arlott885582c2009-11-21 15:12:56 +0000969 usb_info(usbatm, "starting device\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200970 if (instance->modem_type->boot_rom_patch) {
971 val = cpu_to_le32(BR_ADDR);
972 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
973 }
974 else {
975 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
976 }
977 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100978 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200979 return;
980 }
981
982 /* Delay to allow firmware to start up. */
983 msleep_interruptible(1000);
984
985 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
986 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
987 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
988 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
989
990 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
991 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100992 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200993 return;
994 }
995
996 /* Load config data (le32), doing one packet at a time */
997 if (cf)
998 for (off = 0; off < cf->size / 4; ) {
Al Virofd05e722008-04-28 07:00:16 +0100999 __le32 buf[CMD_PACKET_SIZE / 4 - 1];
Duncan Sands1b0e6142005-05-11 20:19:29 +02001000 int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
1001 buf[0] = cpu_to_le32(len);
1002 for (i = 0; i < len; i++, off++) {
1003 buf[i * 2 + 1] = cpu_to_le32(off);
1004 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
1005 }
1006 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
1007 (u8 *) buf, len, NULL, 0);
1008 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001009 usb_err(usbatm, "load config data failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001010 return;
1011 }
1012 }
Duncan Sands1b0e6142005-05-11 20:19:29 +02001013}
1014
1015static int cxacru_find_firmware(struct cxacru_data *instance,
1016 char* phase, const struct firmware **fw_p)
1017{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001018 struct usbatm_data *usbatm = instance->usbatm;
1019 struct device *dev = &usbatm->usb_intf->dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +02001020 char buf[16];
1021
1022 sprintf(buf, "cxacru-%s.bin", phase);
1023 dbg("cxacru_find_firmware: looking for %s", buf);
1024
1025 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001026 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001027 return -ENOENT;
1028 }
1029
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001030 usb_info(usbatm, "found firmware %s\n", buf);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001031
1032 return 0;
1033}
1034
1035static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
1036 struct usb_interface *usb_intf)
1037{
Duncan Sands1b0e6142005-05-11 20:19:29 +02001038 const struct firmware *fw, *bp, *cf;
1039 struct cxacru_data *instance = usbatm_instance->driver_data;
1040
1041 int ret = cxacru_find_firmware(instance, "fw", &fw);
1042 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001043 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001044 return ret;
1045 }
1046
1047 if (instance->modem_type->boot_rom_patch) {
1048 ret = cxacru_find_firmware(instance, "bp", &bp);
1049 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001050 usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001051 release_firmware(fw);
1052 return ret;
1053 }
1054 }
1055
1056 if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */
1057 cf = NULL;
1058
1059 cxacru_upload_firmware(instance, fw, bp, cf);
1060
1061 if (cf)
1062 release_firmware(cf);
1063 if (instance->modem_type->boot_rom_patch)
1064 release_firmware(bp);
1065 release_firmware(fw);
1066
1067 ret = cxacru_card_status(instance);
1068 if (ret)
1069 dbg("modem initialisation failed");
1070 else
1071 dbg("done setting up the modem");
1072
1073 return ret;
1074}
1075
1076static int cxacru_bind(struct usbatm_data *usbatm_instance,
Duncan Sands35644b02006-01-17 11:16:13 +01001077 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands1b0e6142005-05-11 20:19:29 +02001078{
1079 struct cxacru_data *instance;
1080 struct usb_device *usb_dev = interface_to_usbdev(intf);
1081 int ret;
1082
1083 /* instance init */
Duncan Sands9a734ef2006-01-13 09:38:22 +01001084 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001085 if (!instance) {
1086 dbg("cxacru_bind: no memory for instance data");
1087 return -ENOMEM;
1088 }
1089
Duncan Sands1b0e6142005-05-11 20:19:29 +02001090 instance->usbatm = usbatm_instance;
1091 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1092
Simon Arlott6a02c9962007-04-26 00:38:05 -07001093 mutex_init(&instance->poll_state_serialize);
1094 instance->poll_state = CXPOLL_STOPPED;
1095 instance->line_status = -1;
1096 instance->adsl_status = -1;
1097
1098 mutex_init(&instance->adsl_state_serialize);
1099
Duncan Sands1b0e6142005-05-11 20:19:29 +02001100 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
1101 if (!instance->rcv_buf) {
1102 dbg("cxacru_bind: no memory for rcv_buf");
1103 ret = -ENOMEM;
1104 goto fail;
1105 }
1106 instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
1107 if (!instance->snd_buf) {
1108 dbg("cxacru_bind: no memory for snd_buf");
1109 ret = -ENOMEM;
1110 goto fail;
1111 }
1112 instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
1113 if (!instance->rcv_urb) {
1114 dbg("cxacru_bind: no memory for rcv_urb");
1115 ret = -ENOMEM;
1116 goto fail;
1117 }
1118 instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
1119 if (!instance->snd_urb) {
1120 dbg("cxacru_bind: no memory for snd_urb");
1121 ret = -ENOMEM;
1122 goto fail;
1123 }
1124
1125 usb_fill_int_urb(instance->rcv_urb,
1126 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
1127 instance->rcv_buf, PAGE_SIZE,
1128 cxacru_blocking_completion, &instance->rcv_done, 1);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001129
1130 usb_fill_int_urb(instance->snd_urb,
1131 usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
1132 instance->snd_buf, PAGE_SIZE,
1133 cxacru_blocking_completion, &instance->snd_done, 4);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001134
Arjan van de Venab3c81f2006-01-13 15:52:55 +01001135 mutex_init(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001136
David Howellsc4028952006-11-22 14:57:56 +00001137 INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001138
1139 usbatm_instance->driver_data = instance;
1140
Duncan Sands35644b02006-01-17 11:16:13 +01001141 usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001142
1143 return 0;
1144
1145 fail:
1146 free_page((unsigned long) instance->snd_buf);
1147 free_page((unsigned long) instance->rcv_buf);
1148 usb_free_urb(instance->snd_urb);
1149 usb_free_urb(instance->rcv_urb);
1150 kfree(instance);
1151
1152 return ret;
1153}
1154
1155static void cxacru_unbind(struct usbatm_data *usbatm_instance,
1156 struct usb_interface *intf)
1157{
1158 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlott6a02c9962007-04-26 00:38:05 -07001159 int is_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +02001160
1161 dbg("cxacru_unbind entered");
1162
1163 if (!instance) {
1164 dbg("cxacru_unbind: NULL instance!");
1165 return;
1166 }
1167
Simon Arlott6a02c9962007-04-26 00:38:05 -07001168 mutex_lock(&instance->poll_state_serialize);
1169 BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
1170
1171 /* ensure that status polling continues unless
1172 * it has already stopped */
1173 if (instance->poll_state == CXPOLL_STOPPED)
1174 is_polling = 0;
1175
1176 /* stop polling from being stopped or started */
1177 instance->poll_state = CXPOLL_SHUTDOWN;
1178 mutex_unlock(&instance->poll_state_serialize);
1179
1180 if (is_polling)
1181 cancel_rearming_delayed_work(&instance->poll_work);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001182
1183 usb_kill_urb(instance->snd_urb);
1184 usb_kill_urb(instance->rcv_urb);
1185 usb_free_urb(instance->snd_urb);
1186 usb_free_urb(instance->rcv_urb);
1187
1188 free_page((unsigned long) instance->snd_buf);
1189 free_page((unsigned long) instance->rcv_buf);
Simon Arlottfa70fe42007-03-06 02:47:45 -08001190
Duncan Sands1b0e6142005-05-11 20:19:29 +02001191 kfree(instance);
1192
1193 usbatm_instance->driver_data = NULL;
1194}
1195
1196static const struct cxacru_modem_type cxacru_cafe = {
1197 .pll_f_clk = 0x02d874df,
1198 .pll_b_clk = 0x0196a51a,
1199 .boot_rom_patch = 1,
1200};
1201
1202static const struct cxacru_modem_type cxacru_cb00 = {
1203 .pll_f_clk = 0x5,
1204 .pll_b_clk = 0x3,
1205 .boot_rom_patch = 0,
1206};
1207
1208static const struct usb_device_id cxacru_usb_ids[] = {
1209 { /* V = Conexant P = ADSL modem (Euphrates project) */
1210 USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
1211 },
1212 { /* V = Conexant P = ADSL modem (Hasbani project) */
1213 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
1214 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001215 { /* V = Conexant P = ADSL modem */
1216 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1217 },
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001218 { /* V = Conexant P = ADSL modem (Well PTI-800) */
1219 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
1220 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001221 { /* V = Conexant P = ADSL modem */
1222 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
1223 },
Duncan Sands44960af2006-10-05 11:05:50 +02001224 { /* V = Conexant P = ADSL modem (ZTE ZXDSL 852) */
1225 USB_DEVICE(0x0572, 0xcb07), .driver_info = (unsigned long) &cxacru_cb00
1226 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001227 { /* V = Olitec P = ADSL modem version 2 */
1228 USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
1229 },
1230 { /* V = Olitec P = ADSL modem version 3 */
1231 USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
1232 },
1233 { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
1234 USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
1235 },
1236 { /* V = Zoom P = 5510 */
1237 USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
1238 },
1239 { /* V = Draytek P = Vigor 318 */
1240 USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
1241 },
1242 { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
1243 USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
1244 },
1245 { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
1246 USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
1247 },
1248 { /* V = Aethra P = Starmodem UM1020 */
1249 USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
1250 },
1251 { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
1252 USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
1253 },
1254 { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
1255 USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1256 },
1257 { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
1258 USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
1259 },
1260 {}
1261};
1262
1263MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
1264
1265static struct usbatm_driver cxacru_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001266 .driver_name = cxacru_driver_name,
1267 .bind = cxacru_bind,
1268 .heavy_init = cxacru_heavy_init,
1269 .unbind = cxacru_unbind,
1270 .atm_start = cxacru_atm_start,
Simon Arlottda1f82b52007-05-10 23:04:12 -07001271 .atm_stop = cxacru_remove_device_files,
Duncan Sands80aae7a2006-01-13 10:59:23 +01001272 .bulk_in = CXACRU_EP_DATA,
1273 .bulk_out = CXACRU_EP_DATA,
Duncan Sands1b0e6142005-05-11 20:19:29 +02001274 .rx_padding = 3,
1275 .tx_padding = 11,
1276};
1277
1278static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1279{
1280 return usbatm_usb_probe(intf, id, &cxacru_driver);
1281}
1282
1283static struct usb_driver cxacru_usb_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001284 .name = cxacru_driver_name,
1285 .probe = cxacru_usb_probe,
1286 .disconnect = usbatm_usb_disconnect,
1287 .id_table = cxacru_usb_ids
1288};
1289
1290static int __init cxacru_init(void)
1291{
1292 return usb_register(&cxacru_usb_driver);
1293}
1294
1295static void __exit cxacru_cleanup(void)
1296{
1297 usb_deregister(&cxacru_usb_driver);
1298}
1299
1300module_init(cxacru_init);
1301module_exit(cxacru_cleanup);
1302
1303MODULE_AUTHOR(DRIVER_AUTHOR);
1304MODULE_DESCRIPTION(DRIVER_DESC);
1305MODULE_LICENSE("GPL");
1306MODULE_VERSION(DRIVER_VERSION);