sierra.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. USB Driver for Sierra Wireless
  3. Copyright (C) 2006, 2007 Kevin Lloyd <linux@sierrawireless.com>
  4. IMPORTANT DISCLAIMER: This driver is not commercially supported by
  5. Sierra Wireless. Use at your own risk.
  6. This driver is free software; you can redistribute it and/or modify
  7. it under the terms of Version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
  10. Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
  11. */
  12. #define DRIVER_VERSION "v.1.2.5b"
  13. #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
  14. #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
  15. #include <linux/kernel.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/errno.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/module.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/serial.h>
  23. #define SWIMS_USB_REQUEST_SetMode 0x0B
  24. #define SWIMS_USB_REQUEST_TYPE_SetMode 0x40
  25. #define SWIMS_USB_INDEX_SetMode 0x0000
  26. #define SWIMS_SET_MODE_Modem 0x0001
  27. /* per port private data */
  28. #define N_IN_URB 4
  29. #define N_OUT_URB 4
  30. #define IN_BUFLEN 4096
  31. static int debug;
  32. enum devicetype {
  33. DEVICE_3_PORT = 0,
  34. DEVICE_1_PORT = 1,
  35. DEVICE_INSTALLER = 2,
  36. };
  37. static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
  38. {
  39. int result;
  40. dev_dbg(&udev->dev, "%s", "SET POWER STATE\n");
  41. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  42. 0x00, /* __u8 request */
  43. 0x40, /* __u8 request type */
  44. swiState, /* __u16 value */
  45. 0, /* __u16 index */
  46. NULL, /* void *data */
  47. 0, /* __u16 size */
  48. USB_CTRL_SET_TIMEOUT); /* int timeout */
  49. return result;
  50. }
  51. static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSocMode)
  52. {
  53. int result;
  54. dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
  55. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  56. SWIMS_USB_REQUEST_SetMode, /* __u8 request */
  57. SWIMS_USB_REQUEST_TYPE_SetMode, /* __u8 request type */
  58. eSocMode, /* __u16 value */
  59. SWIMS_USB_INDEX_SetMode, /* __u16 index */
  60. NULL, /* void *data */
  61. 0, /* __u16 size */
  62. USB_CTRL_SET_TIMEOUT); /* int timeout */
  63. return result;
  64. }
  65. static int sierra_probe(struct usb_interface *iface,
  66. const struct usb_device_id *id)
  67. {
  68. int result;
  69. struct usb_device *udev;
  70. udev = usb_get_dev(interface_to_usbdev(iface));
  71. /* Check if in installer mode */
  72. if (id->driver_info == DEVICE_INSTALLER) {
  73. dev_dbg(&udev->dev, "%s", "FOUND DEVICE(SW)\n");
  74. result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
  75. /*We do not want to bind to the device when in installer mode*/
  76. return -EIO;
  77. }
  78. return usb_serial_probe(iface, id);
  79. }
  80. static struct usb_device_id id_table [] = {
  81. { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
  82. { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
  83. { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
  84. { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
  85. { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
  86. { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
  87. { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
  88. { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
  89. { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
  90. { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
  91. { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
  92. { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
  93. { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
  94. { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
  95. { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
  96. { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
  97. { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
  98. { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
  99. { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
  100. { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
  101. { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
  102. { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
  103. { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
  104. { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
  105. { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
  106. { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
  107. { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
  108. { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
  109. { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(usb, id_table);
  113. static struct usb_device_id id_table_1port [] = {
  114. { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
  115. { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
  116. { }
  117. };
  118. static struct usb_device_id id_table_3port [] = {
  119. { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
  120. { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
  121. { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
  122. { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
  123. { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
  124. { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
  125. { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
  126. { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
  127. { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
  128. { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
  129. { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
  130. { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
  131. { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
  132. { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
  133. { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
  134. { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
  135. { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
  136. { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
  137. { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
  138. { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
  139. { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
  140. { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
  141. { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
  142. { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
  143. { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
  144. { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
  145. { }
  146. };
  147. static struct usb_driver sierra_driver = {
  148. .name = "sierra",
  149. .probe = sierra_probe,
  150. .disconnect = usb_serial_disconnect,
  151. .id_table = id_table,
  152. .no_dynamic_id = 1,
  153. };
  154. struct sierra_port_private {
  155. spinlock_t lock; /* lock the structure */
  156. int outstanding_urbs; /* number of out urbs in flight */
  157. /* Input endpoints and buffer for this port */
  158. struct urb *in_urbs[N_IN_URB];
  159. char in_buffer[N_IN_URB][IN_BUFLEN];
  160. /* Settings for the port */
  161. int rts_state; /* Handshaking pins (outputs) */
  162. int dtr_state;
  163. int cts_state; /* Handshaking pins (inputs) */
  164. int dsr_state;
  165. int dcd_state;
  166. int ri_state;
  167. };
  168. static int sierra_send_setup(struct usb_serial_port *port)
  169. {
  170. struct usb_serial *serial = port->serial;
  171. struct sierra_port_private *portdata;
  172. dbg("%s", __FUNCTION__);
  173. portdata = usb_get_serial_port_data(port);
  174. if (port->tty) {
  175. int val = 0;
  176. if (portdata->dtr_state)
  177. val |= 0x01;
  178. if (portdata->rts_state)
  179. val |= 0x02;
  180. return usb_control_msg(serial->dev,
  181. usb_rcvctrlpipe(serial->dev, 0),
  182. 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
  183. }
  184. return 0;
  185. }
  186. static void sierra_rx_throttle(struct usb_serial_port *port)
  187. {
  188. dbg("%s", __FUNCTION__);
  189. }
  190. static void sierra_rx_unthrottle(struct usb_serial_port *port)
  191. {
  192. dbg("%s", __FUNCTION__);
  193. }
  194. static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
  195. {
  196. /* Unfortunately, I don't know how to send a break */
  197. dbg("%s", __FUNCTION__);
  198. }
  199. static void sierra_set_termios(struct usb_serial_port *port,
  200. struct ktermios *old_termios)
  201. {
  202. dbg("%s", __FUNCTION__);
  203. tty_termios_copy_hw(port->tty->termios, old_termios);
  204. sierra_send_setup(port);
  205. }
  206. static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
  207. {
  208. unsigned int value;
  209. struct sierra_port_private *portdata;
  210. portdata = usb_get_serial_port_data(port);
  211. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  212. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  213. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  214. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  215. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  216. ((portdata->ri_state) ? TIOCM_RNG : 0);
  217. return value;
  218. }
  219. static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
  220. unsigned int set, unsigned int clear)
  221. {
  222. struct sierra_port_private *portdata;
  223. portdata = usb_get_serial_port_data(port);
  224. if (set & TIOCM_RTS)
  225. portdata->rts_state = 1;
  226. if (set & TIOCM_DTR)
  227. portdata->dtr_state = 1;
  228. if (clear & TIOCM_RTS)
  229. portdata->rts_state = 0;
  230. if (clear & TIOCM_DTR)
  231. portdata->dtr_state = 0;
  232. return sierra_send_setup(port);
  233. }
  234. static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
  235. unsigned int cmd, unsigned long arg)
  236. {
  237. return -ENOIOCTLCMD;
  238. }
  239. static void sierra_outdat_callback(struct urb *urb)
  240. {
  241. struct usb_serial_port *port = urb->context;
  242. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  243. int status = urb->status;
  244. unsigned long flags;
  245. dbg("%s - port %d", __FUNCTION__, port->number);
  246. /* free up the transfer buffer, as usb_free_urb() does not do this */
  247. kfree(urb->transfer_buffer);
  248. if (status)
  249. dbg("%s - nonzero write bulk status received: %d",
  250. __FUNCTION__, status);
  251. spin_lock_irqsave(&portdata->lock, flags);
  252. --portdata->outstanding_urbs;
  253. spin_unlock_irqrestore(&portdata->lock, flags);
  254. usb_serial_port_softint(port);
  255. }
  256. /* Write */
  257. static int sierra_write(struct usb_serial_port *port,
  258. const unsigned char *buf, int count)
  259. {
  260. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  261. struct usb_serial *serial = port->serial;
  262. unsigned long flags;
  263. unsigned char *buffer;
  264. struct urb *urb;
  265. int status;
  266. portdata = usb_get_serial_port_data(port);
  267. dbg("%s: write (%d chars)", __FUNCTION__, count);
  268. spin_lock_irqsave(&portdata->lock, flags);
  269. if (portdata->outstanding_urbs > N_OUT_URB) {
  270. spin_unlock_irqrestore(&portdata->lock, flags);
  271. dbg("%s - write limit hit\n", __FUNCTION__);
  272. return 0;
  273. }
  274. portdata->outstanding_urbs++;
  275. spin_unlock_irqrestore(&portdata->lock, flags);
  276. buffer = kmalloc(count, GFP_ATOMIC);
  277. if (!buffer) {
  278. dev_err(&port->dev, "out of memory\n");
  279. count = -ENOMEM;
  280. goto error_no_buffer;
  281. }
  282. urb = usb_alloc_urb(0, GFP_ATOMIC);
  283. if (!urb) {
  284. dev_err(&port->dev, "no more free urbs\n");
  285. count = -ENOMEM;
  286. goto error_no_urb;
  287. }
  288. memcpy(buffer, buf, count);
  289. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  290. usb_fill_bulk_urb(urb, serial->dev,
  291. usb_sndbulkpipe(serial->dev,
  292. port->bulk_out_endpointAddress),
  293. buffer, count, sierra_outdat_callback, port);
  294. /* send it down the pipe */
  295. status = usb_submit_urb(urb, GFP_ATOMIC);
  296. if (status) {
  297. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  298. "with status = %d\n", __FUNCTION__, status);
  299. count = status;
  300. goto error;
  301. }
  302. /* we are done with this urb, so let the host driver
  303. * really free it when it is finished with it */
  304. usb_free_urb(urb);
  305. return count;
  306. error:
  307. usb_free_urb(urb);
  308. error_no_urb:
  309. kfree(buffer);
  310. error_no_buffer:
  311. spin_lock_irqsave(&portdata->lock, flags);
  312. --portdata->outstanding_urbs;
  313. spin_unlock_irqrestore(&portdata->lock, flags);
  314. return count;
  315. }
  316. static void sierra_indat_callback(struct urb *urb)
  317. {
  318. int err;
  319. int endpoint;
  320. struct usb_serial_port *port;
  321. struct tty_struct *tty;
  322. unsigned char *data = urb->transfer_buffer;
  323. int status = urb->status;
  324. dbg("%s: %p", __FUNCTION__, urb);
  325. endpoint = usb_pipeendpoint(urb->pipe);
  326. port = (struct usb_serial_port *) urb->context;
  327. if (status) {
  328. dbg("%s: nonzero status: %d on endpoint %02x.",
  329. __FUNCTION__, status, endpoint);
  330. } else {
  331. tty = port->tty;
  332. if (urb->actual_length) {
  333. tty_buffer_request_room(tty, urb->actual_length);
  334. tty_insert_flip_string(tty, data, urb->actual_length);
  335. tty_flip_buffer_push(tty);
  336. } else {
  337. dbg("%s: empty read urb received", __FUNCTION__);
  338. }
  339. /* Resubmit urb so we continue receiving */
  340. if (port->open_count && status != -ESHUTDOWN) {
  341. err = usb_submit_urb(urb, GFP_ATOMIC);
  342. if (err)
  343. dev_err(&port->dev, "resubmit read urb failed."
  344. "(%d)\n", err);
  345. }
  346. }
  347. return;
  348. }
  349. static void sierra_instat_callback(struct urb *urb)
  350. {
  351. int err;
  352. int status = urb->status;
  353. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  354. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  355. struct usb_serial *serial = port->serial;
  356. dbg("%s", __FUNCTION__);
  357. dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
  358. if (status == 0) {
  359. struct usb_ctrlrequest *req_pkt =
  360. (struct usb_ctrlrequest *)urb->transfer_buffer;
  361. if (!req_pkt) {
  362. dbg("%s: NULL req_pkt\n", __FUNCTION__);
  363. return;
  364. }
  365. if ((req_pkt->bRequestType == 0xA1) &&
  366. (req_pkt->bRequest == 0x20)) {
  367. int old_dcd_state;
  368. unsigned char signals = *((unsigned char *)
  369. urb->transfer_buffer +
  370. sizeof(struct usb_ctrlrequest));
  371. dbg("%s: signal x%x", __FUNCTION__, signals);
  372. old_dcd_state = portdata->dcd_state;
  373. portdata->cts_state = 1;
  374. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  375. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  376. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  377. if (port->tty && !C_CLOCAL(port->tty) &&
  378. old_dcd_state && !portdata->dcd_state)
  379. tty_hangup(port->tty);
  380. } else {
  381. dbg("%s: type %x req %x", __FUNCTION__,
  382. req_pkt->bRequestType,req_pkt->bRequest);
  383. }
  384. } else
  385. dbg("%s: error %d", __FUNCTION__, status);
  386. /* Resubmit urb so we continue receiving IRQ data */
  387. if (status != -ESHUTDOWN) {
  388. urb->dev = serial->dev;
  389. err = usb_submit_urb(urb, GFP_ATOMIC);
  390. if (err)
  391. dbg("%s: resubmit intr urb failed. (%d)",
  392. __FUNCTION__, err);
  393. }
  394. }
  395. static int sierra_write_room(struct usb_serial_port *port)
  396. {
  397. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  398. unsigned long flags;
  399. dbg("%s - port %d", __FUNCTION__, port->number);
  400. /* try to give a good number back based on if we have any free urbs at
  401. * this point in time */
  402. spin_lock_irqsave(&portdata->lock, flags);
  403. if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
  404. spin_unlock_irqrestore(&portdata->lock, flags);
  405. dbg("%s - write limit hit\n", __FUNCTION__);
  406. return 0;
  407. }
  408. spin_unlock_irqrestore(&portdata->lock, flags);
  409. return 2048;
  410. }
  411. static int sierra_chars_in_buffer(struct usb_serial_port *port)
  412. {
  413. dbg("%s - port %d", __FUNCTION__, port->number);
  414. /*
  415. * We can't really account for how much data we
  416. * have sent out, but hasn't made it through to the
  417. * device as we can't see the backend here, so just
  418. * tell the tty layer that everything is flushed.
  419. */
  420. return 0;
  421. }
  422. static int sierra_open(struct usb_serial_port *port, struct file *filp)
  423. {
  424. struct sierra_port_private *portdata;
  425. struct usb_serial *serial = port->serial;
  426. int i;
  427. struct urb *urb;
  428. int result;
  429. portdata = usb_get_serial_port_data(port);
  430. dbg("%s", __FUNCTION__);
  431. /* Set some sane defaults */
  432. portdata->rts_state = 1;
  433. portdata->dtr_state = 1;
  434. /* Reset low level data toggle and start reading from endpoints */
  435. for (i = 0; i < N_IN_URB; i++) {
  436. urb = portdata->in_urbs[i];
  437. if (!urb)
  438. continue;
  439. if (urb->dev != serial->dev) {
  440. dbg("%s: dev %p != %p", __FUNCTION__,
  441. urb->dev, serial->dev);
  442. continue;
  443. }
  444. /*
  445. * make sure endpoint data toggle is synchronized with the
  446. * device
  447. */
  448. usb_clear_halt(urb->dev, urb->pipe);
  449. result = usb_submit_urb(urb, GFP_KERNEL);
  450. if (result) {
  451. dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
  452. i, result, urb->transfer_buffer_length);
  453. }
  454. }
  455. port->tty->low_latency = 1;
  456. sierra_send_setup(port);
  457. /* start up the interrupt endpoint if we have one */
  458. if (port->interrupt_in_urb) {
  459. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  460. if (result)
  461. dev_err(&port->dev, "submit irq_in urb failed %d\n",
  462. result);
  463. }
  464. return 0;
  465. }
  466. static void sierra_close(struct usb_serial_port *port, struct file *filp)
  467. {
  468. int i;
  469. struct usb_serial *serial = port->serial;
  470. struct sierra_port_private *portdata;
  471. dbg("%s", __FUNCTION__);
  472. portdata = usb_get_serial_port_data(port);
  473. portdata->rts_state = 0;
  474. portdata->dtr_state = 0;
  475. if (serial->dev) {
  476. sierra_send_setup(port);
  477. /* Stop reading/writing urbs */
  478. for (i = 0; i < N_IN_URB; i++)
  479. usb_kill_urb(portdata->in_urbs[i]);
  480. }
  481. usb_kill_urb(port->interrupt_in_urb);
  482. port->tty = NULL;
  483. }
  484. static int sierra_startup(struct usb_serial *serial)
  485. {
  486. struct usb_serial_port *port;
  487. struct sierra_port_private *portdata;
  488. struct urb *urb;
  489. int i;
  490. int j;
  491. dbg("%s", __FUNCTION__);
  492. /*Set Device mode to D0 */
  493. sierra_set_power_state(serial->dev, 0x0000);
  494. /* Now setup per port private data */
  495. for (i = 0; i < serial->num_ports; i++) {
  496. port = serial->port[i];
  497. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  498. if (!portdata) {
  499. dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
  500. __FUNCTION__, i);
  501. return -ENOMEM;
  502. }
  503. spin_lock_init(&portdata->lock);
  504. usb_set_serial_port_data(port, portdata);
  505. /* initialize the in urbs */
  506. for (j = 0; j < N_IN_URB; ++j) {
  507. urb = usb_alloc_urb(0, GFP_KERNEL);
  508. if (urb == NULL) {
  509. dbg("%s: alloc for in port failed.",
  510. __FUNCTION__);
  511. continue;
  512. }
  513. /* Fill URB using supplied data. */
  514. usb_fill_bulk_urb(urb, serial->dev,
  515. usb_rcvbulkpipe(serial->dev,
  516. port->bulk_in_endpointAddress),
  517. portdata->in_buffer[j], IN_BUFLEN,
  518. sierra_indat_callback, port);
  519. portdata->in_urbs[j] = urb;
  520. }
  521. }
  522. return 0;
  523. }
  524. static void sierra_shutdown(struct usb_serial *serial)
  525. {
  526. int i, j;
  527. struct usb_serial_port *port;
  528. struct sierra_port_private *portdata;
  529. dbg("%s", __FUNCTION__);
  530. for (i = 0; i < serial->num_ports; ++i) {
  531. port = serial->port[i];
  532. if (!port)
  533. continue;
  534. portdata = usb_get_serial_port_data(port);
  535. if (!portdata)
  536. continue;
  537. for (j = 0; j < N_IN_URB; j++) {
  538. usb_kill_urb(portdata->in_urbs[j]);
  539. usb_free_urb(portdata->in_urbs[j]);
  540. portdata->in_urbs[j] = NULL;
  541. }
  542. kfree(portdata);
  543. usb_set_serial_port_data(port, NULL);
  544. }
  545. }
  546. static struct usb_serial_driver sierra_1port_device = {
  547. .driver = {
  548. .owner = THIS_MODULE,
  549. .name = "sierra1",
  550. },
  551. .description = "Sierra USB modem (1 port)",
  552. .id_table = id_table_1port,
  553. .usb_driver = &sierra_driver,
  554. .num_interrupt_in = NUM_DONT_CARE,
  555. .num_bulk_in = 1,
  556. .num_bulk_out = 1,
  557. .num_ports = 1,
  558. .open = sierra_open,
  559. .close = sierra_close,
  560. .write = sierra_write,
  561. .write_room = sierra_write_room,
  562. .chars_in_buffer = sierra_chars_in_buffer,
  563. .throttle = sierra_rx_throttle,
  564. .unthrottle = sierra_rx_unthrottle,
  565. .ioctl = sierra_ioctl,
  566. .set_termios = sierra_set_termios,
  567. .break_ctl = sierra_break_ctl,
  568. .tiocmget = sierra_tiocmget,
  569. .tiocmset = sierra_tiocmset,
  570. .attach = sierra_startup,
  571. .shutdown = sierra_shutdown,
  572. .read_int_callback = sierra_instat_callback,
  573. };
  574. static struct usb_serial_driver sierra_3port_device = {
  575. .driver = {
  576. .owner = THIS_MODULE,
  577. .name = "sierra3",
  578. },
  579. .description = "Sierra USB modem (3 port)",
  580. .id_table = id_table_3port,
  581. .usb_driver = &sierra_driver,
  582. .num_interrupt_in = NUM_DONT_CARE,
  583. .num_bulk_in = 3,
  584. .num_bulk_out = 3,
  585. .num_ports = 3,
  586. .open = sierra_open,
  587. .close = sierra_close,
  588. .write = sierra_write,
  589. .write_room = sierra_write_room,
  590. .chars_in_buffer = sierra_chars_in_buffer,
  591. .throttle = sierra_rx_throttle,
  592. .unthrottle = sierra_rx_unthrottle,
  593. .ioctl = sierra_ioctl,
  594. .set_termios = sierra_set_termios,
  595. .break_ctl = sierra_break_ctl,
  596. .tiocmget = sierra_tiocmget,
  597. .tiocmset = sierra_tiocmset,
  598. .attach = sierra_startup,
  599. .shutdown = sierra_shutdown,
  600. .read_int_callback = sierra_instat_callback,
  601. };
  602. /* Functions used by new usb-serial code. */
  603. static int __init sierra_init(void)
  604. {
  605. int retval;
  606. retval = usb_serial_register(&sierra_1port_device);
  607. if (retval)
  608. goto failed_1port_device_register;
  609. retval = usb_serial_register(&sierra_3port_device);
  610. if (retval)
  611. goto failed_3port_device_register;
  612. retval = usb_register(&sierra_driver);
  613. if (retval)
  614. goto failed_driver_register;
  615. info(DRIVER_DESC ": " DRIVER_VERSION);
  616. return 0;
  617. failed_driver_register:
  618. usb_serial_deregister(&sierra_3port_device);
  619. failed_3port_device_register:
  620. usb_serial_deregister(&sierra_1port_device);
  621. failed_1port_device_register:
  622. return retval;
  623. }
  624. static void __exit sierra_exit(void)
  625. {
  626. usb_deregister (&sierra_driver);
  627. usb_serial_deregister(&sierra_1port_device);
  628. usb_serial_deregister(&sierra_3port_device);
  629. }
  630. module_init(sierra_init);
  631. module_exit(sierra_exit);
  632. MODULE_AUTHOR(DRIVER_AUTHOR);
  633. MODULE_DESCRIPTION(DRIVER_DESC);
  634. MODULE_VERSION(DRIVER_VERSION);
  635. MODULE_LICENSE("GPL");
  636. #ifdef CONFIG_USB_DEBUG
  637. module_param(debug, bool, S_IRUGO | S_IWUSR);
  638. MODULE_PARM_DESC(debug, "Debug messages");
  639. #endif