blob: 139be1207d2298c20b159185118748945b479686 [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)
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> /* FIXME: linux/firmware.h should include it itself */
38#include <linux/firmware.h>
39
40#include "usbatm.h"
41
42#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands"
43#define DRIVER_VERSION "0.2"
44#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver"
45
46static const char cxacru_driver_name[] = "cxacru";
47
48#define CXACRU_EP_CMD 0x01 /* Bulk/interrupt in/out */
49#define CXACRU_EP_DATA 0x02 /* Bulk in/out */
50
51#define CMD_PACKET_SIZE 64 /* Should be maxpacket(ep)? */
52
53/* Addresses */
54#define PLLFCLK_ADDR 0x00350068
55#define PLLBCLK_ADDR 0x0035006c
56#define SDRAMEN_ADDR 0x00350010
57#define FW_ADDR 0x00801000
58#define BR_ADDR 0x00180600
59#define SIG_ADDR 0x00180500
60#define BR_STACK_ADDR 0x00187f10
61
62/* Values */
63#define SDRAM_ENA 0x1
64
65#define CMD_TIMEOUT 2000 /* msecs */
66#define POLL_INTERVAL 5000 /* msecs */
67
68/* commands for interaction with the modem through the control channel before
69 * firmware is loaded */
70enum cxacru_fw_request {
71 FW_CMD_ERR,
72 FW_GET_VER,
73 FW_READ_MEM,
74 FW_WRITE_MEM,
75 FW_RMW_MEM,
76 FW_CHECKSUM_MEM,
77 FW_GOTO_MEM,
78};
79
80/* commands for interaction with the modem through the control channel once
81 * firmware is loaded */
82enum cxacru_cm_request {
83 CM_REQUEST_UNDEFINED = 0x80,
84 CM_REQUEST_TEST,
85 CM_REQUEST_CHIP_GET_MAC_ADDRESS,
86 CM_REQUEST_CHIP_GET_DP_VERSIONS,
87 CM_REQUEST_CHIP_ADSL_LINE_START,
88 CM_REQUEST_CHIP_ADSL_LINE_STOP,
89 CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
90 CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
91 CM_REQUEST_CARD_INFO_GET,
92 CM_REQUEST_CARD_DATA_GET,
93 CM_REQUEST_CARD_DATA_SET,
94 CM_REQUEST_COMMAND_HW_IO,
95 CM_REQUEST_INTERFACE_HW_IO,
96 CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
97 CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
98 CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
99 CM_REQUEST_CARD_GET_STATUS,
100 CM_REQUEST_CARD_GET_MAC_ADDRESS,
101 CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
102 CM_REQUEST_MAX,
103};
104
105/* reply codes to the commands above */
106enum cxacru_cm_status {
107 CM_STATUS_UNDEFINED,
108 CM_STATUS_SUCCESS,
109 CM_STATUS_ERROR,
110 CM_STATUS_UNSUPPORTED,
111 CM_STATUS_UNIMPLEMENTED,
112 CM_STATUS_PARAMETER_ERROR,
113 CM_STATUS_DBG_LOOPBACK,
114 CM_STATUS_MAX,
115};
116
117/* indices into CARD_INFO_GET return array */
118enum cxacru_info_idx {
119 CXINF_DOWNSTREAM_RATE,
120 CXINF_UPSTREAM_RATE,
121 CXINF_LINK_STATUS,
122 CXINF_LINE_STATUS,
123 CXINF_MAC_ADDRESS_HIGH,
124 CXINF_MAC_ADDRESS_LOW,
125 CXINF_UPSTREAM_SNR_MARGIN,
126 CXINF_DOWNSTREAM_SNR_MARGIN,
127 CXINF_UPSTREAM_ATTENUATION,
128 CXINF_DOWNSTREAM_ATTENUATION,
129 CXINF_TRANSMITTER_POWER,
130 CXINF_UPSTREAM_BITS_PER_FRAME,
131 CXINF_DOWNSTREAM_BITS_PER_FRAME,
132 CXINF_STARTUP_ATTEMPTS,
133 CXINF_UPSTREAM_CRC_ERRORS,
134 CXINF_DOWNSTREAM_CRC_ERRORS,
135 CXINF_UPSTREAM_FEC_ERRORS,
136 CXINF_DOWNSTREAM_FEC_ERRORS,
137 CXINF_UPSTREAM_HEC_ERRORS,
138 CXINF_DOWNSTREAM_HEC_ERRORS,
139 CXINF_LINE_STARTABLE,
140 CXINF_MODULATION,
141 CXINF_ADSL_HEADEND,
142 CXINF_ADSL_HEADEND_ENVIRONMENT,
143 CXINF_CONTROLLER_VERSION,
144 /* dunno what the missing two mean */
145 CXINF_MAX = 0x1c,
146};
147
148struct cxacru_modem_type {
149 u32 pll_f_clk;
150 u32 pll_b_clk;
151 int boot_rom_patch;
152};
153
154struct cxacru_data {
155 struct usbatm_data *usbatm;
156
157 const struct cxacru_modem_type *modem_type;
158
159 int line_status;
160 struct work_struct poll_work;
161
162 /* contol handles */
163 struct semaphore cm_serialize;
164 u8 *rcv_buf;
165 u8 *snd_buf;
166 struct urb *rcv_urb;
167 struct urb *snd_urb;
168 struct completion rcv_done;
169 struct completion snd_done;
170};
171
172/* the following three functions are stolen from drivers/usb/core/message.c */
173static void cxacru_blocking_completion(struct urb *urb, struct pt_regs *regs)
174{
175 complete((struct completion *)urb->context);
176}
177
178static void cxacru_timeout_kill(unsigned long data)
179{
180 usb_unlink_urb((struct urb *) data);
181}
182
183static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
184 int* actual_length)
185{
186 struct timer_list timer;
187 int status;
188
189 init_timer(&timer);
190 timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
191 timer.data = (unsigned long) urb;
192 timer.function = cxacru_timeout_kill;
193 add_timer(&timer);
194 wait_for_completion(done);
195 status = urb->status;
196 if (status == -ECONNRESET)
197 status = -ETIMEDOUT;
198 del_timer_sync(&timer);
199
200 if (actual_length)
201 *actual_length = urb->actual_length;
202 return status;
203}
204
205static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
206 u8 *wdata, int wsize, u8 *rdata, int rsize)
207{
208 int ret, actlen;
209 int offb, offd;
210 const int stride = CMD_PACKET_SIZE - 4;
211 u8 *wbuf = instance->snd_buf;
212 u8 *rbuf = instance->rcv_buf;
213 int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
214 int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
215
216 if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
217 dbg("too big transfer requested");
218 ret = -ENOMEM;
219 goto fail;
220 }
221
222 down(&instance->cm_serialize);
223
224 /* submit reading urb before the writing one */
225 init_completion(&instance->rcv_done);
226 ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
227 if (ret < 0) {
228 dbg("submitting read urb for cm %#x failed", cm);
229 ret = ret;
230 goto fail;
231 }
232
233 memset(wbuf, 0, wbuflen);
234 /* handle wsize == 0 */
235 wbuf[0] = cm;
236 for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
237 wbuf[offb] = cm;
238 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
239 }
240
241 instance->snd_urb->transfer_buffer_length = wbuflen;
242 init_completion(&instance->snd_done);
243 ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
244 if (ret < 0) {
245 dbg("submitting write urb for cm %#x failed", cm);
246 ret = ret;
247 goto fail;
248 }
249
250 ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
251 if (ret < 0) {
252 dbg("sending cm %#x failed", cm);
253 ret = ret;
254 goto fail;
255 }
256
257 ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
258 if (ret < 0) {
259 dbg("receiving cm %#x failed", cm);
260 ret = ret;
261 goto fail;
262 }
263 if (actlen % CMD_PACKET_SIZE || !actlen) {
264 dbg("response is not a positive multiple of %d: %#x",
265 CMD_PACKET_SIZE, actlen);
266 ret = -EIO;
267 goto fail;
268 }
269
270 /* check the return status and copy the data to the output buffer, if needed */
271 for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
272 if (rbuf[offb] != cm) {
273 dbg("wrong cm %#x in response", rbuf[offb]);
274 ret = -EIO;
275 goto fail;
276 }
277 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
278 dbg("response failed: %#x", rbuf[offb + 1]);
279 ret = -EIO;
280 goto fail;
281 }
282 if (offd >= rsize)
283 break;
284 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
285 offd += stride;
286 }
287
288 ret = offd;
289 dbg("cm %#x", cm);
290fail:
291 up(&instance->cm_serialize);
292 return ret;
293}
294
295static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
296 u32 *data, int size)
297{
298 int ret, len;
299 u32 *buf;
300 int offb, offd;
301 const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
302 int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
303
304 buf = kmalloc(buflen, GFP_KERNEL);
305 if (!buf)
306 return -ENOMEM;
307
308 ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
309 if (ret < 0)
310 goto cleanup;
311
312 /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
313 len = ret / 4;
314 for (offb = 0; offb < len; ) {
315 int l = le32_to_cpu(buf[offb++]);
316 if (l > stride || l > (len - offb) / 2) {
317 dbg("wrong data length %#x in response", l);
318 ret = -EIO;
319 goto cleanup;
320 }
321 while (l--) {
322 offd = le32_to_cpu(buf[offb++]);
323 if (offd >= size) {
324 dbg("wrong index %#x in response", offd);
325 ret = -EIO;
326 goto cleanup;
327 }
328 data[offd] = le32_to_cpu(buf[offb++]);
329 }
330 }
331
332 ret = 0;
333
334cleanup:
335 kfree(buf);
336 return ret;
337}
338
339static int cxacru_card_status(struct cxacru_data *instance)
340{
341 int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
342 if (ret < 0) { /* firmware not loaded */
343 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
344 return ret;
345 }
346 return 0;
347}
348
349static void cxacru_poll_status(struct cxacru_data *instance);
350
351static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
352 struct atm_dev *atm_dev)
353{
354 struct cxacru_data *instance = usbatm_instance->driver_data;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200355 /*
356 struct atm_dev *atm_dev = usbatm_instance->atm_dev;
357 */
358 int ret;
359
360 dbg("cxacru_atm_start");
361
362 /* Read MAC address */
363 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
364 atm_dev->esi, sizeof(atm_dev->esi));
365 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100366 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200367 return ret;
368 }
369
370 /* start ADSL */
371 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
372 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100373 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200374 return ret;
375 }
376
377 /* Start status polling */
378 cxacru_poll_status(instance);
379 return 0;
380}
381
382static void cxacru_poll_status(struct cxacru_data *instance)
383{
384 u32 buf[CXINF_MAX] = {};
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100385 struct usbatm_data *usbatm = instance->usbatm;
386 struct atm_dev *atm_dev = usbatm->atm_dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200387 int ret;
388
389 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
390 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100391 atm_warn(usbatm, "poll status: error %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200392 goto reschedule;
393 }
394
395 if (instance->line_status == buf[CXINF_LINE_STATUS])
396 goto reschedule;
397
398 instance->line_status = buf[CXINF_LINE_STATUS];
399 switch (instance->line_status) {
400 case 0:
401 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100402 atm_info(usbatm, "ADSL line: down\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200403 break;
404
405 case 1:
406 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100407 atm_info(usbatm, "ADSL line: attempting to activate\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200408 break;
409
410 case 2:
411 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100412 atm_info(usbatm, "ADSL line: training\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200413 break;
414
415 case 3:
416 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100417 atm_info(usbatm, "ADSL line: channel analysis\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200418 break;
419
420 case 4:
421 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100422 atm_info(usbatm, "ADSL line: exchange\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200423 break;
424
425 case 5:
426 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
427 atm_dev->signal = ATM_PHY_SIG_FOUND;
428
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100429 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
Duncan Sands1b0e6142005-05-11 20:19:29 +0200430 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
431 break;
432
433 case 6:
434 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100435 atm_info(usbatm, "ADSL line: waiting\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200436 break;
437
438 case 7:
439 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100440 atm_info(usbatm, "ADSL line: initializing\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200441 break;
442
443 default:
444 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100445 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200446 break;
447 }
448reschedule:
449 schedule_delayed_work(&instance->poll_work, msecs_to_jiffies(POLL_INTERVAL));
450}
451
452static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
453 u8 code1, u8 code2, u32 addr, u8 *data, int size)
454{
455 int ret;
456 u8 *buf;
457 int offd, offb;
458 const int stride = CMD_PACKET_SIZE - 8;
459
460 buf = (u8 *) __get_free_page(GFP_KERNEL);
461 if (!buf)
462 return -ENOMEM;
463
464 offb = offd = 0;
465 do {
466 int l = min_t(int, stride, size - offd);
467 buf[offb++] = fw;
468 buf[offb++] = l;
469 buf[offb++] = code1;
470 buf[offb++] = code2;
471 *((u32 *) (buf + offb)) = cpu_to_le32(addr);
472 offb += 4;
473 addr += l;
474 if(l)
475 memcpy(buf + offb, data + offd, l);
476 if (l < stride)
477 memset(buf + offb + l, 0, stride - l);
478 offb += stride;
479 offd += stride;
480 if ((offb >= PAGE_SIZE) || (offd >= size)) {
481 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
482 buf, offb, NULL, CMD_TIMEOUT);
483 if (ret < 0) {
484 dbg("sending fw %#x failed", fw);
485 goto cleanup;
486 }
487 offb = 0;
488 }
489 } while(offd < size);
490 dbg("sent fw %#x", fw);
491
492 ret = 0;
493
494cleanup:
495 free_page((unsigned long) buf);
496 return ret;
497}
498
499static void cxacru_upload_firmware(struct cxacru_data *instance,
500 const struct firmware *fw,
501 const struct firmware *bp,
502 const struct firmware *cf)
503{
504 int ret;
505 int off;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100506 struct usbatm_data *usbatm = instance->usbatm;
507 struct usb_device *usb_dev = usbatm->usb_dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200508 u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct };
509 u32 val;
510
511 dbg("cxacru_upload_firmware");
512
513 /* FirmwarePllFClkValue */
514 val = cpu_to_le32(instance->modem_type->pll_f_clk);
515 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
516 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100517 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200518 return;
519 }
520
521 /* FirmwarePllBClkValue */
522 val = cpu_to_le32(instance->modem_type->pll_b_clk);
523 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
524 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100525 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200526 return;
527 }
528
529 /* Enable SDRAM */
530 val = cpu_to_le32(SDRAM_ENA);
531 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
532 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100533 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200534 return;
535 }
536
537 /* Firmware */
538 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
539 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100540 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200541 return;
542 }
543
544 /* Boot ROM patch */
545 if (instance->modem_type->boot_rom_patch) {
546 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
547 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100548 usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200549 return;
550 }
551 }
552
553 /* Signature */
554 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
555 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100556 usb_err(usbatm, "Signature storing failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200557 return;
558 }
559
560 if (instance->modem_type->boot_rom_patch) {
561 val = cpu_to_le32(BR_ADDR);
562 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
563 }
564 else {
565 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
566 }
567 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100568 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200569 return;
570 }
571
572 /* Delay to allow firmware to start up. */
573 msleep_interruptible(1000);
574
575 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
576 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
577 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
578 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
579
580 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
581 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100582 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200583 return;
584 }
585
586 /* Load config data (le32), doing one packet at a time */
587 if (cf)
588 for (off = 0; off < cf->size / 4; ) {
589 u32 buf[CMD_PACKET_SIZE / 4 - 1];
590 int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
591 buf[0] = cpu_to_le32(len);
592 for (i = 0; i < len; i++, off++) {
593 buf[i * 2 + 1] = cpu_to_le32(off);
594 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
595 }
596 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
597 (u8 *) buf, len, NULL, 0);
598 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100599 usb_err(usbatm, "load config data failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200600 return;
601 }
602 }
603
604 msleep_interruptible(4000);
605}
606
607static int cxacru_find_firmware(struct cxacru_data *instance,
608 char* phase, const struct firmware **fw_p)
609{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100610 struct usbatm_data *usbatm = instance->usbatm;
611 struct device *dev = &usbatm->usb_intf->dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200612 char buf[16];
613
614 sprintf(buf, "cxacru-%s.bin", phase);
615 dbg("cxacru_find_firmware: looking for %s", buf);
616
617 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100618 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200619 return -ENOENT;
620 }
621
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100622 usb_info(usbatm, "found firmware %s\n", buf);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200623
624 return 0;
625}
626
627static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
628 struct usb_interface *usb_intf)
629{
Duncan Sands1b0e6142005-05-11 20:19:29 +0200630 const struct firmware *fw, *bp, *cf;
631 struct cxacru_data *instance = usbatm_instance->driver_data;
632
633 int ret = cxacru_find_firmware(instance, "fw", &fw);
634 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100635 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200636 return ret;
637 }
638
639 if (instance->modem_type->boot_rom_patch) {
640 ret = cxacru_find_firmware(instance, "bp", &bp);
641 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100642 usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200643 release_firmware(fw);
644 return ret;
645 }
646 }
647
648 if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */
649 cf = NULL;
650
651 cxacru_upload_firmware(instance, fw, bp, cf);
652
653 if (cf)
654 release_firmware(cf);
655 if (instance->modem_type->boot_rom_patch)
656 release_firmware(bp);
657 release_firmware(fw);
658
659 ret = cxacru_card_status(instance);
660 if (ret)
661 dbg("modem initialisation failed");
662 else
663 dbg("done setting up the modem");
664
665 return ret;
666}
667
668static int cxacru_bind(struct usbatm_data *usbatm_instance,
669 struct usb_interface *intf, const struct usb_device_id *id,
670 int *need_heavy_init)
671{
672 struct cxacru_data *instance;
673 struct usb_device *usb_dev = interface_to_usbdev(intf);
674 int ret;
675
676 /* instance init */
677 instance = kmalloc(sizeof(*instance), GFP_KERNEL);
678 if (!instance) {
679 dbg("cxacru_bind: no memory for instance data");
680 return -ENOMEM;
681 }
682
683 memset(instance, 0, sizeof(*instance));
684
685 instance->usbatm = usbatm_instance;
686 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
687
688 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
689 if (!instance->rcv_buf) {
690 dbg("cxacru_bind: no memory for rcv_buf");
691 ret = -ENOMEM;
692 goto fail;
693 }
694 instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
695 if (!instance->snd_buf) {
696 dbg("cxacru_bind: no memory for snd_buf");
697 ret = -ENOMEM;
698 goto fail;
699 }
700 instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
701 if (!instance->rcv_urb) {
702 dbg("cxacru_bind: no memory for rcv_urb");
703 ret = -ENOMEM;
704 goto fail;
705 }
706 instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
707 if (!instance->snd_urb) {
708 dbg("cxacru_bind: no memory for snd_urb");
709 ret = -ENOMEM;
710 goto fail;
711 }
712
713 usb_fill_int_urb(instance->rcv_urb,
714 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
715 instance->rcv_buf, PAGE_SIZE,
716 cxacru_blocking_completion, &instance->rcv_done, 1);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200717
718 usb_fill_int_urb(instance->snd_urb,
719 usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
720 instance->snd_buf, PAGE_SIZE,
721 cxacru_blocking_completion, &instance->snd_done, 4);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200722
723 init_MUTEX(&instance->cm_serialize);
724
725 INIT_WORK(&instance->poll_work, (void *)cxacru_poll_status, instance);
726
727 usbatm_instance->driver_data = instance;
728
729 *need_heavy_init = cxacru_card_status(instance);
730
731 return 0;
732
733 fail:
734 free_page((unsigned long) instance->snd_buf);
735 free_page((unsigned long) instance->rcv_buf);
736 usb_free_urb(instance->snd_urb);
737 usb_free_urb(instance->rcv_urb);
738 kfree(instance);
739
740 return ret;
741}
742
743static void cxacru_unbind(struct usbatm_data *usbatm_instance,
744 struct usb_interface *intf)
745{
746 struct cxacru_data *instance = usbatm_instance->driver_data;
747
748 dbg("cxacru_unbind entered");
749
750 if (!instance) {
751 dbg("cxacru_unbind: NULL instance!");
752 return;
753 }
754
755 while (!cancel_delayed_work(&instance->poll_work))
756 flush_scheduled_work();
757
758 usb_kill_urb(instance->snd_urb);
759 usb_kill_urb(instance->rcv_urb);
760 usb_free_urb(instance->snd_urb);
761 usb_free_urb(instance->rcv_urb);
762
763 free_page((unsigned long) instance->snd_buf);
764 free_page((unsigned long) instance->rcv_buf);
765 kfree(instance);
766
767 usbatm_instance->driver_data = NULL;
768}
769
770static const struct cxacru_modem_type cxacru_cafe = {
771 .pll_f_clk = 0x02d874df,
772 .pll_b_clk = 0x0196a51a,
773 .boot_rom_patch = 1,
774};
775
776static const struct cxacru_modem_type cxacru_cb00 = {
777 .pll_f_clk = 0x5,
778 .pll_b_clk = 0x3,
779 .boot_rom_patch = 0,
780};
781
782static const struct usb_device_id cxacru_usb_ids[] = {
783 { /* V = Conexant P = ADSL modem (Euphrates project) */
784 USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
785 },
786 { /* V = Conexant P = ADSL modem (Hasbani project) */
787 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
788 },
Duncan Sands1b0e6142005-05-11 20:19:29 +0200789 { /* V = Conexant P = ADSL modem */
790 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
791 },
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100792 { /* V = Conexant P = ADSL modem (Well PTI-800) */
793 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
794 },
Duncan Sands1b0e6142005-05-11 20:19:29 +0200795 { /* V = Conexant P = ADSL modem */
796 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
797 },
798 { /* V = Olitec P = ADSL modem version 2 */
799 USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
800 },
801 { /* V = Olitec P = ADSL modem version 3 */
802 USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
803 },
804 { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
805 USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
806 },
807 { /* V = Zoom P = 5510 */
808 USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
809 },
810 { /* V = Draytek P = Vigor 318 */
811 USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
812 },
813 { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
814 USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
815 },
816 { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
817 USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
818 },
819 { /* V = Aethra P = Starmodem UM1020 */
820 USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
821 },
822 { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
823 USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
824 },
825 { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
826 USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
827 },
828 { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
829 USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
830 },
831 {}
832};
833
834MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
835
836static struct usbatm_driver cxacru_driver = {
837 .owner = THIS_MODULE,
838 .driver_name = cxacru_driver_name,
839 .bind = cxacru_bind,
840 .heavy_init = cxacru_heavy_init,
841 .unbind = cxacru_unbind,
842 .atm_start = cxacru_atm_start,
843 .in = CXACRU_EP_DATA,
844 .out = CXACRU_EP_DATA,
845 .rx_padding = 3,
846 .tx_padding = 11,
847};
848
849static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
850{
851 return usbatm_usb_probe(intf, id, &cxacru_driver);
852}
853
854static struct usb_driver cxacru_usb_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +0200855 .name = cxacru_driver_name,
856 .probe = cxacru_usb_probe,
857 .disconnect = usbatm_usb_disconnect,
858 .id_table = cxacru_usb_ids
859};
860
861static int __init cxacru_init(void)
862{
863 return usb_register(&cxacru_usb_driver);
864}
865
866static void __exit cxacru_cleanup(void)
867{
868 usb_deregister(&cxacru_usb_driver);
869}
870
871module_init(cxacru_init);
872module_exit(cxacru_cleanup);
873
874MODULE_AUTHOR(DRIVER_AUTHOR);
875MODULE_DESCRIPTION(DRIVER_DESC);
876MODULE_LICENSE("GPL");
877MODULE_VERSION(DRIVER_VERSION);