cxacru.c 39 KB

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