cxacru.c 30 KB

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