speedtch.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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/gfp.h>
  30. #include <linux/init.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/slab.h>
  35. #include <linux/stat.h>
  36. #include <linux/timer.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.9"
  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 TOTAL 15
  51. #define SIZE_7 1
  52. #define SIZE_b 8
  53. #define SIZE_d 4
  54. #define SIZE_e 1
  55. #define SIZE_f 1
  56. #define MIN_POLL_DELAY 5000 /* milliseconds */
  57. #define MAX_POLL_DELAY 60000 /* milliseconds */
  58. #define RESUBMIT_DELAY 1000 /* milliseconds */
  59. #define DEFAULT_ALTSETTING 1
  60. #define DEFAULT_DL_512_FIRST 0
  61. #define DEFAULT_SW_BUFFERING 0
  62. static int altsetting = DEFAULT_ALTSETTING;
  63. static int dl_512_first = DEFAULT_DL_512_FIRST;
  64. static int sw_buffering = DEFAULT_SW_BUFFERING;
  65. module_param(altsetting, int, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(altsetting,
  67. "Alternative setting for data interface (default: "
  68. __MODULE_STRING(DEFAULT_ALTSETTING) ")");
  69. module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
  70. MODULE_PARM_DESC(dl_512_first,
  71. "Read 512 bytes before sending firmware (default: "
  72. __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
  73. module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
  74. MODULE_PARM_DESC(sw_buffering,
  75. "Enable software buffering (default: "
  76. __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
  77. #define ENDPOINT_INT 0x81
  78. #define ENDPOINT_DATA 0x07
  79. #define ENDPOINT_FIRMWARE 0x05
  80. #define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
  81. struct speedtch_instance_data {
  82. struct usbatm_data *usbatm;
  83. struct work_struct status_checker;
  84. unsigned char last_status;
  85. int poll_delay; /* milliseconds */
  86. struct timer_list resubmit_timer;
  87. struct urb *int_urb;
  88. unsigned char int_data[16];
  89. unsigned char scratch_buffer[TOTAL];
  90. };
  91. /***************
  92. ** firmware **
  93. ***************/
  94. static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
  95. {
  96. struct usbatm_data *usbatm = instance->usbatm;
  97. struct usb_device *usb_dev = usbatm->usb_dev;
  98. int ret;
  99. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  100. 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
  101. if (ret < 0)
  102. usb_warn(usbatm,
  103. "%sabling SW buffering: usb_control_msg returned %d\n",
  104. state ? "En" : "Dis", ret);
  105. else
  106. dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
  107. }
  108. static void speedtch_test_sequence(struct speedtch_instance_data *instance)
  109. {
  110. struct usbatm_data *usbatm = instance->usbatm;
  111. struct usb_device *usb_dev = usbatm->usb_dev;
  112. unsigned char *buf = instance->scratch_buffer;
  113. int ret;
  114. /* URB 147 */
  115. buf[0] = 0x1c;
  116. buf[1] = 0x50;
  117. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  118. 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
  119. if (ret < 0)
  120. usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
  121. /* URB 148 */
  122. buf[0] = 0x32;
  123. buf[1] = 0x00;
  124. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  125. 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
  126. if (ret < 0)
  127. usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
  128. /* URB 149 */
  129. buf[0] = 0x01;
  130. buf[1] = 0x00;
  131. buf[2] = 0x01;
  132. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  133. 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
  134. if (ret < 0)
  135. usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
  136. /* URB 150 */
  137. buf[0] = 0x01;
  138. buf[1] = 0x00;
  139. buf[2] = 0x01;
  140. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  141. 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
  142. if (ret < 0)
  143. usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
  144. }
  145. static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
  146. const struct firmware *fw1,
  147. const struct firmware *fw2)
  148. {
  149. unsigned char *buffer;
  150. struct usbatm_data *usbatm = instance->usbatm;
  151. struct usb_interface *intf;
  152. struct usb_device *usb_dev = usbatm->usb_dev;
  153. int actual_length;
  154. int ret = 0;
  155. int offset;
  156. usb_dbg(usbatm, "%s entered\n", __func__);
  157. if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
  158. ret = -ENOMEM;
  159. usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
  160. goto out;
  161. }
  162. if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
  163. ret = -ENODEV;
  164. usb_dbg(usbatm, "%s: interface not found!\n", __func__);
  165. goto out_free;
  166. }
  167. /* URB 7 */
  168. if (dl_512_first) { /* some modems need a read before writing the firmware */
  169. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  170. buffer, 0x200, &actual_length, 2000);
  171. if (ret < 0 && ret != -ETIMEDOUT)
  172. usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
  173. else
  174. usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
  175. }
  176. /* URB 8 : both leds are static green */
  177. for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
  178. int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
  179. memcpy(buffer, fw1->data + offset, thislen);
  180. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  181. buffer, thislen, &actual_length, DATA_TIMEOUT);
  182. if (ret < 0) {
  183. usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
  184. goto out_free;
  185. }
  186. usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
  187. }
  188. /* USB led blinking green, ADSL led off */
  189. /* URB 11 */
  190. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  191. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  192. if (ret < 0) {
  193. usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
  194. goto out_free;
  195. }
  196. usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
  197. /* URBs 12 to 139 - USB led blinking green, ADSL led off */
  198. for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
  199. int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
  200. memcpy(buffer, fw2->data + offset, thislen);
  201. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  202. buffer, thislen, &actual_length, DATA_TIMEOUT);
  203. if (ret < 0) {
  204. usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
  205. goto out_free;
  206. }
  207. }
  208. usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
  209. /* USB led static green, ADSL led static red */
  210. /* URB 142 */
  211. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  212. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  213. if (ret < 0) {
  214. usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
  215. goto out_free;
  216. }
  217. /* success */
  218. usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
  219. /* Delay to allow firmware to start up. We can do this here
  220. because we're in our own kernel thread anyway. */
  221. msleep_interruptible(1000);
  222. /* Enable software buffering, if requested */
  223. if (sw_buffering)
  224. speedtch_set_swbuff(instance, 1);
  225. /* Magic spell; don't ask us what this does */
  226. speedtch_test_sequence(instance);
  227. ret = 0;
  228. out_free:
  229. free_page((unsigned long)buffer);
  230. out:
  231. return ret;
  232. }
  233. static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
  234. int phase, const struct firmware **fw_p)
  235. {
  236. struct device *dev = &intf->dev;
  237. const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
  238. const u8 major_revision = bcdDevice >> 8;
  239. const u8 minor_revision = bcdDevice & 0xff;
  240. char buf[24];
  241. sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
  242. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  243. if (request_firmware(fw_p, buf, dev)) {
  244. sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
  245. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  246. if (request_firmware(fw_p, buf, dev)) {
  247. sprintf(buf, "speedtch-%d.bin", phase);
  248. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  249. if (request_firmware(fw_p, buf, dev)) {
  250. usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
  251. return -ENOENT;
  252. }
  253. }
  254. }
  255. usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
  256. return 0;
  257. }
  258. static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
  259. {
  260. const struct firmware *fw1, *fw2;
  261. struct speedtch_instance_data *instance = usbatm->driver_data;
  262. int ret;
  263. if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
  264. return ret;
  265. if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
  266. release_firmware(fw1);
  267. return ret;
  268. }
  269. if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
  270. usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
  271. release_firmware(fw2);
  272. release_firmware(fw1);
  273. return ret;
  274. }
  275. /**********
  276. ** ATM **
  277. **********/
  278. static int speedtch_read_status(struct speedtch_instance_data *instance)
  279. {
  280. struct usbatm_data *usbatm = instance->usbatm;
  281. struct usb_device *usb_dev = usbatm->usb_dev;
  282. unsigned char *buf = instance->scratch_buffer;
  283. int ret;
  284. memset(buf, 0, TOTAL);
  285. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  286. 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
  287. CTRL_TIMEOUT);
  288. if (ret < 0) {
  289. atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
  290. return ret;
  291. }
  292. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  293. 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
  294. CTRL_TIMEOUT);
  295. if (ret < 0) {
  296. atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
  297. return ret;
  298. }
  299. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  300. 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
  301. CTRL_TIMEOUT);
  302. if (ret < 0) {
  303. atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
  304. return ret;
  305. }
  306. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  307. 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
  308. CTRL_TIMEOUT);
  309. if (ret < 0) {
  310. atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
  311. return ret;
  312. }
  313. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  314. 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
  315. CTRL_TIMEOUT);
  316. if (ret < 0) {
  317. atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. static int speedtch_start_synchro(struct speedtch_instance_data *instance)
  323. {
  324. struct usbatm_data *usbatm = instance->usbatm;
  325. struct usb_device *usb_dev = usbatm->usb_dev;
  326. unsigned char *buf = instance->scratch_buffer;
  327. int ret;
  328. atm_dbg(usbatm, "%s entered\n", __func__);
  329. memset(buf, 0, 2);
  330. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  331. 0x12, 0xc0, 0x04, 0x00,
  332. buf, 2, CTRL_TIMEOUT);
  333. if (ret < 0)
  334. atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
  335. else
  336. atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
  337. __func__, ret, buf[0], buf[1]);
  338. return ret;
  339. }
  340. static void speedtch_check_status(struct speedtch_instance_data *instance)
  341. {
  342. struct usbatm_data *usbatm = instance->usbatm;
  343. struct atm_dev *atm_dev = usbatm->atm_dev;
  344. unsigned char *buf = instance->scratch_buffer;
  345. int down_speed, up_speed, ret;
  346. unsigned char status;
  347. #ifdef VERBOSE_DEBUG
  348. atm_dbg(usbatm, "%s entered\n", __func__);
  349. #endif
  350. ret = speedtch_read_status(instance);
  351. if (ret < 0) {
  352. atm_warn(usbatm, "error %d fetching device status\n", ret);
  353. instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
  354. return;
  355. }
  356. instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
  357. status = buf[OFFSET_7];
  358. if ((status != instance->last_status) || !status) {
  359. atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
  360. switch (status) {
  361. case 0:
  362. atm_dev->signal = ATM_PHY_SIG_LOST;
  363. if (instance->last_status)
  364. atm_info(usbatm, "ADSL line is down\n");
  365. /* It may never resync again unless we ask it to... */
  366. ret = speedtch_start_synchro(instance);
  367. break;
  368. case 0x08:
  369. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  370. atm_info(usbatm, "ADSL line is blocked?\n");
  371. break;
  372. case 0x10:
  373. atm_dev->signal = ATM_PHY_SIG_LOST;
  374. atm_info(usbatm, "ADSL line is synchronising\n");
  375. break;
  376. case 0x20:
  377. down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
  378. | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
  379. up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
  380. | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
  381. if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
  382. down_speed >>= 16;
  383. up_speed >>= 16;
  384. }
  385. atm_dev->link_rate = down_speed * 1000 / 424;
  386. atm_dev->signal = ATM_PHY_SIG_FOUND;
  387. atm_info(usbatm,
  388. "ADSL line is up (%d kb/s down | %d kb/s up)\n",
  389. down_speed, up_speed);
  390. break;
  391. default:
  392. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  393. atm_info(usbatm, "unknown line state %02x\n", status);
  394. break;
  395. }
  396. instance->last_status = status;
  397. }
  398. }
  399. static void speedtch_status_poll(unsigned long data)
  400. {
  401. struct speedtch_instance_data *instance = (void *)data;
  402. schedule_work(&instance->status_checker);
  403. /* The following check is racy, but the race is harmless */
  404. if (instance->poll_delay < MAX_POLL_DELAY)
  405. mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
  406. else
  407. atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
  408. }
  409. static void speedtch_resubmit_int(unsigned long data)
  410. {
  411. struct speedtch_instance_data *instance = (void *)data;
  412. struct urb *int_urb = instance->int_urb;
  413. int ret;
  414. atm_dbg(instance->usbatm, "%s entered\n", __func__);
  415. if (int_urb) {
  416. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  417. if (!ret)
  418. schedule_work(&instance->status_checker);
  419. else {
  420. atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  421. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  422. }
  423. }
  424. }
  425. static void speedtch_handle_int(struct urb *int_urb, struct pt_regs *regs)
  426. {
  427. struct speedtch_instance_data *instance = int_urb->context;
  428. struct usbatm_data *usbatm = instance->usbatm;
  429. unsigned int count = int_urb->actual_length;
  430. int ret = int_urb->status;
  431. /* The magic interrupt for "up state" */
  432. static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
  433. /* The magic interrupt for "down state" */
  434. static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  435. atm_dbg(usbatm, "%s entered\n", __func__);
  436. if (ret < 0) {
  437. atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, ret);
  438. goto fail;
  439. }
  440. if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
  441. del_timer(&instance->status_checker.timer);
  442. atm_info(usbatm, "DSL line goes up\n");
  443. } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
  444. atm_info(usbatm, "DSL line goes down\n");
  445. } else {
  446. int i;
  447. atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
  448. for (i = 0; i < count; i++)
  449. printk(" %02x", instance->int_data[i]);
  450. printk("\n");
  451. goto fail;
  452. }
  453. if ((int_urb = instance->int_urb)) {
  454. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  455. schedule_work(&instance->status_checker);
  456. if (ret < 0) {
  457. atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  458. goto fail;
  459. }
  460. }
  461. return;
  462. fail:
  463. if ((int_urb = instance->int_urb))
  464. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  465. }
  466. static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  467. {
  468. struct usb_device *usb_dev = usbatm->usb_dev;
  469. struct speedtch_instance_data *instance = usbatm->driver_data;
  470. int i, ret;
  471. unsigned char mac_str[13];
  472. atm_dbg(usbatm, "%s entered\n", __func__);
  473. if ((ret = usb_set_interface(usb_dev, 1, altsetting)) < 0) {
  474. atm_dbg(usbatm, "%s: usb_set_interface returned %d!\n", __func__, ret);
  475. return ret;
  476. }
  477. /* Set MAC address, it is stored in the serial number */
  478. memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
  479. if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
  480. for (i = 0; i < 6; i++)
  481. atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
  482. }
  483. /* Start modem synchronisation */
  484. ret = speedtch_start_synchro(instance);
  485. /* Set up interrupt endpoint */
  486. if (instance->int_urb) {
  487. ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
  488. if (ret < 0) {
  489. /* Doesn't matter; we'll poll anyway */
  490. atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
  491. usb_free_urb(instance->int_urb);
  492. instance->int_urb = NULL;
  493. }
  494. }
  495. /* Start status polling */
  496. mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
  497. return 0;
  498. }
  499. static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  500. {
  501. struct speedtch_instance_data *instance = usbatm->driver_data;
  502. struct urb *int_urb = instance->int_urb;
  503. atm_dbg(usbatm, "%s entered\n", __func__);
  504. del_timer_sync(&instance->status_checker.timer);
  505. /*
  506. * Since resubmit_timer and int_urb can schedule themselves and
  507. * each other, shutting them down correctly takes some care
  508. */
  509. instance->int_urb = NULL; /* signal shutdown */
  510. mb();
  511. usb_kill_urb(int_urb);
  512. del_timer_sync(&instance->resubmit_timer);
  513. /*
  514. * At this point, speedtch_handle_int and speedtch_resubmit_int
  515. * can run or be running, but instance->int_urb == NULL means that
  516. * they will not reschedule
  517. */
  518. usb_kill_urb(int_urb);
  519. del_timer_sync(&instance->resubmit_timer);
  520. usb_free_urb(int_urb);
  521. flush_scheduled_work();
  522. }
  523. /**********
  524. ** USB **
  525. **********/
  526. static struct usb_device_id speedtch_usb_ids[] = {
  527. {USB_DEVICE(0x06b9, 0x4061)},
  528. {}
  529. };
  530. MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
  531. static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
  532. static struct usb_driver speedtch_usb_driver = {
  533. .name = speedtch_driver_name,
  534. .probe = speedtch_usb_probe,
  535. .disconnect = usbatm_usb_disconnect,
  536. .id_table = speedtch_usb_ids
  537. };
  538. static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) {
  539. struct usb_interface *cur_intf;
  540. int i;
  541. for(i = 0; i < num_interfaces; i++)
  542. if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
  543. usb_set_intfdata(cur_intf, NULL);
  544. usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
  545. }
  546. }
  547. static int speedtch_bind(struct usbatm_data *usbatm,
  548. struct usb_interface *intf,
  549. const struct usb_device_id *id,
  550. int *need_heavy_init)
  551. {
  552. struct usb_device *usb_dev = interface_to_usbdev(intf);
  553. struct usb_interface *cur_intf;
  554. struct speedtch_instance_data *instance;
  555. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  556. int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
  557. int i, ret;
  558. usb_dbg(usbatm, "%s entered\n", __func__);
  559. /* sanity checks */
  560. if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  561. usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
  562. return -ENODEV;
  563. }
  564. /* claim all interfaces */
  565. for (i=0; i < num_interfaces; i++) {
  566. cur_intf = usb_ifnum_to_if(usb_dev, i);
  567. if ((i != ifnum) && cur_intf) {
  568. ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
  569. if (ret < 0) {
  570. usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
  571. speedtch_release_interfaces(usb_dev, i);
  572. return ret;
  573. }
  574. }
  575. }
  576. instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  577. if (!instance) {
  578. usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
  579. ret = -ENOMEM;
  580. goto fail_release;
  581. }
  582. memset(instance, 0, sizeof(struct speedtch_instance_data));
  583. instance->usbatm = usbatm;
  584. INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance);
  585. instance->status_checker.timer.function = speedtch_status_poll;
  586. instance->status_checker.timer.data = (unsigned long)instance;
  587. instance->last_status = 0xff;
  588. instance->poll_delay = MIN_POLL_DELAY;
  589. init_timer(&instance->resubmit_timer);
  590. instance->resubmit_timer.function = speedtch_resubmit_int;
  591. instance->resubmit_timer.data = (unsigned long)instance;
  592. instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  593. if (instance->int_urb)
  594. usb_fill_int_urb(instance->int_urb, usb_dev,
  595. usb_rcvintpipe(usb_dev, ENDPOINT_INT),
  596. instance->int_data, sizeof(instance->int_data),
  597. speedtch_handle_int, instance, 50);
  598. else
  599. usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
  600. /* check whether the modem already seems to be alive */
  601. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  602. 0x12, 0xc0, 0x07, 0x00,
  603. instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
  604. *need_heavy_init = (ret != SIZE_7);
  605. usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, need_heavy_init ? "not" : "already");
  606. if (*need_heavy_init)
  607. if ((ret = usb_reset_device(usb_dev)) < 0) {
  608. usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
  609. goto fail_free;
  610. }
  611. usbatm->driver_data = instance;
  612. return 0;
  613. fail_free:
  614. usb_free_urb(instance->int_urb);
  615. kfree(instance);
  616. fail_release:
  617. speedtch_release_interfaces(usb_dev, num_interfaces);
  618. return ret;
  619. }
  620. static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
  621. {
  622. struct usb_device *usb_dev = interface_to_usbdev(intf);
  623. struct speedtch_instance_data *instance = usbatm->driver_data;
  624. usb_dbg(usbatm, "%s entered\n", __func__);
  625. speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
  626. usb_free_urb(instance->int_urb);
  627. kfree(instance);
  628. }
  629. /***********
  630. ** init **
  631. ***********/
  632. static struct usbatm_driver speedtch_usbatm_driver = {
  633. .owner = THIS_MODULE,
  634. .driver_name = speedtch_driver_name,
  635. .bind = speedtch_bind,
  636. .heavy_init = speedtch_heavy_init,
  637. .unbind = speedtch_unbind,
  638. .atm_start = speedtch_atm_start,
  639. .atm_stop = speedtch_atm_stop,
  640. .in = ENDPOINT_DATA,
  641. .out = ENDPOINT_DATA
  642. };
  643. static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  644. {
  645. return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
  646. }
  647. static int __init speedtch_usb_init(void)
  648. {
  649. dbg("%s: driver version %s", __func__, DRIVER_VERSION);
  650. return usb_register(&speedtch_usb_driver);
  651. }
  652. static void __exit speedtch_usb_cleanup(void)
  653. {
  654. dbg("%s", __func__);
  655. usb_deregister(&speedtch_usb_driver);
  656. }
  657. module_init(speedtch_usb_init);
  658. module_exit(speedtch_usb_cleanup);
  659. MODULE_AUTHOR(DRIVER_AUTHOR);
  660. MODULE_DESCRIPTION(DRIVER_DESC);
  661. MODULE_LICENSE("GPL");
  662. MODULE_VERSION(DRIVER_VERSION);