speedtch.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /******************************************************************************
  2. * speedtch.c - Alcatel SpeedTouch USB xDSL modem driver
  3. *
  4. * Copyright (C) 2001, Alcatel
  5. * Copyright (C) 2003, Duncan Sands
  6. * Copyright (C) 2004, David Woodhouse
  7. *
  8. * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
  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. #include <asm/page.h>
  26. #include <linux/device.h>
  27. #include <linux/errno.h>
  28. #include <linux/firmware.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/slab.h>
  33. #include <linux/stat.h>
  34. #include <linux/timer.h>
  35. #include <linux/types.h>
  36. #include <linux/usb/ch9.h>
  37. #include <linux/workqueue.h>
  38. #include "usbatm.h"
  39. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  40. #define DRIVER_VERSION "1.10"
  41. #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
  42. static const char speedtch_driver_name[] = "speedtch";
  43. #define CTRL_TIMEOUT 2000 /* milliseconds */
  44. #define DATA_TIMEOUT 2000 /* milliseconds */
  45. #define OFFSET_7 0 /* size 1 */
  46. #define OFFSET_b 1 /* size 8 */
  47. #define OFFSET_d 9 /* size 4 */
  48. #define OFFSET_e 13 /* size 1 */
  49. #define OFFSET_f 14 /* size 1 */
  50. #define SIZE_7 1
  51. #define SIZE_b 8
  52. #define SIZE_d 4
  53. #define SIZE_e 1
  54. #define SIZE_f 1
  55. #define MIN_POLL_DELAY 5000 /* milliseconds */
  56. #define MAX_POLL_DELAY 60000 /* milliseconds */
  57. #define RESUBMIT_DELAY 1000 /* milliseconds */
  58. #define DEFAULT_BULK_ALTSETTING 1
  59. #define DEFAULT_ISOC_ALTSETTING 3
  60. #define DEFAULT_DL_512_FIRST 0
  61. #define DEFAULT_ENABLE_ISOC 0
  62. #define DEFAULT_SW_BUFFERING 0
  63. static unsigned int altsetting = 0; /* zero means: use the default */
  64. static bool dl_512_first = DEFAULT_DL_512_FIRST;
  65. static bool enable_isoc = DEFAULT_ENABLE_ISOC;
  66. static bool sw_buffering = DEFAULT_SW_BUFFERING;
  67. #define DEFAULT_B_MAX_DSL 8128
  68. #define DEFAULT_MODEM_MODE 11
  69. #define MODEM_OPTION_LENGTH 16
  70. static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
  71. 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  72. };
  73. static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
  74. static unsigned char ModemMode = DEFAULT_MODEM_MODE;
  75. static unsigned char ModemOption[MODEM_OPTION_LENGTH];
  76. static unsigned int num_ModemOption;
  77. module_param(altsetting, uint, S_IRUGO | S_IWUSR);
  78. MODULE_PARM_DESC(altsetting,
  79. "Alternative setting for data interface (bulk_default: "
  80. __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
  81. __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
  82. module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
  83. MODULE_PARM_DESC(dl_512_first,
  84. "Read 512 bytes before sending firmware (default: "
  85. __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
  86. module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
  87. MODULE_PARM_DESC(enable_isoc,
  88. "Use isochronous transfers if available (default: "
  89. __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
  90. module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
  91. MODULE_PARM_DESC(sw_buffering,
  92. "Enable software buffering (default: "
  93. __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
  94. module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(BMaxDSL,
  96. "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
  97. module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
  98. MODULE_PARM_DESC(ModemMode,
  99. "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
  100. module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
  101. MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
  102. #define INTERFACE_DATA 1
  103. #define ENDPOINT_INT 0x81
  104. #define ENDPOINT_BULK_DATA 0x07
  105. #define ENDPOINT_ISOC_DATA 0x07
  106. #define ENDPOINT_FIRMWARE 0x05
  107. struct speedtch_params {
  108. unsigned int altsetting;
  109. unsigned int BMaxDSL;
  110. unsigned char ModemMode;
  111. unsigned char ModemOption[MODEM_OPTION_LENGTH];
  112. };
  113. struct speedtch_instance_data {
  114. struct usbatm_data *usbatm;
  115. struct speedtch_params params; /* set in probe, constant afterwards */
  116. struct timer_list status_check_timer;
  117. struct work_struct status_check_work;
  118. unsigned char last_status;
  119. int poll_delay; /* milliseconds */
  120. struct timer_list resubmit_timer;
  121. struct urb *int_urb;
  122. unsigned char int_data[16];
  123. unsigned char scratch_buffer[16];
  124. };
  125. /***************
  126. ** firmware **
  127. ***************/
  128. static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
  129. {
  130. struct usbatm_data *usbatm = instance->usbatm;
  131. struct usb_device *usb_dev = usbatm->usb_dev;
  132. int ret;
  133. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  134. 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
  135. if (ret < 0)
  136. usb_warn(usbatm,
  137. "%sabling SW buffering: usb_control_msg returned %d\n",
  138. state ? "En" : "Dis", ret);
  139. else
  140. usb_dbg(usbatm, "speedtch_set_swbuff: %sbled SW buffering\n", state ? "En" : "Dis");
  141. }
  142. static void speedtch_test_sequence(struct speedtch_instance_data *instance)
  143. {
  144. struct usbatm_data *usbatm = instance->usbatm;
  145. struct usb_device *usb_dev = usbatm->usb_dev;
  146. unsigned char *buf = instance->scratch_buffer;
  147. int ret;
  148. /* URB 147 */
  149. buf[0] = 0x1c;
  150. buf[1] = 0x50;
  151. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  152. 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
  153. if (ret < 0)
  154. usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
  155. /* URB 148 */
  156. buf[0] = 0x32;
  157. buf[1] = 0x00;
  158. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  159. 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
  160. if (ret < 0)
  161. usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
  162. /* URB 149 */
  163. buf[0] = 0x01;
  164. buf[1] = 0x00;
  165. buf[2] = 0x01;
  166. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  167. 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
  168. if (ret < 0)
  169. usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
  170. /* URB 150 */
  171. buf[0] = 0x01;
  172. buf[1] = 0x00;
  173. buf[2] = 0x01;
  174. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  175. 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
  176. if (ret < 0)
  177. usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
  178. /* Extra initialisation in recent drivers - gives higher speeds */
  179. /* URBext1 */
  180. buf[0] = instance->params.ModemMode;
  181. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  182. 0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
  183. if (ret < 0)
  184. usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
  185. /* URBext2 */
  186. /* This seems to be the one which actually triggers the higher sync
  187. rate -- it does require the new firmware too, although it works OK
  188. with older firmware */
  189. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  190. 0x01, 0x40, 0x14, 0x00,
  191. instance->params.ModemOption,
  192. MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
  193. if (ret < 0)
  194. usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
  195. /* URBext3 */
  196. buf[0] = instance->params.BMaxDSL & 0xff;
  197. buf[1] = instance->params.BMaxDSL >> 8;
  198. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  199. 0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
  200. if (ret < 0)
  201. usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
  202. }
  203. static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
  204. const struct firmware *fw1,
  205. const struct firmware *fw2)
  206. {
  207. unsigned char *buffer;
  208. struct usbatm_data *usbatm = instance->usbatm;
  209. struct usb_device *usb_dev = usbatm->usb_dev;
  210. int actual_length;
  211. int ret = 0;
  212. int offset;
  213. usb_dbg(usbatm, "%s entered\n", __func__);
  214. buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
  215. if (!buffer) {
  216. ret = -ENOMEM;
  217. usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
  218. goto out;
  219. }
  220. if (!usb_ifnum_to_if(usb_dev, 2)) {
  221. ret = -ENODEV;
  222. usb_dbg(usbatm, "%s: interface not found!\n", __func__);
  223. goto out_free;
  224. }
  225. /* URB 7 */
  226. if (dl_512_first) { /* some modems need a read before writing the firmware */
  227. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  228. buffer, 0x200, &actual_length, 2000);
  229. if (ret < 0 && ret != -ETIMEDOUT)
  230. usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
  231. else
  232. usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
  233. }
  234. /* URB 8 : both leds are static green */
  235. for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
  236. int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
  237. memcpy(buffer, fw1->data + offset, thislen);
  238. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  239. buffer, thislen, &actual_length, DATA_TIMEOUT);
  240. if (ret < 0) {
  241. usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
  242. goto out_free;
  243. }
  244. usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
  245. }
  246. /* USB led blinking green, ADSL led off */
  247. /* URB 11 */
  248. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  249. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  250. if (ret < 0) {
  251. usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
  252. goto out_free;
  253. }
  254. usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
  255. /* URBs 12 to 139 - USB led blinking green, ADSL led off */
  256. for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
  257. int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
  258. memcpy(buffer, fw2->data + offset, thislen);
  259. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  260. buffer, thislen, &actual_length, DATA_TIMEOUT);
  261. if (ret < 0) {
  262. usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
  263. goto out_free;
  264. }
  265. }
  266. usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
  267. /* USB led static green, ADSL led static red */
  268. /* URB 142 */
  269. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  270. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  271. if (ret < 0) {
  272. usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
  273. goto out_free;
  274. }
  275. /* success */
  276. usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
  277. /* Delay to allow firmware to start up. We can do this here
  278. because we're in our own kernel thread anyway. */
  279. msleep_interruptible(1000);
  280. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
  281. usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
  282. goto out_free;
  283. }
  284. /* Enable software buffering, if requested */
  285. if (sw_buffering)
  286. speedtch_set_swbuff(instance, 1);
  287. /* Magic spell; don't ask us what this does */
  288. speedtch_test_sequence(instance);
  289. ret = 0;
  290. out_free:
  291. free_page((unsigned long)buffer);
  292. out:
  293. return ret;
  294. }
  295. static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
  296. int phase, const struct firmware **fw_p)
  297. {
  298. struct device *dev = &intf->dev;
  299. const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
  300. const u8 major_revision = bcdDevice >> 8;
  301. const u8 minor_revision = bcdDevice & 0xff;
  302. char buf[24];
  303. sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
  304. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  305. if (request_firmware(fw_p, buf, dev)) {
  306. sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
  307. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  308. if (request_firmware(fw_p, buf, dev)) {
  309. sprintf(buf, "speedtch-%d.bin", phase);
  310. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  311. if (request_firmware(fw_p, buf, dev)) {
  312. usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
  313. return -ENOENT;
  314. }
  315. }
  316. }
  317. usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
  318. return 0;
  319. }
  320. static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
  321. {
  322. const struct firmware *fw1, *fw2;
  323. struct speedtch_instance_data *instance = usbatm->driver_data;
  324. int ret;
  325. if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
  326. return ret;
  327. if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
  328. release_firmware(fw1);
  329. return ret;
  330. }
  331. if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
  332. usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
  333. release_firmware(fw2);
  334. release_firmware(fw1);
  335. return ret;
  336. }
  337. /**********
  338. ** ATM **
  339. **********/
  340. static int speedtch_read_status(struct speedtch_instance_data *instance)
  341. {
  342. struct usbatm_data *usbatm = instance->usbatm;
  343. struct usb_device *usb_dev = usbatm->usb_dev;
  344. unsigned char *buf = instance->scratch_buffer;
  345. int ret;
  346. memset(buf, 0, 16);
  347. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  348. 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
  349. CTRL_TIMEOUT);
  350. if (ret < 0) {
  351. atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
  352. return ret;
  353. }
  354. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  355. 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
  356. CTRL_TIMEOUT);
  357. if (ret < 0) {
  358. atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
  359. return ret;
  360. }
  361. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  362. 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
  363. CTRL_TIMEOUT);
  364. if (ret < 0) {
  365. atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
  366. return ret;
  367. }
  368. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  369. 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
  370. CTRL_TIMEOUT);
  371. if (ret < 0) {
  372. atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
  373. return ret;
  374. }
  375. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  376. 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
  377. CTRL_TIMEOUT);
  378. if (ret < 0) {
  379. atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
  380. return ret;
  381. }
  382. return 0;
  383. }
  384. static int speedtch_start_synchro(struct speedtch_instance_data *instance)
  385. {
  386. struct usbatm_data *usbatm = instance->usbatm;
  387. struct usb_device *usb_dev = usbatm->usb_dev;
  388. unsigned char *buf = instance->scratch_buffer;
  389. int ret;
  390. atm_dbg(usbatm, "%s entered\n", __func__);
  391. memset(buf, 0, 2);
  392. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  393. 0x12, 0xc0, 0x04, 0x00,
  394. buf, 2, CTRL_TIMEOUT);
  395. if (ret < 0)
  396. atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
  397. else
  398. atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
  399. __func__, ret, buf[0], buf[1]);
  400. return ret;
  401. }
  402. static void speedtch_check_status(struct work_struct *work)
  403. {
  404. struct speedtch_instance_data *instance =
  405. container_of(work, struct speedtch_instance_data,
  406. status_check_work);
  407. struct usbatm_data *usbatm = instance->usbatm;
  408. struct atm_dev *atm_dev = usbatm->atm_dev;
  409. unsigned char *buf = instance->scratch_buffer;
  410. int down_speed, up_speed, ret;
  411. unsigned char status;
  412. #ifdef VERBOSE_DEBUG
  413. atm_dbg(usbatm, "%s entered\n", __func__);
  414. #endif
  415. ret = speedtch_read_status(instance);
  416. if (ret < 0) {
  417. atm_warn(usbatm, "error %d fetching device status\n", ret);
  418. instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
  419. return;
  420. }
  421. instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
  422. status = buf[OFFSET_7];
  423. if ((status != instance->last_status) || !status) {
  424. atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
  425. switch (status) {
  426. case 0:
  427. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
  428. if (instance->last_status)
  429. atm_info(usbatm, "ADSL line is down\n");
  430. /* It may never resync again unless we ask it to... */
  431. ret = speedtch_start_synchro(instance);
  432. break;
  433. case 0x08:
  434. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
  435. atm_info(usbatm, "ADSL line is blocked?\n");
  436. break;
  437. case 0x10:
  438. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
  439. atm_info(usbatm, "ADSL line is synchronising\n");
  440. break;
  441. case 0x20:
  442. down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
  443. | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
  444. up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
  445. | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
  446. if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
  447. down_speed >>= 16;
  448. up_speed >>= 16;
  449. }
  450. atm_dev->link_rate = down_speed * 1000 / 424;
  451. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
  452. atm_info(usbatm,
  453. "ADSL line is up (%d kb/s down | %d kb/s up)\n",
  454. down_speed, up_speed);
  455. break;
  456. default:
  457. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
  458. atm_info(usbatm, "unknown line state %02x\n", status);
  459. break;
  460. }
  461. instance->last_status = status;
  462. }
  463. }
  464. static void speedtch_status_poll(unsigned long data)
  465. {
  466. struct speedtch_instance_data *instance = (void *)data;
  467. schedule_work(&instance->status_check_work);
  468. /* The following check is racy, but the race is harmless */
  469. if (instance->poll_delay < MAX_POLL_DELAY)
  470. mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
  471. else
  472. atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
  473. }
  474. static void speedtch_resubmit_int(unsigned long data)
  475. {
  476. struct speedtch_instance_data *instance = (void *)data;
  477. struct urb *int_urb = instance->int_urb;
  478. int ret;
  479. atm_dbg(instance->usbatm, "%s entered\n", __func__);
  480. if (int_urb) {
  481. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  482. if (!ret)
  483. schedule_work(&instance->status_check_work);
  484. else {
  485. atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  486. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  487. }
  488. }
  489. }
  490. static void speedtch_handle_int(struct urb *int_urb)
  491. {
  492. struct speedtch_instance_data *instance = int_urb->context;
  493. struct usbatm_data *usbatm = instance->usbatm;
  494. unsigned int count = int_urb->actual_length;
  495. int status = int_urb->status;
  496. int ret;
  497. /* The magic interrupt for "up state" */
  498. static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
  499. /* The magic interrupt for "down state" */
  500. static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  501. atm_dbg(usbatm, "%s entered\n", __func__);
  502. if (status < 0) {
  503. atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
  504. goto fail;
  505. }
  506. if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
  507. del_timer(&instance->status_check_timer);
  508. atm_info(usbatm, "DSL line goes up\n");
  509. } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
  510. atm_info(usbatm, "DSL line goes down\n");
  511. } else {
  512. int i;
  513. atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
  514. for (i = 0; i < count; i++)
  515. printk(" %02x", instance->int_data[i]);
  516. printk("\n");
  517. goto fail;
  518. }
  519. int_urb = instance->int_urb;
  520. if (int_urb) {
  521. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  522. schedule_work(&instance->status_check_work);
  523. if (ret < 0) {
  524. atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  525. goto fail;
  526. }
  527. }
  528. return;
  529. fail:
  530. int_urb = instance->int_urb;
  531. if (int_urb)
  532. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  533. }
  534. static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  535. {
  536. struct usb_device *usb_dev = usbatm->usb_dev;
  537. struct speedtch_instance_data *instance = usbatm->driver_data;
  538. int i, ret;
  539. unsigned char mac_str[13];
  540. atm_dbg(usbatm, "%s entered\n", __func__);
  541. /* Set MAC address, it is stored in the serial number */
  542. memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
  543. if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
  544. for (i = 0; i < 6; i++)
  545. atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
  546. hex_to_bin(mac_str[i * 2 + 1]);
  547. }
  548. /* Start modem synchronisation */
  549. ret = speedtch_start_synchro(instance);
  550. /* Set up interrupt endpoint */
  551. if (instance->int_urb) {
  552. ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
  553. if (ret < 0) {
  554. /* Doesn't matter; we'll poll anyway */
  555. atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
  556. usb_free_urb(instance->int_urb);
  557. instance->int_urb = NULL;
  558. }
  559. }
  560. /* Start status polling */
  561. mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(1000));
  562. return 0;
  563. }
  564. static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  565. {
  566. struct speedtch_instance_data *instance = usbatm->driver_data;
  567. struct urb *int_urb = instance->int_urb;
  568. atm_dbg(usbatm, "%s entered\n", __func__);
  569. del_timer_sync(&instance->status_check_timer);
  570. /*
  571. * Since resubmit_timer and int_urb can schedule themselves and
  572. * each other, shutting them down correctly takes some care
  573. */
  574. instance->int_urb = NULL; /* signal shutdown */
  575. mb();
  576. usb_kill_urb(int_urb);
  577. del_timer_sync(&instance->resubmit_timer);
  578. /*
  579. * At this point, speedtch_handle_int and speedtch_resubmit_int
  580. * can run or be running, but instance->int_urb == NULL means that
  581. * they will not reschedule
  582. */
  583. usb_kill_urb(int_urb);
  584. del_timer_sync(&instance->resubmit_timer);
  585. usb_free_urb(int_urb);
  586. flush_work(&instance->status_check_work);
  587. }
  588. static int speedtch_pre_reset(struct usb_interface *intf)
  589. {
  590. return 0;
  591. }
  592. static int speedtch_post_reset(struct usb_interface *intf)
  593. {
  594. return 0;
  595. }
  596. /**********
  597. ** USB **
  598. **********/
  599. static struct usb_device_id speedtch_usb_ids[] = {
  600. {USB_DEVICE(0x06b9, 0x4061)},
  601. {}
  602. };
  603. MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
  604. static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
  605. static struct usb_driver speedtch_usb_driver = {
  606. .name = speedtch_driver_name,
  607. .probe = speedtch_usb_probe,
  608. .disconnect = usbatm_usb_disconnect,
  609. .pre_reset = speedtch_pre_reset,
  610. .post_reset = speedtch_post_reset,
  611. .id_table = speedtch_usb_ids
  612. };
  613. static void speedtch_release_interfaces(struct usb_device *usb_dev,
  614. int num_interfaces)
  615. {
  616. struct usb_interface *cur_intf;
  617. int i;
  618. for (i = 0; i < num_interfaces; i++) {
  619. cur_intf = usb_ifnum_to_if(usb_dev, i);
  620. if (cur_intf) {
  621. usb_set_intfdata(cur_intf, NULL);
  622. usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
  623. }
  624. }
  625. }
  626. static int speedtch_bind(struct usbatm_data *usbatm,
  627. struct usb_interface *intf,
  628. const struct usb_device_id *id)
  629. {
  630. struct usb_device *usb_dev = interface_to_usbdev(intf);
  631. struct usb_interface *cur_intf, *data_intf;
  632. struct speedtch_instance_data *instance;
  633. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  634. int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
  635. int i, ret;
  636. int use_isoc;
  637. usb_dbg(usbatm, "%s entered\n", __func__);
  638. /* sanity checks */
  639. if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  640. usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
  641. return -ENODEV;
  642. }
  643. data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA);
  644. if (!data_intf) {
  645. usb_err(usbatm, "%s: data interface not found!\n", __func__);
  646. return -ENODEV;
  647. }
  648. /* claim all interfaces */
  649. for (i = 0; i < num_interfaces; i++) {
  650. cur_intf = usb_ifnum_to_if(usb_dev, i);
  651. if ((i != ifnum) && cur_intf) {
  652. ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
  653. if (ret < 0) {
  654. usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
  655. speedtch_release_interfaces(usb_dev, i);
  656. return ret;
  657. }
  658. }
  659. }
  660. instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  661. if (!instance) {
  662. usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
  663. ret = -ENOMEM;
  664. goto fail_release;
  665. }
  666. instance->usbatm = usbatm;
  667. /* module parameters may change at any moment, so take a snapshot */
  668. instance->params.altsetting = altsetting;
  669. instance->params.BMaxDSL = BMaxDSL;
  670. instance->params.ModemMode = ModemMode;
  671. memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
  672. memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
  673. use_isoc = enable_isoc;
  674. if (instance->params.altsetting)
  675. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
  676. usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
  677. instance->params.altsetting = 0; /* fall back to default */
  678. }
  679. if (!instance->params.altsetting && use_isoc)
  680. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
  681. usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
  682. use_isoc = 0; /* fall back to bulk */
  683. }
  684. if (use_isoc) {
  685. const struct usb_host_interface *desc = data_intf->cur_altsetting;
  686. const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
  687. use_isoc = 0; /* fall back to bulk if endpoint not found */
  688. for (i = 0; i < desc->desc.bNumEndpoints; i++) {
  689. const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
  690. if ((endpoint_desc->bEndpointAddress == target_address)) {
  691. use_isoc =
  692. usb_endpoint_xfer_isoc(endpoint_desc);
  693. break;
  694. }
  695. }
  696. if (!use_isoc)
  697. usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
  698. }
  699. if (!use_isoc && !instance->params.altsetting)
  700. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
  701. usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
  702. goto fail_free;
  703. }
  704. if (!instance->params.altsetting)
  705. instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
  706. usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
  707. INIT_WORK(&instance->status_check_work, speedtch_check_status);
  708. init_timer(&instance->status_check_timer);
  709. instance->status_check_timer.function = speedtch_status_poll;
  710. instance->status_check_timer.data = (unsigned long)instance;
  711. instance->last_status = 0xff;
  712. instance->poll_delay = MIN_POLL_DELAY;
  713. init_timer(&instance->resubmit_timer);
  714. instance->resubmit_timer.function = speedtch_resubmit_int;
  715. instance->resubmit_timer.data = (unsigned long)instance;
  716. instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  717. if (instance->int_urb)
  718. usb_fill_int_urb(instance->int_urb, usb_dev,
  719. usb_rcvintpipe(usb_dev, ENDPOINT_INT),
  720. instance->int_data, sizeof(instance->int_data),
  721. speedtch_handle_int, instance, 16);
  722. else
  723. usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
  724. /* check whether the modem already seems to be alive */
  725. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  726. 0x12, 0xc0, 0x07, 0x00,
  727. instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
  728. usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
  729. usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
  730. if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
  731. if ((ret = usb_reset_device(usb_dev)) < 0) {
  732. usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
  733. goto fail_free;
  734. }
  735. usbatm->driver_data = instance;
  736. return 0;
  737. fail_free:
  738. usb_free_urb(instance->int_urb);
  739. kfree(instance);
  740. fail_release:
  741. speedtch_release_interfaces(usb_dev, num_interfaces);
  742. return ret;
  743. }
  744. static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
  745. {
  746. struct usb_device *usb_dev = interface_to_usbdev(intf);
  747. struct speedtch_instance_data *instance = usbatm->driver_data;
  748. usb_dbg(usbatm, "%s entered\n", __func__);
  749. speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
  750. usb_free_urb(instance->int_urb);
  751. kfree(instance);
  752. }
  753. /***********
  754. ** init **
  755. ***********/
  756. static struct usbatm_driver speedtch_usbatm_driver = {
  757. .driver_name = speedtch_driver_name,
  758. .bind = speedtch_bind,
  759. .heavy_init = speedtch_heavy_init,
  760. .unbind = speedtch_unbind,
  761. .atm_start = speedtch_atm_start,
  762. .atm_stop = speedtch_atm_stop,
  763. .bulk_in = ENDPOINT_BULK_DATA,
  764. .bulk_out = ENDPOINT_BULK_DATA,
  765. .isoc_in = ENDPOINT_ISOC_DATA
  766. };
  767. static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  768. {
  769. return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
  770. }
  771. module_usb_driver(speedtch_usb_driver);
  772. MODULE_AUTHOR(DRIVER_AUTHOR);
  773. MODULE_DESCRIPTION(DRIVER_DESC);
  774. MODULE_LICENSE("GPL");
  775. MODULE_VERSION(DRIVER_VERSION);