cxacru.c 39 KB

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