USB: cxacru: export detailed device info through sysfs
[pandora-kernel.git] / drivers / usb / atm / cxacru.c
1 /******************************************************************************
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)
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under the terms of the GNU General Public License as published by the Free
10  *  Software Foundation; either version 2 of the License, or (at your option)
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful, but WITHOUT
14  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  *  more details.
17  *
18  *  You should have received a copy of the GNU General Public License along with
19  *  this program; if not, write to the Free Software Foundation, Inc., 59
20  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  ******************************************************************************/
23
24 /*
25  *  Credit is due for Josep Comas, who created the original patch to speedtch.c
26  *  to support the different padding used by the AccessRunner (now generalized
27  *  into usbatm), and the userspace firmware loading utility.
28  */
29
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/timer.h>
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/device.h>
38 #include <linux/firmware.h>
39 #include <linux/mutex.h>
40
41 #include "usbatm.h"
42
43 #define DRIVER_AUTHOR   "Roman Kagan, David Woodhouse, Duncan Sands, Simon Arlott"
44 #define DRIVER_VERSION  "0.3"
45 #define DRIVER_DESC     "Conexant AccessRunner ADSL USB modem driver"
46
47 static const char cxacru_driver_name[] = "cxacru";
48
49 #define CXACRU_EP_CMD           0x01    /* Bulk/interrupt in/out */
50 #define CXACRU_EP_DATA          0x02    /* Bulk in/out */
51
52 #define CMD_PACKET_SIZE         64      /* Should be maxpacket(ep)? */
53
54 /* Addresses */
55 #define PLLFCLK_ADDR    0x00350068
56 #define PLLBCLK_ADDR    0x0035006c
57 #define SDRAMEN_ADDR    0x00350010
58 #define FW_ADDR         0x00801000
59 #define BR_ADDR         0x00180600
60 #define SIG_ADDR        0x00180500
61 #define BR_STACK_ADDR   0x00187f10
62
63 /* Values */
64 #define SDRAM_ENA       0x1
65
66 #define CMD_TIMEOUT     2000    /* msecs */
67 #define POLL_INTERVAL   1       /* secs */
68
69 /* commands for interaction with the modem through the control channel before
70  * firmware is loaded  */
71 enum cxacru_fw_request {
72         FW_CMD_ERR,
73         FW_GET_VER,
74         FW_READ_MEM,
75         FW_WRITE_MEM,
76         FW_RMW_MEM,
77         FW_CHECKSUM_MEM,
78         FW_GOTO_MEM,
79 };
80
81 /* commands for interaction with the modem through the control channel once
82  * firmware is loaded  */
83 enum cxacru_cm_request {
84         CM_REQUEST_UNDEFINED = 0x80,
85         CM_REQUEST_TEST,
86         CM_REQUEST_CHIP_GET_MAC_ADDRESS,
87         CM_REQUEST_CHIP_GET_DP_VERSIONS,
88         CM_REQUEST_CHIP_ADSL_LINE_START,
89         CM_REQUEST_CHIP_ADSL_LINE_STOP,
90         CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
91         CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
92         CM_REQUEST_CARD_INFO_GET,
93         CM_REQUEST_CARD_DATA_GET,
94         CM_REQUEST_CARD_DATA_SET,
95         CM_REQUEST_COMMAND_HW_IO,
96         CM_REQUEST_INTERFACE_HW_IO,
97         CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
98         CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
99         CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
100         CM_REQUEST_CARD_GET_STATUS,
101         CM_REQUEST_CARD_GET_MAC_ADDRESS,
102         CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
103         CM_REQUEST_MAX,
104 };
105
106 /* reply codes to the commands above */
107 enum cxacru_cm_status {
108         CM_STATUS_UNDEFINED,
109         CM_STATUS_SUCCESS,
110         CM_STATUS_ERROR,
111         CM_STATUS_UNSUPPORTED,
112         CM_STATUS_UNIMPLEMENTED,
113         CM_STATUS_PARAMETER_ERROR,
114         CM_STATUS_DBG_LOOPBACK,
115         CM_STATUS_MAX,
116 };
117
118 /* indices into CARD_INFO_GET return array */
119 enum cxacru_info_idx {
120         CXINF_DOWNSTREAM_RATE,
121         CXINF_UPSTREAM_RATE,
122         CXINF_LINK_STATUS,
123         CXINF_LINE_STATUS,
124         CXINF_MAC_ADDRESS_HIGH,
125         CXINF_MAC_ADDRESS_LOW,
126         CXINF_UPSTREAM_SNR_MARGIN,
127         CXINF_DOWNSTREAM_SNR_MARGIN,
128         CXINF_UPSTREAM_ATTENUATION,
129         CXINF_DOWNSTREAM_ATTENUATION,
130         CXINF_TRANSMITTER_POWER,
131         CXINF_UPSTREAM_BITS_PER_FRAME,
132         CXINF_DOWNSTREAM_BITS_PER_FRAME,
133         CXINF_STARTUP_ATTEMPTS,
134         CXINF_UPSTREAM_CRC_ERRORS,
135         CXINF_DOWNSTREAM_CRC_ERRORS,
136         CXINF_UPSTREAM_FEC_ERRORS,
137         CXINF_DOWNSTREAM_FEC_ERRORS,
138         CXINF_UPSTREAM_HEC_ERRORS,
139         CXINF_DOWNSTREAM_HEC_ERRORS,
140         CXINF_LINE_STARTABLE,
141         CXINF_MODULATION,
142         CXINF_ADSL_HEADEND,
143         CXINF_ADSL_HEADEND_ENVIRONMENT,
144         CXINF_CONTROLLER_VERSION,
145         /* dunno what the missing two mean */
146         CXINF_MAX = 0x1c,
147 };
148
149 struct cxacru_modem_type {
150         u32 pll_f_clk;
151         u32 pll_b_clk;
152         int boot_rom_patch;
153 };
154
155 struct cxacru_data {
156         struct usbatm_data *usbatm;
157
158         const struct cxacru_modem_type *modem_type;
159
160         int line_status;
161         struct delayed_work poll_work;
162         u32 card_info[CXINF_MAX];
163
164         /* contol handles */
165         struct mutex cm_serialize;
166         u8 *rcv_buf;
167         u8 *snd_buf;
168         struct urb *rcv_urb;
169         struct urb *snd_urb;
170         struct completion rcv_done;
171         struct completion snd_done;
172 };
173
174 /* Card info exported through sysfs */
175 #define CXACRU__ATTR_INIT(_name) \
176 static DEVICE_ATTR(_name, S_IRUGO, cxacru_sysfs_show_##_name, NULL)
177
178 #define CXACRU_ATTR_INIT(_value, _type, _name) \
179 static ssize_t cxacru_sysfs_show_##_name(struct device *dev, \
180         struct device_attribute *attr, char *buf) \
181 { \
182         struct usb_interface *intf = to_usb_interface(dev); \
183         struct usbatm_data *usbatm_instance = usb_get_intfdata(intf); \
184         struct cxacru_data *instance = usbatm_instance->driver_data; \
185         return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
186 } \
187 CXACRU__ATTR_INIT(_name)
188
189 #define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
190 #define CXACRU__ATTR_CREATE(_name)        CXACRU_DEVICE_CREATE_FILE(_name)
191
192 #define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
193 #define CXACRU__ATTR_REMOVE(_name)        CXACRU_DEVICE_REMOVE_FILE(_name)
194
195 static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
196 {
197         return snprintf(buf, PAGE_SIZE, "%u\n", value);
198 }
199
200 static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
201 {
202         return snprintf(buf, PAGE_SIZE, "%d\n", value);
203 }
204
205 static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
206 {
207         if (unlikely(value < 0)) {
208                 return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
209                                                 value / 100, -value % 100);
210         } else {
211                 return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
212                                                 value / 100, value % 100);
213         }
214 }
215
216 static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
217 {
218         switch (value) {
219         case 0: return snprintf(buf, PAGE_SIZE, "no\n");
220         case 1: return snprintf(buf, PAGE_SIZE, "yes\n");
221         default: return 0;
222         }
223 }
224
225 static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
226 {
227         switch (value) {
228         case 1: return snprintf(buf, PAGE_SIZE, "not connected\n");
229         case 2: return snprintf(buf, PAGE_SIZE, "connected\n");
230         case 3: return snprintf(buf, PAGE_SIZE, "lost\n");
231         default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
232         }
233 }
234
235 static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
236 {
237         switch (value) {
238         case 0: return snprintf(buf, PAGE_SIZE, "down\n");
239         case 1: return snprintf(buf, PAGE_SIZE, "attempting to activate\n");
240         case 2: return snprintf(buf, PAGE_SIZE, "training\n");
241         case 3: return snprintf(buf, PAGE_SIZE, "channel analysis\n");
242         case 4: return snprintf(buf, PAGE_SIZE, "exchange\n");
243         case 5: return snprintf(buf, PAGE_SIZE, "up\n");
244         case 6: return snprintf(buf, PAGE_SIZE, "waiting\n");
245         case 7: return snprintf(buf, PAGE_SIZE, "initialising\n");
246         default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
247         }
248 }
249
250 static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
251 {
252         switch (value) {
253         case 0: return 0;
254         case 1: return snprintf(buf, PAGE_SIZE, "ANSI T1.413\n");
255         case 2: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.1 (G.DMT)\n");
256         case 3: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.2 (G.LITE)\n");
257         default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
258         }
259 }
260
261 /*
262  * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
263  * this data is already in atm_dev there's no point.
264  *
265  * MAC_ADDRESS_HIGH = 0x????5544
266  * MAC_ADDRESS_LOW  = 0x33221100
267  * Where 00-55 are bytes 0-5 of the MAC.
268  */
269 static ssize_t cxacru_sysfs_show_mac_address(struct device *dev,
270         struct device_attribute *attr, char *buf)
271 {
272         struct usb_interface *intf = to_usb_interface(dev);
273         struct usbatm_data *usbatm_instance = usb_get_intfdata(intf);
274         struct atm_dev *atm_dev = usbatm_instance->atm_dev;
275
276         return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
277                         atm_dev->esi[0], atm_dev->esi[1], atm_dev->esi[2],
278                         atm_dev->esi[3], atm_dev->esi[4], atm_dev->esi[5]);
279 }
280
281 /*
282  * All device attributes are included in CXACRU_ALL_FILES
283  * so that the same list can be used multiple times:
284  *     INIT   (define the device attributes)
285  *     CREATE (create all the device files)
286  *     REMOVE (remove all the device files)
287  *
288  * With the last two being defined as needed in the functions
289  * they are used in before calling CXACRU_ALL_FILES()
290  */
291 #define CXACRU_ALL_FILES(_action) \
292 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE,           u32,  downstream_rate); \
293 CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE,             u32,  upstream_rate); \
294 CXACRU_ATTR_##_action(CXINF_LINK_STATUS,               LINK, link_status); \
295 CXACRU_ATTR_##_action(CXINF_LINE_STATUS,               LINE, line_status); \
296 CXACRU__ATTR_##_action(                                      mac_address); \
297 CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN,       dB,   upstream_snr_margin); \
298 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN,     dB,   downstream_snr_margin); \
299 CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION,      dB,   upstream_attenuation); \
300 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION,    dB,   downstream_attenuation); \
301 CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER,         s8,   transmitter_power); \
302 CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME,   u32,  upstream_bits_per_frame); \
303 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32,  downstream_bits_per_frame); \
304 CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS,          u32,  startup_attempts); \
305 CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS,       u32,  upstream_crc_errors); \
306 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS,     u32,  downstream_crc_errors); \
307 CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS,       u32,  upstream_fec_errors); \
308 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS,     u32,  downstream_fec_errors); \
309 CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS,       u32,  upstream_hec_errors); \
310 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS,     u32,  downstream_hec_errors); \
311 CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE,            bool, line_startable); \
312 CXACRU_ATTR_##_action(CXINF_MODULATION,                MODU, modulation); \
313 CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND,              u32,  adsl_headend); \
314 CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT,  u32,  adsl_headend_environment); \
315 CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION,        u32,  adsl_controller_version);
316
317 CXACRU_ALL_FILES(INIT);
318
319 /* the following three functions are stolen from drivers/usb/core/message.c */
320 static void cxacru_blocking_completion(struct urb *urb)
321 {
322         complete((struct completion *)urb->context);
323 }
324
325 static void cxacru_timeout_kill(unsigned long data)
326 {
327         usb_unlink_urb((struct urb *) data);
328 }
329
330 static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
331                                  int* actual_length)
332 {
333         struct timer_list timer;
334         int status;
335
336         init_timer(&timer);
337         timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
338         timer.data = (unsigned long) urb;
339         timer.function = cxacru_timeout_kill;
340         add_timer(&timer);
341         wait_for_completion(done);
342         status = urb->status;
343         if (status == -ECONNRESET)
344                 status = -ETIMEDOUT;
345         del_timer_sync(&timer);
346
347         if (actual_length)
348                 *actual_length = urb->actual_length;
349         return status;
350 }
351
352 static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
353                      u8 *wdata, int wsize, u8 *rdata, int rsize)
354 {
355         int ret, actlen;
356         int offb, offd;
357         const int stride = CMD_PACKET_SIZE - 4;
358         u8 *wbuf = instance->snd_buf;
359         u8 *rbuf = instance->rcv_buf;
360         int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
361         int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
362
363         if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
364                 dbg("too big transfer requested");
365                 ret = -ENOMEM;
366                 goto fail;
367         }
368
369         mutex_lock(&instance->cm_serialize);
370
371         /* submit reading urb before the writing one */
372         init_completion(&instance->rcv_done);
373         ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
374         if (ret < 0) {
375                 dbg("submitting read urb for cm %#x failed", cm);
376                 ret = ret;
377                 goto fail;
378         }
379
380         memset(wbuf, 0, wbuflen);
381         /* handle wsize == 0 */
382         wbuf[0] = cm;
383         for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
384                 wbuf[offb] = cm;
385                 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
386         }
387
388         instance->snd_urb->transfer_buffer_length = wbuflen;
389         init_completion(&instance->snd_done);
390         ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
391         if (ret < 0) {
392                 dbg("submitting write urb for cm %#x failed", cm);
393                 ret = ret;
394                 goto fail;
395         }
396
397         ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
398         if (ret < 0) {
399                 dbg("sending cm %#x failed", cm);
400                 ret = ret;
401                 goto fail;
402         }
403
404         ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
405         if (ret < 0) {
406                 dbg("receiving cm %#x failed", cm);
407                 ret = ret;
408                 goto fail;
409         }
410         if (actlen % CMD_PACKET_SIZE || !actlen) {
411                 dbg("response is not a positive multiple of %d: %#x",
412                                 CMD_PACKET_SIZE, actlen);
413                 ret = -EIO;
414                 goto fail;
415         }
416
417         /* check the return status and copy the data to the output buffer, if needed */
418         for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
419                 if (rbuf[offb] != cm) {
420                         dbg("wrong cm %#x in response", rbuf[offb]);
421                         ret = -EIO;
422                         goto fail;
423                 }
424                 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
425                         dbg("response failed: %#x", rbuf[offb + 1]);
426                         ret = -EIO;
427                         goto fail;
428                 }
429                 if (offd >= rsize)
430                         break;
431                 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
432                 offd += stride;
433         }
434
435         ret = offd;
436         dbg("cm %#x", cm);
437 fail:
438         mutex_unlock(&instance->cm_serialize);
439         return ret;
440 }
441
442 static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
443                                u32 *data, int size)
444 {
445         int ret, len;
446         u32 *buf;
447         int offb, offd;
448         const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
449         int buflen =  ((size - 1) / stride + 1 + size * 2) * 4;
450
451         buf = kmalloc(buflen, GFP_KERNEL);
452         if (!buf)
453                 return -ENOMEM;
454
455         ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
456         if (ret < 0)
457                 goto cleanup;
458
459         /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
460         len = ret / 4;
461         for (offb = 0; offb < len; ) {
462                 int l = le32_to_cpu(buf[offb++]);
463                 if (l > stride || l > (len - offb) / 2) {
464                         dbg("wrong data length %#x in response", l);
465                         ret = -EIO;
466                         goto cleanup;
467                 }
468                 while (l--) {
469                         offd = le32_to_cpu(buf[offb++]);
470                         if (offd >= size) {
471                                 dbg("wrong index %#x in response", offd);
472                                 ret = -EIO;
473                                 goto cleanup;
474                         }
475                         data[offd] = le32_to_cpu(buf[offb++]);
476                 }
477         }
478
479         ret = 0;
480
481 cleanup:
482         kfree(buf);
483         return ret;
484 }
485
486 static int cxacru_card_status(struct cxacru_data *instance)
487 {
488         int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
489         if (ret < 0) {          /* firmware not loaded */
490                 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
491                 return ret;
492         }
493         return 0;
494 }
495
496 static void cxacru_poll_status(struct work_struct *work);
497
498 static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
499                 struct atm_dev *atm_dev)
500 {
501         struct cxacru_data *instance = usbatm_instance->driver_data;
502         /*
503         struct atm_dev *atm_dev = usbatm_instance->atm_dev;
504         */
505         int ret;
506
507         dbg("cxacru_atm_start");
508
509         /* Read MAC address */
510         ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
511                         atm_dev->esi, sizeof(atm_dev->esi));
512         if (ret < 0) {
513                 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
514                 return ret;
515         }
516
517         /* start ADSL */
518         ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
519         if (ret < 0) {
520                 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
521                 return ret;
522         }
523
524         /* Start status polling */
525         cxacru_poll_status(&instance->poll_work.work);
526         return 0;
527 }
528
529 static void cxacru_poll_status(struct work_struct *work)
530 {
531         struct cxacru_data *instance =
532                 container_of(work, struct cxacru_data, poll_work.work);
533         u32 buf[CXINF_MAX] = {};
534         struct usbatm_data *usbatm = instance->usbatm;
535         struct atm_dev *atm_dev = usbatm->atm_dev;
536         int ret;
537
538         ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
539         if (ret < 0) {
540                 atm_warn(usbatm, "poll status: error %d\n", ret);
541                 goto reschedule;
542         }
543
544         memcpy(instance->card_info, buf, sizeof(instance->card_info));
545
546         if (instance->line_status == buf[CXINF_LINE_STATUS])
547                 goto reschedule;
548
549         instance->line_status = buf[CXINF_LINE_STATUS];
550         switch (instance->line_status) {
551         case 0:
552                 atm_dev->signal = ATM_PHY_SIG_LOST;
553                 atm_info(usbatm, "ADSL line: down\n");
554                 break;
555
556         case 1:
557                 atm_dev->signal = ATM_PHY_SIG_LOST;
558                 atm_info(usbatm, "ADSL line: attempting to activate\n");
559                 break;
560
561         case 2:
562                 atm_dev->signal = ATM_PHY_SIG_LOST;
563                 atm_info(usbatm, "ADSL line: training\n");
564                 break;
565
566         case 3:
567                 atm_dev->signal = ATM_PHY_SIG_LOST;
568                 atm_info(usbatm, "ADSL line: channel analysis\n");
569                 break;
570
571         case 4:
572                 atm_dev->signal = ATM_PHY_SIG_LOST;
573                 atm_info(usbatm, "ADSL line: exchange\n");
574                 break;
575
576         case 5:
577                 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
578                 atm_dev->signal = ATM_PHY_SIG_FOUND;
579
580                 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
581                      buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
582                 break;
583
584         case 6:
585                 atm_dev->signal = ATM_PHY_SIG_LOST;
586                 atm_info(usbatm, "ADSL line: waiting\n");
587                 break;
588
589         case 7:
590                 atm_dev->signal = ATM_PHY_SIG_LOST;
591                 atm_info(usbatm, "ADSL line: initializing\n");
592                 break;
593
594         default:
595                 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
596                 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
597                 break;
598         }
599 reschedule:
600         schedule_delayed_work(&instance->poll_work,
601                         round_jiffies_relative(msecs_to_jiffies(POLL_INTERVAL*1000)));
602 }
603
604 static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
605                      u8 code1, u8 code2, u32 addr, u8 *data, int size)
606 {
607         int ret;
608         u8 *buf;
609         int offd, offb;
610         const int stride = CMD_PACKET_SIZE - 8;
611
612         buf = (u8 *) __get_free_page(GFP_KERNEL);
613         if (!buf)
614                 return -ENOMEM;
615
616         offb = offd = 0;
617         do {
618                 int l = min_t(int, stride, size - offd);
619                 buf[offb++] = fw;
620                 buf[offb++] = l;
621                 buf[offb++] = code1;
622                 buf[offb++] = code2;
623                 *((u32 *) (buf + offb)) = cpu_to_le32(addr);
624                 offb += 4;
625                 addr += l;
626                 if(l)
627                         memcpy(buf + offb, data + offd, l);
628                 if (l < stride)
629                         memset(buf + offb + l, 0, stride - l);
630                 offb += stride;
631                 offd += stride;
632                 if ((offb >= PAGE_SIZE) || (offd >= size)) {
633                         ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
634                                            buf, offb, NULL, CMD_TIMEOUT);
635                         if (ret < 0) {
636                                 dbg("sending fw %#x failed", fw);
637                                 goto cleanup;
638                         }
639                         offb = 0;
640                 }
641         } while(offd < size);
642         dbg("sent fw %#x", fw);
643
644         ret = 0;
645
646 cleanup:
647         free_page((unsigned long) buf);
648         return ret;
649 }
650
651 static void cxacru_upload_firmware(struct cxacru_data *instance,
652                                    const struct firmware *fw,
653                                    const struct firmware *bp,
654                                    const struct firmware *cf)
655 {
656         int ret;
657         int off;
658         struct usbatm_data *usbatm = instance->usbatm;
659         struct usb_device *usb_dev = usbatm->usb_dev;
660         u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct };
661         u32 val;
662
663         dbg("cxacru_upload_firmware");
664
665         /* FirmwarePllFClkValue */
666         val = cpu_to_le32(instance->modem_type->pll_f_clk);
667         ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
668         if (ret) {
669                 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
670                 return;
671         }
672
673         /* FirmwarePllBClkValue */
674         val = cpu_to_le32(instance->modem_type->pll_b_clk);
675         ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
676         if (ret) {
677                 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
678                 return;
679         }
680
681         /* Enable SDRAM */
682         val = cpu_to_le32(SDRAM_ENA);
683         ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
684         if (ret) {
685                 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
686                 return;
687         }
688
689         /* Firmware */
690         ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
691         if (ret) {
692                 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
693                 return;
694         }
695
696         /* Boot ROM patch */
697         if (instance->modem_type->boot_rom_patch) {
698                 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
699                 if (ret) {
700                         usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
701                         return;
702                 }
703         }
704
705         /* Signature */
706         ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
707         if (ret) {
708                 usb_err(usbatm, "Signature storing failed: %d\n", ret);
709                 return;
710         }
711
712         if (instance->modem_type->boot_rom_patch) {
713                 val = cpu_to_le32(BR_ADDR);
714                 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
715         }
716         else {
717                 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
718         }
719         if (ret) {
720                 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
721                 return;
722         }
723
724         /* Delay to allow firmware to start up. */
725         msleep_interruptible(1000);
726
727         usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
728         usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
729         usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
730         usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
731
732         ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
733         if (ret < 0) {
734                 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
735                 return;
736         }
737
738         /* Load config data (le32), doing one packet at a time */
739         if (cf)
740                 for (off = 0; off < cf->size / 4; ) {
741                         u32 buf[CMD_PACKET_SIZE / 4 - 1];
742                         int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
743                         buf[0] = cpu_to_le32(len);
744                         for (i = 0; i < len; i++, off++) {
745                                 buf[i * 2 + 1] = cpu_to_le32(off);
746                                 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
747                         }
748                         ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
749                                         (u8 *) buf, len, NULL, 0);
750                         if (ret < 0) {
751                                 usb_err(usbatm, "load config data failed: %d\n", ret);
752                                 return;
753                         }
754                 }
755
756         msleep_interruptible(4000);
757 }
758
759 static int cxacru_find_firmware(struct cxacru_data *instance,
760                                 char* phase, const struct firmware **fw_p)
761 {
762         struct usbatm_data *usbatm = instance->usbatm;
763         struct device *dev = &usbatm->usb_intf->dev;
764         char buf[16];
765
766         sprintf(buf, "cxacru-%s.bin", phase);
767         dbg("cxacru_find_firmware: looking for %s", buf);
768
769         if (request_firmware(fw_p, buf, dev)) {
770                 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
771                 return -ENOENT;
772         }
773
774         usb_info(usbatm, "found firmware %s\n", buf);
775
776         return 0;
777 }
778
779 static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
780                              struct usb_interface *usb_intf)
781 {
782         const struct firmware *fw, *bp, *cf;
783         struct cxacru_data *instance = usbatm_instance->driver_data;
784
785         int ret = cxacru_find_firmware(instance, "fw", &fw);
786         if (ret) {
787                 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
788                 return ret;
789         }
790
791         if (instance->modem_type->boot_rom_patch) {
792                 ret = cxacru_find_firmware(instance, "bp", &bp);
793                 if (ret) {
794                         usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
795                         release_firmware(fw);
796                         return ret;
797                 }
798         }
799
800         if (cxacru_find_firmware(instance, "cf", &cf))          /* optional */
801                 cf = NULL;
802
803         cxacru_upload_firmware(instance, fw, bp, cf);
804
805         if (cf)
806                 release_firmware(cf);
807         if (instance->modem_type->boot_rom_patch)
808                 release_firmware(bp);
809         release_firmware(fw);
810
811         ret = cxacru_card_status(instance);
812         if (ret)
813                 dbg("modem initialisation failed");
814         else
815                 dbg("done setting up the modem");
816
817         return ret;
818 }
819
820 static int cxacru_bind(struct usbatm_data *usbatm_instance,
821                        struct usb_interface *intf, const struct usb_device_id *id)
822 {
823         struct cxacru_data *instance;
824         struct usb_device *usb_dev = interface_to_usbdev(intf);
825         int ret;
826
827         /* instance init */
828         instance = kzalloc(sizeof(*instance), GFP_KERNEL);
829         if (!instance) {
830                 dbg("cxacru_bind: no memory for instance data");
831                 return -ENOMEM;
832         }
833
834         instance->usbatm = usbatm_instance;
835         instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
836         memset(instance->card_info, 0, sizeof(instance->card_info));
837
838         instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
839         if (!instance->rcv_buf) {
840                 dbg("cxacru_bind: no memory for rcv_buf");
841                 ret = -ENOMEM;
842                 goto fail;
843         }
844         instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
845         if (!instance->snd_buf) {
846                 dbg("cxacru_bind: no memory for snd_buf");
847                 ret = -ENOMEM;
848                 goto fail;
849         }
850         instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
851         if (!instance->rcv_urb) {
852                 dbg("cxacru_bind: no memory for rcv_urb");
853                 ret = -ENOMEM;
854                 goto fail;
855         }
856         instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
857         if (!instance->snd_urb) {
858                 dbg("cxacru_bind: no memory for snd_urb");
859                 ret = -ENOMEM;
860                 goto fail;
861         }
862
863         #define CXACRU_DEVICE_CREATE_FILE(_name) \
864                 ret = device_create_file(&intf->dev, &dev_attr_##_name); \
865                 if (unlikely(ret)) \
866                         goto fail_sysfs;
867         CXACRU_ALL_FILES(CREATE);
868         #undef CXACRU_DEVICE_CREATE_FILE
869
870         usb_fill_int_urb(instance->rcv_urb,
871                         usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
872                         instance->rcv_buf, PAGE_SIZE,
873                         cxacru_blocking_completion, &instance->rcv_done, 1);
874
875         usb_fill_int_urb(instance->snd_urb,
876                         usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
877                         instance->snd_buf, PAGE_SIZE,
878                         cxacru_blocking_completion, &instance->snd_done, 4);
879
880         mutex_init(&instance->cm_serialize);
881
882         INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
883
884         usbatm_instance->driver_data = instance;
885
886         usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
887
888         return 0;
889
890  fail_sysfs:
891         dbg("cxacru_bind: device_create_file failed (%d)\n", ret);
892
893         #define CXACRU_DEVICE_REMOVE_FILE(_name) \
894                 device_remove_file(&intf->dev, &dev_attr_##_name);
895         CXACRU_ALL_FILES(REMOVE);
896         #undef CXACRU_DEVICE_REVOVE_FILE
897
898  fail:
899         free_page((unsigned long) instance->snd_buf);
900         free_page((unsigned long) instance->rcv_buf);
901         usb_free_urb(instance->snd_urb);
902         usb_free_urb(instance->rcv_urb);
903         kfree(instance);
904
905         return ret;
906 }
907
908 static void cxacru_unbind(struct usbatm_data *usbatm_instance,
909                 struct usb_interface *intf)
910 {
911         struct cxacru_data *instance = usbatm_instance->driver_data;
912
913         dbg("cxacru_unbind entered");
914
915         if (!instance) {
916                 dbg("cxacru_unbind: NULL instance!");
917                 return;
918         }
919
920         while (!cancel_delayed_work(&instance->poll_work))
921                flush_scheduled_work();
922
923         usb_kill_urb(instance->snd_urb);
924         usb_kill_urb(instance->rcv_urb);
925         usb_free_urb(instance->snd_urb);
926         usb_free_urb(instance->rcv_urb);
927
928         free_page((unsigned long) instance->snd_buf);
929         free_page((unsigned long) instance->rcv_buf);
930
931         #define CXACRU_DEVICE_REMOVE_FILE(_name) \
932                 device_remove_file(&intf->dev, &dev_attr_##_name);
933         CXACRU_ALL_FILES(REMOVE);
934         #undef CXACRU_DEVICE_REVOVE_FILE
935
936         kfree(instance);
937
938         usbatm_instance->driver_data = NULL;
939 }
940
941 static const struct cxacru_modem_type cxacru_cafe = {
942         .pll_f_clk = 0x02d874df,
943         .pll_b_clk = 0x0196a51a,
944         .boot_rom_patch = 1,
945 };
946
947 static const struct cxacru_modem_type cxacru_cb00 = {
948         .pll_f_clk = 0x5,
949         .pll_b_clk = 0x3,
950         .boot_rom_patch = 0,
951 };
952
953 static const struct usb_device_id cxacru_usb_ids[] = {
954         { /* V = Conexant                       P = ADSL modem (Euphrates project)      */
955                 USB_DEVICE(0x0572, 0xcafe),     .driver_info = (unsigned long) &cxacru_cafe
956         },
957         { /* V = Conexant                       P = ADSL modem (Hasbani project)        */
958                 USB_DEVICE(0x0572, 0xcb00),     .driver_info = (unsigned long) &cxacru_cb00
959         },
960         { /* V = Conexant                       P = ADSL modem                          */
961                 USB_DEVICE(0x0572, 0xcb01),     .driver_info = (unsigned long) &cxacru_cb00
962         },
963         { /* V = Conexant                       P = ADSL modem (Well PTI-800) */
964                 USB_DEVICE(0x0572, 0xcb02),     .driver_info = (unsigned long) &cxacru_cb00
965         },
966         { /* V = Conexant                       P = ADSL modem                          */
967                 USB_DEVICE(0x0572, 0xcb06),     .driver_info = (unsigned long) &cxacru_cb00
968         },
969         { /* V = Conexant                       P = ADSL modem (ZTE ZXDSL 852)          */
970                 USB_DEVICE(0x0572, 0xcb07),     .driver_info = (unsigned long) &cxacru_cb00
971         },
972         { /* V = Olitec                         P = ADSL modem version 2                */
973                 USB_DEVICE(0x08e3, 0x0100),     .driver_info = (unsigned long) &cxacru_cafe
974         },
975         { /* V = Olitec                         P = ADSL modem version 3                */
976                 USB_DEVICE(0x08e3, 0x0102),     .driver_info = (unsigned long) &cxacru_cb00
977         },
978         { /* V = Trust/Amigo Technology Co.     P = AMX-CA86U                           */
979                 USB_DEVICE(0x0eb0, 0x3457),     .driver_info = (unsigned long) &cxacru_cafe
980         },
981         { /* V = Zoom                           P = 5510                                */
982                 USB_DEVICE(0x1803, 0x5510),     .driver_info = (unsigned long) &cxacru_cb00
983         },
984         { /* V = Draytek                        P = Vigor 318                           */
985                 USB_DEVICE(0x0675, 0x0200),     .driver_info = (unsigned long) &cxacru_cb00
986         },
987         { /* V = Zyxel                          P = 630-C1 aka OMNI ADSL USB (Annex A)  */
988                 USB_DEVICE(0x0586, 0x330a),     .driver_info = (unsigned long) &cxacru_cb00
989         },
990         { /* V = Zyxel                          P = 630-C3 aka OMNI ADSL USB (Annex B)  */
991                 USB_DEVICE(0x0586, 0x330b),     .driver_info = (unsigned long) &cxacru_cb00
992         },
993         { /* V = Aethra                         P = Starmodem UM1020                    */
994                 USB_DEVICE(0x0659, 0x0020),     .driver_info = (unsigned long) &cxacru_cb00
995         },
996         { /* V = Aztech Systems                 P = ? AKA Pirelli AUA-010               */
997                 USB_DEVICE(0x0509, 0x0812),     .driver_info = (unsigned long) &cxacru_cb00
998         },
999         { /* V = Netopia                        P = Cayman 3341(Annex A)/3351(Annex B)  */
1000                 USB_DEVICE(0x100d, 0xcb01),     .driver_info = (unsigned long) &cxacru_cb00
1001         },
1002         { /* V = Netopia                        P = Cayman 3342(Annex A)/3352(Annex B)  */
1003                 USB_DEVICE(0x100d, 0x3342),     .driver_info = (unsigned long) &cxacru_cb00
1004         },
1005         {}
1006 };
1007
1008 MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
1009
1010 static struct usbatm_driver cxacru_driver = {
1011         .driver_name    = cxacru_driver_name,
1012         .bind           = cxacru_bind,
1013         .heavy_init     = cxacru_heavy_init,
1014         .unbind         = cxacru_unbind,
1015         .atm_start      = cxacru_atm_start,
1016         .bulk_in        = CXACRU_EP_DATA,
1017         .bulk_out       = CXACRU_EP_DATA,
1018         .rx_padding     = 3,
1019         .tx_padding     = 11,
1020 };
1021
1022 static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1023 {
1024         return usbatm_usb_probe(intf, id, &cxacru_driver);
1025 }
1026
1027 static struct usb_driver cxacru_usb_driver = {
1028         .name           = cxacru_driver_name,
1029         .probe          = cxacru_usb_probe,
1030         .disconnect     = usbatm_usb_disconnect,
1031         .id_table       = cxacru_usb_ids
1032 };
1033
1034 static int __init cxacru_init(void)
1035 {
1036         return usb_register(&cxacru_usb_driver);
1037 }
1038
1039 static void __exit cxacru_cleanup(void)
1040 {
1041         usb_deregister(&cxacru_usb_driver);
1042 }
1043
1044 module_init(cxacru_init);
1045 module_exit(cxacru_cleanup);
1046
1047 MODULE_AUTHOR(DRIVER_AUTHOR);
1048 MODULE_DESCRIPTION(DRIVER_DESC);
1049 MODULE_LICENSE("GPL");
1050 MODULE_VERSION(DRIVER_VERSION);