sierra.c 21 KB

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