cxacru.c 37 KB

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