usb_wwan.c 19 KB

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