usb_wwan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. USB Driver layer for GSM modems
  3. Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
  4. This driver is free software; you can redistribute it and/or modify
  5. it under the terms of Version 2 of the GNU General Public License as
  6. published by the Free Software Foundation.
  7. Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
  8. History: see the git log.
  9. Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
  10. This driver exists because the "normal" serial driver doesn't work too well
  11. with GSM modems. Issues:
  12. - data loss -- one single Receive URB is not nearly enough
  13. - controlling the baud rate doesn't make sense
  14. */
  15. #define DRIVER_VERSION "v0.7.2"
  16. #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
  17. #define DRIVER_DESC "USB Driver for GSM modems"
  18. #include <linux/kernel.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/module.h>
  25. #include <linux/bitops.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include <linux/serial.h>
  30. #include "usb-wwan.h"
  31. static bool debug;
  32. void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
  33. {
  34. struct usb_serial *serial = port->serial;
  35. struct usb_wwan_port_private *portdata;
  36. struct usb_wwan_intf_private *intfdata;
  37. intfdata = port->serial->private;
  38. if (!intfdata->send_setup)
  39. return;
  40. portdata = usb_get_serial_port_data(port);
  41. mutex_lock(&serial->disc_mutex);
  42. portdata->rts_state = on;
  43. portdata->dtr_state = on;
  44. if (serial->dev)
  45. intfdata->send_setup(port);
  46. mutex_unlock(&serial->disc_mutex);
  47. }
  48. EXPORT_SYMBOL(usb_wwan_dtr_rts);
  49. void usb_wwan_set_termios(struct tty_struct *tty,
  50. struct usb_serial_port *port,
  51. struct ktermios *old_termios)
  52. {
  53. struct usb_wwan_intf_private *intfdata = port->serial->private;
  54. /* Doesn't support option setting */
  55. tty_termios_copy_hw(tty->termios, old_termios);
  56. if (intfdata->send_setup)
  57. intfdata->send_setup(port);
  58. }
  59. EXPORT_SYMBOL(usb_wwan_set_termios);
  60. int usb_wwan_tiocmget(struct tty_struct *tty)
  61. {
  62. struct usb_serial_port *port = tty->driver_data;
  63. unsigned int value;
  64. struct usb_wwan_port_private *portdata;
  65. portdata = usb_get_serial_port_data(port);
  66. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  67. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  68. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  69. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  70. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  71. ((portdata->ri_state) ? TIOCM_RNG : 0);
  72. return value;
  73. }
  74. EXPORT_SYMBOL(usb_wwan_tiocmget);
  75. int usb_wwan_tiocmset(struct tty_struct *tty,
  76. unsigned int set, unsigned int clear)
  77. {
  78. struct usb_serial_port *port = tty->driver_data;
  79. struct usb_wwan_port_private *portdata;
  80. struct usb_wwan_intf_private *intfdata;
  81. portdata = usb_get_serial_port_data(port);
  82. intfdata = port->serial->private;
  83. if (!intfdata->send_setup)
  84. return -EINVAL;
  85. /* FIXME: what locks portdata fields ? */
  86. if (set & TIOCM_RTS)
  87. portdata->rts_state = 1;
  88. if (set & TIOCM_DTR)
  89. portdata->dtr_state = 1;
  90. if (clear & TIOCM_RTS)
  91. portdata->rts_state = 0;
  92. if (clear & TIOCM_DTR)
  93. portdata->dtr_state = 0;
  94. return intfdata->send_setup(port);
  95. }
  96. EXPORT_SYMBOL(usb_wwan_tiocmset);
  97. static int get_serial_info(struct usb_serial_port *port,
  98. struct serial_struct __user *retinfo)
  99. {
  100. struct serial_struct tmp;
  101. if (!retinfo)
  102. return -EFAULT;
  103. memset(&tmp, 0, sizeof(tmp));
  104. tmp.line = port->serial->minor;
  105. tmp.port = port->number;
  106. tmp.baud_base = tty_get_baud_rate(port->port.tty);
  107. tmp.close_delay = port->port.close_delay / 10;
  108. tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  109. ASYNC_CLOSING_WAIT_NONE :
  110. port->port.closing_wait / 10;
  111. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  112. return -EFAULT;
  113. return 0;
  114. }
  115. static int set_serial_info(struct usb_serial_port *port,
  116. struct serial_struct __user *newinfo)
  117. {
  118. struct serial_struct new_serial;
  119. unsigned int closing_wait, close_delay;
  120. int retval = 0;
  121. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  122. return -EFAULT;
  123. close_delay = new_serial.close_delay * 10;
  124. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  125. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  126. mutex_lock(&port->port.mutex);
  127. if (!capable(CAP_SYS_ADMIN)) {
  128. if ((close_delay != port->port.close_delay) ||
  129. (closing_wait != port->port.closing_wait))
  130. retval = -EPERM;
  131. else
  132. retval = -EOPNOTSUPP;
  133. } else {
  134. port->port.close_delay = close_delay;
  135. port->port.closing_wait = closing_wait;
  136. }
  137. mutex_unlock(&port->port.mutex);
  138. return retval;
  139. }
  140. int usb_wwan_ioctl(struct tty_struct *tty,
  141. unsigned int cmd, unsigned long arg)
  142. {
  143. struct usb_serial_port *port = tty->driver_data;
  144. dbg("%s cmd 0x%04x", __func__, cmd);
  145. switch (cmd) {
  146. case TIOCGSERIAL:
  147. return get_serial_info(port,
  148. (struct serial_struct __user *) arg);
  149. case TIOCSSERIAL:
  150. return set_serial_info(port,
  151. (struct serial_struct __user *) arg);
  152. default:
  153. break;
  154. }
  155. dbg("%s arg not supported", __func__);
  156. return -ENOIOCTLCMD;
  157. }
  158. EXPORT_SYMBOL(usb_wwan_ioctl);
  159. /* Write */
  160. int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
  161. const unsigned char *buf, int count)
  162. {
  163. struct usb_wwan_port_private *portdata;
  164. struct usb_wwan_intf_private *intfdata;
  165. int i;
  166. int left, todo;
  167. struct urb *this_urb = NULL; /* spurious */
  168. int err;
  169. unsigned long flags;
  170. portdata = usb_get_serial_port_data(port);
  171. intfdata = port->serial->private;
  172. dbg("%s: write (%d chars)", __func__, count);
  173. i = 0;
  174. left = count;
  175. for (i = 0; left > 0 && i < N_OUT_URB; i++) {
  176. todo = left;
  177. if (todo > OUT_BUFLEN)
  178. todo = OUT_BUFLEN;
  179. this_urb = portdata->out_urbs[i];
  180. if (test_and_set_bit(i, &portdata->out_busy)) {
  181. if (time_before(jiffies,
  182. portdata->tx_start_time[i] + 10 * HZ))
  183. continue;
  184. usb_unlink_urb(this_urb);
  185. continue;
  186. }
  187. dbg("%s: endpoint %d buf %d", __func__,
  188. usb_pipeendpoint(this_urb->pipe), i);
  189. err = usb_autopm_get_interface_async(port->serial->interface);
  190. if (err < 0)
  191. break;
  192. /* send the data */
  193. memcpy(this_urb->transfer_buffer, buf, todo);
  194. this_urb->transfer_buffer_length = todo;
  195. spin_lock_irqsave(&intfdata->susp_lock, flags);
  196. if (intfdata->suspended) {
  197. usb_anchor_urb(this_urb, &portdata->delayed);
  198. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  199. } else {
  200. intfdata->in_flight++;
  201. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  202. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  203. if (err) {
  204. dbg("usb_submit_urb %p (write bulk) failed "
  205. "(%d)", this_urb, err);
  206. clear_bit(i, &portdata->out_busy);
  207. spin_lock_irqsave(&intfdata->susp_lock, flags);
  208. intfdata->in_flight--;
  209. spin_unlock_irqrestore(&intfdata->susp_lock,
  210. flags);
  211. usb_autopm_put_interface_async(port->serial->interface);
  212. break;
  213. }
  214. }
  215. portdata->tx_start_time[i] = jiffies;
  216. buf += todo;
  217. left -= todo;
  218. }
  219. count -= left;
  220. dbg("%s: wrote (did %d)", __func__, count);
  221. return count;
  222. }
  223. EXPORT_SYMBOL(usb_wwan_write);
  224. static void usb_wwan_indat_callback(struct urb *urb)
  225. {
  226. int err;
  227. int endpoint;
  228. struct usb_serial_port *port;
  229. struct tty_struct *tty;
  230. unsigned char *data = urb->transfer_buffer;
  231. int status = urb->status;
  232. endpoint = usb_pipeendpoint(urb->pipe);
  233. port = urb->context;
  234. if (status) {
  235. dbg("%s: nonzero status: %d on endpoint %02x.",
  236. __func__, status, endpoint);
  237. } else {
  238. tty = tty_port_tty_get(&port->port);
  239. if (tty) {
  240. if (urb->actual_length) {
  241. tty_insert_flip_string(tty, data,
  242. urb->actual_length);
  243. tty_flip_buffer_push(tty);
  244. } else
  245. dbg("%s: empty read urb received", __func__);
  246. tty_kref_put(tty);
  247. }
  248. /* Resubmit urb so we continue receiving */
  249. err = usb_submit_urb(urb, GFP_ATOMIC);
  250. if (err) {
  251. if (err != -EPERM) {
  252. printk(KERN_ERR "%s: resubmit read urb failed. "
  253. "(%d)", __func__, err);
  254. /* busy also in error unless we are killed */
  255. usb_mark_last_busy(port->serial->dev);
  256. }
  257. } else {
  258. usb_mark_last_busy(port->serial->dev);
  259. }
  260. }
  261. }
  262. static void usb_wwan_outdat_callback(struct urb *urb)
  263. {
  264. struct usb_serial_port *port;
  265. struct usb_wwan_port_private *portdata;
  266. struct usb_wwan_intf_private *intfdata;
  267. int i;
  268. port = urb->context;
  269. intfdata = port->serial->private;
  270. usb_serial_port_softint(port);
  271. usb_autopm_put_interface_async(port->serial->interface);
  272. portdata = usb_get_serial_port_data(port);
  273. spin_lock(&intfdata->susp_lock);
  274. intfdata->in_flight--;
  275. spin_unlock(&intfdata->susp_lock);
  276. for (i = 0; i < N_OUT_URB; ++i) {
  277. if (portdata->out_urbs[i] == urb) {
  278. smp_mb__before_clear_bit();
  279. clear_bit(i, &portdata->out_busy);
  280. break;
  281. }
  282. }
  283. }
  284. int usb_wwan_write_room(struct tty_struct *tty)
  285. {
  286. struct usb_serial_port *port = tty->driver_data;
  287. struct usb_wwan_port_private *portdata;
  288. int i;
  289. int data_len = 0;
  290. struct urb *this_urb;
  291. portdata = usb_get_serial_port_data(port);
  292. for (i = 0; i < N_OUT_URB; i++) {
  293. this_urb = portdata->out_urbs[i];
  294. if (this_urb && !test_bit(i, &portdata->out_busy))
  295. data_len += OUT_BUFLEN;
  296. }
  297. dbg("%s: %d", __func__, data_len);
  298. return data_len;
  299. }
  300. EXPORT_SYMBOL(usb_wwan_write_room);
  301. int usb_wwan_chars_in_buffer(struct tty_struct *tty)
  302. {
  303. struct usb_serial_port *port = tty->driver_data;
  304. struct usb_wwan_port_private *portdata;
  305. int i;
  306. int data_len = 0;
  307. struct urb *this_urb;
  308. portdata = usb_get_serial_port_data(port);
  309. for (i = 0; i < N_OUT_URB; i++) {
  310. this_urb = portdata->out_urbs[i];
  311. /* FIXME: This locking is insufficient as this_urb may
  312. go unused during the test */
  313. if (this_urb && test_bit(i, &portdata->out_busy))
  314. data_len += this_urb->transfer_buffer_length;
  315. }
  316. dbg("%s: %d", __func__, data_len);
  317. return data_len;
  318. }
  319. EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
  320. int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
  321. {
  322. struct usb_wwan_port_private *portdata;
  323. struct usb_wwan_intf_private *intfdata;
  324. struct usb_serial *serial = port->serial;
  325. int i, err;
  326. struct urb *urb;
  327. portdata = usb_get_serial_port_data(port);
  328. intfdata = serial->private;
  329. /* Start reading from the IN endpoint */
  330. for (i = 0; i < N_IN_URB; i++) {
  331. urb = portdata->in_urbs[i];
  332. if (!urb)
  333. continue;
  334. err = usb_submit_urb(urb, GFP_KERNEL);
  335. if (err) {
  336. dbg("%s: submit urb %d failed (%d) %d",
  337. __func__, i, err, urb->transfer_buffer_length);
  338. }
  339. }
  340. if (intfdata->send_setup)
  341. intfdata->send_setup(port);
  342. serial->interface->needs_remote_wakeup = 1;
  343. spin_lock_irq(&intfdata->susp_lock);
  344. portdata->opened = 1;
  345. spin_unlock_irq(&intfdata->susp_lock);
  346. /* this balances a get in the generic USB serial code */
  347. usb_autopm_put_interface(serial->interface);
  348. return 0;
  349. }
  350. EXPORT_SYMBOL(usb_wwan_open);
  351. void usb_wwan_close(struct usb_serial_port *port)
  352. {
  353. int i;
  354. struct usb_serial *serial = port->serial;
  355. struct usb_wwan_port_private *portdata;
  356. struct usb_wwan_intf_private *intfdata = port->serial->private;
  357. portdata = usb_get_serial_port_data(port);
  358. if (serial->dev) {
  359. /* Stop reading/writing urbs */
  360. spin_lock_irq(&intfdata->susp_lock);
  361. portdata->opened = 0;
  362. spin_unlock_irq(&intfdata->susp_lock);
  363. for (i = 0; i < N_IN_URB; i++)
  364. usb_kill_urb(portdata->in_urbs[i]);
  365. for (i = 0; i < N_OUT_URB; i++)
  366. usb_kill_urb(portdata->out_urbs[i]);
  367. /* balancing - important as an error cannot be handled*/
  368. usb_autopm_get_interface_no_resume(serial->interface);
  369. serial->interface->needs_remote_wakeup = 0;
  370. }
  371. }
  372. EXPORT_SYMBOL(usb_wwan_close);
  373. /* Helper functions used by usb_wwan_setup_urbs */
  374. static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
  375. int dir, void *ctx, char *buf, int len,
  376. void (*callback) (struct urb *))
  377. {
  378. struct urb *urb;
  379. if (endpoint == -1)
  380. return NULL; /* endpoint not needed */
  381. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  382. if (urb == NULL) {
  383. dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
  384. return NULL;
  385. }
  386. /* Fill URB using supplied data. */
  387. usb_fill_bulk_urb(urb, serial->dev,
  388. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  389. buf, len, callback, ctx);
  390. return urb;
  391. }
  392. /* Setup urbs */
  393. static void usb_wwan_setup_urbs(struct usb_serial *serial)
  394. {
  395. int i, j;
  396. struct usb_serial_port *port;
  397. struct usb_wwan_port_private *portdata;
  398. for (i = 0; i < serial->num_ports; i++) {
  399. port = serial->port[i];
  400. portdata = usb_get_serial_port_data(port);
  401. /* Do indat endpoints first */
  402. for (j = 0; j < N_IN_URB; ++j) {
  403. portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
  404. port->
  405. bulk_in_endpointAddress,
  406. USB_DIR_IN,
  407. port,
  408. portdata->
  409. in_buffer[j],
  410. IN_BUFLEN,
  411. usb_wwan_indat_callback);
  412. }
  413. /* outdat endpoints */
  414. for (j = 0; j < N_OUT_URB; ++j) {
  415. portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
  416. port->
  417. bulk_out_endpointAddress,
  418. USB_DIR_OUT,
  419. port,
  420. portdata->
  421. out_buffer
  422. [j],
  423. OUT_BUFLEN,
  424. usb_wwan_outdat_callback);
  425. }
  426. }
  427. }
  428. int usb_wwan_startup(struct usb_serial *serial)
  429. {
  430. int i, j, err;
  431. struct usb_serial_port *port;
  432. struct usb_wwan_port_private *portdata;
  433. u8 *buffer;
  434. /* Now setup per port private data */
  435. for (i = 0; i < serial->num_ports; i++) {
  436. port = serial->port[i];
  437. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  438. if (!portdata) {
  439. dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
  440. __func__, i);
  441. return 1;
  442. }
  443. init_usb_anchor(&portdata->delayed);
  444. for (j = 0; j < N_IN_URB; j++) {
  445. buffer = (u8 *) __get_free_page(GFP_KERNEL);
  446. if (!buffer)
  447. goto bail_out_error;
  448. portdata->in_buffer[j] = buffer;
  449. }
  450. for (j = 0; j < N_OUT_URB; j++) {
  451. buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
  452. if (!buffer)
  453. goto bail_out_error2;
  454. portdata->out_buffer[j] = buffer;
  455. }
  456. usb_set_serial_port_data(port, portdata);
  457. if (!port->interrupt_in_urb)
  458. continue;
  459. err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  460. if (err)
  461. dbg("%s: submit irq_in urb failed %d", __func__, err);
  462. }
  463. usb_wwan_setup_urbs(serial);
  464. return 0;
  465. bail_out_error2:
  466. for (j = 0; j < N_OUT_URB; j++)
  467. kfree(portdata->out_buffer[j]);
  468. bail_out_error:
  469. for (j = 0; j < N_IN_URB; j++)
  470. if (portdata->in_buffer[j])
  471. free_page((unsigned long)portdata->in_buffer[j]);
  472. kfree(portdata);
  473. return 1;
  474. }
  475. EXPORT_SYMBOL(usb_wwan_startup);
  476. static void stop_read_write_urbs(struct usb_serial *serial)
  477. {
  478. int i, j;
  479. struct usb_serial_port *port;
  480. struct usb_wwan_port_private *portdata;
  481. /* Stop reading/writing urbs */
  482. for (i = 0; i < serial->num_ports; ++i) {
  483. port = serial->port[i];
  484. portdata = usb_get_serial_port_data(port);
  485. for (j = 0; j < N_IN_URB; j++)
  486. usb_kill_urb(portdata->in_urbs[j]);
  487. for (j = 0; j < N_OUT_URB; j++)
  488. usb_kill_urb(portdata->out_urbs[j]);
  489. }
  490. }
  491. void usb_wwan_disconnect(struct usb_serial *serial)
  492. {
  493. stop_read_write_urbs(serial);
  494. }
  495. EXPORT_SYMBOL(usb_wwan_disconnect);
  496. void usb_wwan_release(struct usb_serial *serial)
  497. {
  498. int i, j;
  499. struct usb_serial_port *port;
  500. struct usb_wwan_port_private *portdata;
  501. /* Now free them */
  502. for (i = 0; i < serial->num_ports; ++i) {
  503. port = serial->port[i];
  504. portdata = usb_get_serial_port_data(port);
  505. for (j = 0; j < N_IN_URB; j++) {
  506. usb_free_urb(portdata->in_urbs[j]);
  507. free_page((unsigned long)
  508. portdata->in_buffer[j]);
  509. portdata->in_urbs[j] = NULL;
  510. }
  511. for (j = 0; j < N_OUT_URB; j++) {
  512. usb_free_urb(portdata->out_urbs[j]);
  513. kfree(portdata->out_buffer[j]);
  514. portdata->out_urbs[j] = NULL;
  515. }
  516. }
  517. /* Now free per port private data */
  518. for (i = 0; i < serial->num_ports; i++) {
  519. port = serial->port[i];
  520. kfree(usb_get_serial_port_data(port));
  521. }
  522. }
  523. EXPORT_SYMBOL(usb_wwan_release);
  524. #ifdef CONFIG_PM
  525. int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
  526. {
  527. struct usb_wwan_intf_private *intfdata = serial->private;
  528. int b;
  529. if (PMSG_IS_AUTO(message)) {
  530. spin_lock_irq(&intfdata->susp_lock);
  531. b = intfdata->in_flight;
  532. spin_unlock_irq(&intfdata->susp_lock);
  533. if (b)
  534. return -EBUSY;
  535. }
  536. spin_lock_irq(&intfdata->susp_lock);
  537. intfdata->suspended = 1;
  538. spin_unlock_irq(&intfdata->susp_lock);
  539. stop_read_write_urbs(serial);
  540. return 0;
  541. }
  542. EXPORT_SYMBOL(usb_wwan_suspend);
  543. static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
  544. {
  545. int i;
  546. for (i = 0; i < N_OUT_URB; i++) {
  547. if (urb == portdata->out_urbs[i]) {
  548. clear_bit(i, &portdata->out_busy);
  549. break;
  550. }
  551. }
  552. }
  553. static void play_delayed(struct usb_serial_port *port)
  554. {
  555. struct usb_wwan_intf_private *data;
  556. struct usb_wwan_port_private *portdata;
  557. struct urb *urb;
  558. int err;
  559. portdata = usb_get_serial_port_data(port);
  560. data = port->serial->private;
  561. while ((urb = usb_get_from_anchor(&portdata->delayed))) {
  562. err = usb_submit_urb(urb, GFP_ATOMIC);
  563. if (!err) {
  564. data->in_flight++;
  565. } else {
  566. /* we have to throw away the rest */
  567. do {
  568. unbusy_queued_urb(urb, portdata);
  569. usb_autopm_put_interface_no_suspend(port->serial->interface);
  570. } while ((urb = usb_get_from_anchor(&portdata->delayed)));
  571. break;
  572. }
  573. }
  574. }
  575. int usb_wwan_resume(struct usb_serial *serial)
  576. {
  577. int i, j;
  578. struct usb_serial_port *port;
  579. struct usb_wwan_intf_private *intfdata = serial->private;
  580. struct usb_wwan_port_private *portdata;
  581. struct urb *urb;
  582. int err = 0;
  583. /* get the interrupt URBs resubmitted unconditionally */
  584. for (i = 0; i < serial->num_ports; i++) {
  585. port = serial->port[i];
  586. if (!port->interrupt_in_urb) {
  587. dbg("%s: No interrupt URB for port %d", __func__, i);
  588. continue;
  589. }
  590. err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  591. dbg("Submitted interrupt URB for port %d (result %d)", i, err);
  592. if (err < 0) {
  593. dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
  594. __func__, err);
  595. goto err_out;
  596. }
  597. }
  598. for (i = 0; i < serial->num_ports; i++) {
  599. /* walk all ports */
  600. port = serial->port[i];
  601. portdata = usb_get_serial_port_data(port);
  602. /* skip closed ports */
  603. spin_lock_irq(&intfdata->susp_lock);
  604. if (!portdata->opened) {
  605. spin_unlock_irq(&intfdata->susp_lock);
  606. continue;
  607. }
  608. for (j = 0; j < N_IN_URB; j++) {
  609. urb = portdata->in_urbs[j];
  610. err = usb_submit_urb(urb, GFP_ATOMIC);
  611. if (err < 0) {
  612. dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
  613. __func__, err, i);
  614. spin_unlock_irq(&intfdata->susp_lock);
  615. goto err_out;
  616. }
  617. }
  618. play_delayed(port);
  619. spin_unlock_irq(&intfdata->susp_lock);
  620. }
  621. spin_lock_irq(&intfdata->susp_lock);
  622. intfdata->suspended = 0;
  623. spin_unlock_irq(&intfdata->susp_lock);
  624. err_out:
  625. return err;
  626. }
  627. EXPORT_SYMBOL(usb_wwan_resume);
  628. #endif
  629. MODULE_AUTHOR(DRIVER_AUTHOR);
  630. MODULE_DESCRIPTION(DRIVER_DESC);
  631. MODULE_VERSION(DRIVER_VERSION);
  632. MODULE_LICENSE("GPL");
  633. module_param(debug, bool, S_IRUGO | S_IWUSR);
  634. MODULE_PARM_DESC(debug, "Debug messages");