sierra.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. USB Driver for Sierra Wireless
  3. Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
  4. Copyright (C) 2008, 2009 Elina Pasheva, Matthew Safar, Rory Filer
  5. <linux@sierrawireless.com>
  6. IMPORTANT DISCLAIMER: This driver is not commercially supported by
  7. Sierra Wireless. Use at your own risk.
  8. This driver is free software; you can redistribute it and/or modify
  9. it under the terms of Version 2 of the GNU General Public License as
  10. published by the Free Software Foundation.
  11. Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
  12. Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
  13. */
  14. #define DRIVER_VERSION "v.1.3.7"
  15. #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
  16. #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
  17. #include <linux/kernel.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/errno.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/serial.h>
  25. #define SWIMS_USB_REQUEST_SetPower 0x00
  26. #define SWIMS_USB_REQUEST_SetNmea 0x07
  27. #define N_IN_URB 8
  28. #define N_OUT_URB 64
  29. #define IN_BUFLEN 4096
  30. #define MAX_TRANSFER (PAGE_SIZE - 512)
  31. /* MAX_TRANSFER is chosen so that the VM is not stressed by
  32. allocations > PAGE_SIZE and the number of packets in a page
  33. is an integer 512 is the largest possible packet on EHCI */
  34. static int debug;
  35. static int nmea;
  36. /* Used in interface blacklisting */
  37. struct sierra_iface_info {
  38. const u32 infolen; /* number of interface numbers on blacklist */
  39. const u8 *ifaceinfo; /* pointer to the array holding the numbers */
  40. };
  41. struct sierra_intf_private {
  42. spinlock_t susp_lock;
  43. unsigned int suspended:1;
  44. int in_flight;
  45. };
  46. static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
  47. {
  48. int result;
  49. dev_dbg(&udev->dev, "%s\n", __func__);
  50. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  51. SWIMS_USB_REQUEST_SetPower, /* __u8 request */
  52. USB_TYPE_VENDOR, /* __u8 request type */
  53. swiState, /* __u16 value */
  54. 0, /* __u16 index */
  55. NULL, /* void *data */
  56. 0, /* __u16 size */
  57. USB_CTRL_SET_TIMEOUT); /* int timeout */
  58. return result;
  59. }
  60. static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
  61. {
  62. int result;
  63. dev_dbg(&udev->dev, "%s\n", __func__);
  64. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  65. SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
  66. USB_TYPE_VENDOR, /* __u8 request type */
  67. enable, /* __u16 value */
  68. 0x0000, /* __u16 index */
  69. NULL, /* void *data */
  70. 0, /* __u16 size */
  71. USB_CTRL_SET_TIMEOUT); /* int timeout */
  72. return result;
  73. }
  74. static int sierra_calc_num_ports(struct usb_serial *serial)
  75. {
  76. int num_ports = 0;
  77. u8 ifnum, numendpoints;
  78. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  79. ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
  80. numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
  81. /* Dummy interface present on some SKUs should be ignored */
  82. if (ifnum == 0x99)
  83. num_ports = 0;
  84. else if (numendpoints <= 3)
  85. num_ports = 1;
  86. else
  87. num_ports = (numendpoints-1)/2;
  88. return num_ports;
  89. }
  90. static int is_blacklisted(const u8 ifnum,
  91. const struct sierra_iface_info *blacklist)
  92. {
  93. const u8 *info;
  94. int i;
  95. if (blacklist) {
  96. info = blacklist->ifaceinfo;
  97. for (i = 0; i < blacklist->infolen; i++) {
  98. if (info[i] == ifnum)
  99. return 1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static int sierra_calc_interface(struct usb_serial *serial)
  105. {
  106. int interface;
  107. struct usb_interface *p_interface;
  108. struct usb_host_interface *p_host_interface;
  109. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  110. /* Get the interface structure pointer from the serial struct */
  111. p_interface = serial->interface;
  112. /* Get a pointer to the host interface structure */
  113. p_host_interface = p_interface->cur_altsetting;
  114. /* read the interface descriptor for this active altsetting
  115. * to find out the interface number we are on
  116. */
  117. interface = p_host_interface->desc.bInterfaceNumber;
  118. return interface;
  119. }
  120. static int sierra_probe(struct usb_serial *serial,
  121. const struct usb_device_id *id)
  122. {
  123. int result = 0;
  124. struct usb_device *udev;
  125. struct sierra_intf_private *data;
  126. u8 ifnum;
  127. udev = serial->dev;
  128. dev_dbg(&udev->dev, "%s\n", __func__);
  129. ifnum = sierra_calc_interface(serial);
  130. /*
  131. * If this interface supports more than 1 alternate
  132. * select the 2nd one
  133. */
  134. if (serial->interface->num_altsetting == 2) {
  135. dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
  136. ifnum);
  137. /* We know the alternate setting is 1 for the MC8785 */
  138. usb_set_interface(udev, ifnum, 1);
  139. }
  140. /* ifnum could have changed - by calling usb_set_interface */
  141. ifnum = sierra_calc_interface(serial);
  142. if (is_blacklisted(ifnum,
  143. (struct sierra_iface_info *)id->driver_info)) {
  144. dev_dbg(&serial->dev->dev,
  145. "Ignoring blacklisted interface #%d\n", ifnum);
  146. return -ENODEV;
  147. }
  148. data = serial->private = kzalloc(sizeof(struct sierra_intf_private), GFP_KERNEL);
  149. if (!data)
  150. return -ENOMEM;
  151. spin_lock_init(&data->susp_lock);
  152. return result;
  153. }
  154. static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
  155. static const struct sierra_iface_info direct_ip_interface_blacklist = {
  156. .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
  157. .ifaceinfo = direct_ip_non_serial_ifaces,
  158. };
  159. static struct usb_device_id id_table [] = {
  160. { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
  161. { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
  162. { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
  163. { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
  164. { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
  165. { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
  166. { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
  167. { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
  168. { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
  169. { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
  170. { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
  171. { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
  172. { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
  173. { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
  174. { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
  175. /* Sierra Wireless C597 */
  176. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
  177. /* Sierra Wireless T598 */
  178. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
  179. { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
  180. { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
  181. { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
  182. { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
  183. { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
  184. { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
  185. { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
  186. { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
  187. { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
  188. { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
  189. { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
  190. { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
  191. { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
  192. { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
  193. { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
  194. { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
  195. { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
  196. { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
  197. { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
  198. { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
  199. { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
  200. { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
  201. { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
  202. { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
  203. { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
  204. /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
  205. { USB_DEVICE(0x1199, 0x683C) },
  206. { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
  207. /* Sierra Wireless MC8790, MC8791, MC8792 */
  208. { USB_DEVICE(0x1199, 0x683E) },
  209. { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
  210. { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
  211. { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
  212. { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
  213. { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
  214. { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
  215. { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
  216. { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
  217. /* Sierra Wireless C885 */
  218. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
  219. /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
  220. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
  221. /* Sierra Wireless C22/C33 */
  222. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
  223. /* Sierra Wireless HSPA Non-Composite Device */
  224. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
  225. { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
  226. { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
  227. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  228. },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(usb, id_table);
  232. static struct usb_driver sierra_driver = {
  233. .name = "sierra",
  234. .probe = usb_serial_probe,
  235. .disconnect = usb_serial_disconnect,
  236. .suspend = usb_serial_suspend,
  237. .resume = usb_serial_resume,
  238. .id_table = id_table,
  239. .no_dynamic_id = 1,
  240. .supports_autosuspend = 1,
  241. };
  242. struct sierra_port_private {
  243. spinlock_t lock; /* lock the structure */
  244. int outstanding_urbs; /* number of out urbs in flight */
  245. struct usb_anchor active;
  246. struct usb_anchor delayed;
  247. /* Input endpoints and buffers for this port */
  248. struct urb *in_urbs[N_IN_URB];
  249. /* Settings for the port */
  250. int rts_state; /* Handshaking pins (outputs) */
  251. int dtr_state;
  252. int cts_state; /* Handshaking pins (inputs) */
  253. int dsr_state;
  254. int dcd_state;
  255. int ri_state;
  256. unsigned int opened:1;
  257. };
  258. static int sierra_send_setup(struct usb_serial_port *port)
  259. {
  260. struct usb_serial *serial = port->serial;
  261. struct sierra_port_private *portdata;
  262. __u16 interface = 0;
  263. int val = 0;
  264. dev_dbg(&port->dev, "%s\n", __func__);
  265. portdata = usb_get_serial_port_data(port);
  266. if (portdata->dtr_state)
  267. val |= 0x01;
  268. if (portdata->rts_state)
  269. val |= 0x02;
  270. /* If composite device then properly report interface */
  271. if (serial->num_ports == 1) {
  272. interface = sierra_calc_interface(serial);
  273. /* Control message is sent only to interfaces with
  274. * interrupt_in endpoints
  275. */
  276. if (port->interrupt_in_urb) {
  277. /* send control message */
  278. return usb_control_msg(serial->dev,
  279. usb_rcvctrlpipe(serial->dev, 0),
  280. 0x22, 0x21, val, interface,
  281. NULL, 0, USB_CTRL_SET_TIMEOUT);
  282. }
  283. }
  284. /* Otherwise the need to do non-composite mapping */
  285. else {
  286. if (port->bulk_out_endpointAddress == 2)
  287. interface = 0;
  288. else if (port->bulk_out_endpointAddress == 4)
  289. interface = 1;
  290. else if (port->bulk_out_endpointAddress == 5)
  291. interface = 2;
  292. return usb_control_msg(serial->dev,
  293. usb_rcvctrlpipe(serial->dev, 0),
  294. 0x22, 0x21, val, interface,
  295. NULL, 0, USB_CTRL_SET_TIMEOUT);
  296. }
  297. return 0;
  298. }
  299. static void sierra_set_termios(struct tty_struct *tty,
  300. struct usb_serial_port *port, struct ktermios *old_termios)
  301. {
  302. dev_dbg(&port->dev, "%s\n", __func__);
  303. tty_termios_copy_hw(tty->termios, old_termios);
  304. sierra_send_setup(port);
  305. }
  306. static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
  307. {
  308. struct usb_serial_port *port = tty->driver_data;
  309. unsigned int value;
  310. struct sierra_port_private *portdata;
  311. dev_dbg(&port->dev, "%s\n", __func__);
  312. portdata = usb_get_serial_port_data(port);
  313. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  314. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  315. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  316. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  317. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  318. ((portdata->ri_state) ? TIOCM_RNG : 0);
  319. return value;
  320. }
  321. static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
  322. unsigned int set, unsigned int clear)
  323. {
  324. struct usb_serial_port *port = tty->driver_data;
  325. struct sierra_port_private *portdata;
  326. portdata = usb_get_serial_port_data(port);
  327. if (set & TIOCM_RTS)
  328. portdata->rts_state = 1;
  329. if (set & TIOCM_DTR)
  330. portdata->dtr_state = 1;
  331. if (clear & TIOCM_RTS)
  332. portdata->rts_state = 0;
  333. if (clear & TIOCM_DTR)
  334. portdata->dtr_state = 0;
  335. return sierra_send_setup(port);
  336. }
  337. static void sierra_release_urb(struct urb *urb)
  338. {
  339. struct usb_serial_port *port;
  340. if (urb) {
  341. port = urb->context;
  342. dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
  343. kfree(urb->transfer_buffer);
  344. usb_free_urb(urb);
  345. }
  346. }
  347. static void sierra_outdat_callback(struct urb *urb)
  348. {
  349. struct usb_serial_port *port = urb->context;
  350. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  351. struct sierra_intf_private *intfdata;
  352. int status = urb->status;
  353. dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
  354. intfdata = port->serial->private;
  355. /* free up the transfer buffer, as usb_free_urb() does not do this */
  356. kfree(urb->transfer_buffer);
  357. usb_autopm_put_interface_async(port->serial->interface);
  358. if (status)
  359. dev_dbg(&port->dev, "%s - nonzero write bulk status "
  360. "received: %d\n", __func__, status);
  361. spin_lock(&portdata->lock);
  362. --portdata->outstanding_urbs;
  363. spin_unlock(&portdata->lock);
  364. spin_lock(&intfdata->susp_lock);
  365. --intfdata->in_flight;
  366. spin_unlock(&intfdata->susp_lock);
  367. usb_serial_port_softint(port);
  368. }
  369. /* Write */
  370. static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
  371. const unsigned char *buf, int count)
  372. {
  373. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  374. struct sierra_intf_private *intfdata;
  375. struct usb_serial *serial = port->serial;
  376. unsigned long flags;
  377. unsigned char *buffer;
  378. struct urb *urb;
  379. size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
  380. int retval = 0;
  381. /* verify that we actually have some data to write */
  382. if (count == 0)
  383. return 0;
  384. portdata = usb_get_serial_port_data(port);
  385. intfdata = serial->private;
  386. dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
  387. spin_lock_irqsave(&portdata->lock, flags);
  388. dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
  389. portdata->outstanding_urbs);
  390. if (portdata->outstanding_urbs > N_OUT_URB) {
  391. spin_unlock_irqrestore(&portdata->lock, flags);
  392. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  393. return 0;
  394. }
  395. portdata->outstanding_urbs++;
  396. dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
  397. portdata->outstanding_urbs);
  398. spin_unlock_irqrestore(&portdata->lock, flags);
  399. retval = usb_autopm_get_interface_async(serial->interface);
  400. if (retval < 0) {
  401. spin_lock_irqsave(&portdata->lock, flags);
  402. portdata->outstanding_urbs--;
  403. spin_unlock_irqrestore(&portdata->lock, flags);
  404. goto error_simple;
  405. }
  406. buffer = kmalloc(writesize, GFP_ATOMIC);
  407. if (!buffer) {
  408. dev_err(&port->dev, "out of memory\n");
  409. retval = -ENOMEM;
  410. goto error_no_buffer;
  411. }
  412. urb = usb_alloc_urb(0, GFP_ATOMIC);
  413. if (!urb) {
  414. dev_err(&port->dev, "no more free urbs\n");
  415. retval = -ENOMEM;
  416. goto error_no_urb;
  417. }
  418. memcpy(buffer, buf, writesize);
  419. usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
  420. usb_fill_bulk_urb(urb, serial->dev,
  421. usb_sndbulkpipe(serial->dev,
  422. port->bulk_out_endpointAddress),
  423. buffer, writesize, sierra_outdat_callback, port);
  424. /* Handle the need to send a zero length packet */
  425. urb->transfer_flags |= URB_ZERO_PACKET;
  426. spin_lock_irqsave(&intfdata->susp_lock, flags);
  427. if (intfdata->suspended) {
  428. usb_anchor_urb(urb, &portdata->delayed);
  429. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  430. goto skip_power;
  431. } else {
  432. usb_anchor_urb(urb, &portdata->active);
  433. }
  434. /* send it down the pipe */
  435. retval = usb_submit_urb(urb, GFP_ATOMIC);
  436. if (retval) {
  437. usb_unanchor_urb(urb);
  438. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  439. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  440. "with status = %d\n", __func__, retval);
  441. goto error;
  442. } else {
  443. intfdata->in_flight++;
  444. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  445. }
  446. skip_power:
  447. /* we are done with this urb, so let the host driver
  448. * really free it when it is finished with it */
  449. usb_free_urb(urb);
  450. return writesize;
  451. error:
  452. usb_free_urb(urb);
  453. error_no_urb:
  454. kfree(buffer);
  455. error_no_buffer:
  456. spin_lock_irqsave(&portdata->lock, flags);
  457. --portdata->outstanding_urbs;
  458. dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
  459. portdata->outstanding_urbs);
  460. spin_unlock_irqrestore(&portdata->lock, flags);
  461. usb_autopm_put_interface_async(serial->interface);
  462. error_simple:
  463. return retval;
  464. }
  465. static void sierra_indat_callback(struct urb *urb)
  466. {
  467. int err;
  468. int endpoint;
  469. struct usb_serial_port *port;
  470. struct tty_struct *tty;
  471. unsigned char *data = urb->transfer_buffer;
  472. int status = urb->status;
  473. endpoint = usb_pipeendpoint(urb->pipe);
  474. port = urb->context;
  475. dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
  476. if (status) {
  477. dev_dbg(&port->dev, "%s: nonzero status: %d on"
  478. " endpoint %02x\n", __func__, status, endpoint);
  479. } else {
  480. if (urb->actual_length) {
  481. tty = tty_port_tty_get(&port->port);
  482. tty_buffer_request_room(tty, urb->actual_length);
  483. tty_insert_flip_string(tty, data, urb->actual_length);
  484. tty_flip_buffer_push(tty);
  485. tty_kref_put(tty);
  486. usb_serial_debug_data(debug, &port->dev, __func__,
  487. urb->actual_length, data);
  488. } else {
  489. dev_dbg(&port->dev, "%s: empty read urb"
  490. " received\n", __func__);
  491. }
  492. }
  493. /* Resubmit urb so we continue receiving */
  494. if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
  495. usb_mark_last_busy(port->serial->dev);
  496. err = usb_submit_urb(urb, GFP_ATOMIC);
  497. if (err)
  498. dev_err(&port->dev, "resubmit read urb failed."
  499. "(%d)\n", err);
  500. }
  501. return;
  502. }
  503. static void sierra_instat_callback(struct urb *urb)
  504. {
  505. int err;
  506. int status = urb->status;
  507. struct usb_serial_port *port = urb->context;
  508. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  509. struct usb_serial *serial = port->serial;
  510. dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
  511. urb, port, portdata);
  512. if (status == 0) {
  513. struct usb_ctrlrequest *req_pkt =
  514. (struct usb_ctrlrequest *)urb->transfer_buffer;
  515. if (!req_pkt) {
  516. dev_dbg(&port->dev, "%s: NULL req_pkt\n",
  517. __func__);
  518. return;
  519. }
  520. if ((req_pkt->bRequestType == 0xA1) &&
  521. (req_pkt->bRequest == 0x20)) {
  522. int old_dcd_state;
  523. unsigned char signals = *((unsigned char *)
  524. urb->transfer_buffer +
  525. sizeof(struct usb_ctrlrequest));
  526. struct tty_struct *tty;
  527. dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
  528. signals);
  529. old_dcd_state = portdata->dcd_state;
  530. portdata->cts_state = 1;
  531. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  532. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  533. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  534. tty = tty_port_tty_get(&port->port);
  535. if (tty && !C_CLOCAL(tty) &&
  536. old_dcd_state && !portdata->dcd_state)
  537. tty_hangup(tty);
  538. tty_kref_put(tty);
  539. } else {
  540. dev_dbg(&port->dev, "%s: type %x req %x\n",
  541. __func__, req_pkt->bRequestType,
  542. req_pkt->bRequest);
  543. }
  544. } else
  545. dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
  546. /* Resubmit urb so we continue receiving IRQ data */
  547. if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
  548. usb_mark_last_busy(serial->dev);
  549. urb->dev = serial->dev;
  550. err = usb_submit_urb(urb, GFP_ATOMIC);
  551. if (err)
  552. dev_err(&port->dev, "%s: resubmit intr urb "
  553. "failed. (%d)\n", __func__, err);
  554. }
  555. }
  556. static int sierra_write_room(struct tty_struct *tty)
  557. {
  558. struct usb_serial_port *port = tty->driver_data;
  559. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  560. unsigned long flags;
  561. dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
  562. /* try to give a good number back based on if we have any free urbs at
  563. * this point in time */
  564. spin_lock_irqsave(&portdata->lock, flags);
  565. if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
  566. spin_unlock_irqrestore(&portdata->lock, flags);
  567. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  568. return 0;
  569. }
  570. spin_unlock_irqrestore(&portdata->lock, flags);
  571. return 2048;
  572. }
  573. static void sierra_stop_rx_urbs(struct usb_serial_port *port)
  574. {
  575. int i;
  576. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  577. for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
  578. usb_kill_urb(portdata->in_urbs[i]);
  579. usb_kill_urb(port->interrupt_in_urb);
  580. }
  581. static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
  582. {
  583. int ok_cnt;
  584. int err = -EINVAL;
  585. int i;
  586. struct urb *urb;
  587. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  588. ok_cnt = 0;
  589. for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
  590. urb = portdata->in_urbs[i];
  591. if (!urb)
  592. continue;
  593. err = usb_submit_urb(urb, mem_flags);
  594. if (err) {
  595. dev_err(&port->dev, "%s: submit urb failed: %d\n",
  596. __func__, err);
  597. } else {
  598. ok_cnt++;
  599. }
  600. }
  601. if (ok_cnt && port->interrupt_in_urb) {
  602. err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
  603. if (err) {
  604. dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
  605. __func__, err);
  606. }
  607. }
  608. if (ok_cnt > 0) /* at least one rx urb submitted */
  609. return 0;
  610. else
  611. return err;
  612. }
  613. static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
  614. int dir, void *ctx, int len,
  615. gfp_t mem_flags,
  616. usb_complete_t callback)
  617. {
  618. struct urb *urb;
  619. u8 *buf;
  620. if (endpoint == -1)
  621. return NULL;
  622. urb = usb_alloc_urb(0, mem_flags);
  623. if (urb == NULL) {
  624. dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
  625. __func__, endpoint);
  626. return NULL;
  627. }
  628. buf = kmalloc(len, mem_flags);
  629. if (buf) {
  630. /* Fill URB using supplied data */
  631. usb_fill_bulk_urb(urb, serial->dev,
  632. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  633. buf, len, callback, ctx);
  634. /* debug */
  635. dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
  636. dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
  637. } else {
  638. dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
  639. dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
  640. sierra_release_urb(urb);
  641. urb = NULL;
  642. }
  643. return urb;
  644. }
  645. static void sierra_close(struct usb_serial_port *port)
  646. {
  647. int i;
  648. struct usb_serial *serial = port->serial;
  649. struct sierra_port_private *portdata;
  650. struct sierra_intf_private *intfdata = port->serial->private;
  651. dev_dbg(&port->dev, "%s\n", __func__);
  652. portdata = usb_get_serial_port_data(port);
  653. portdata->rts_state = 0;
  654. portdata->dtr_state = 0;
  655. if (serial->dev) {
  656. mutex_lock(&serial->disc_mutex);
  657. if (!serial->disconnected)
  658. sierra_send_setup(port);
  659. mutex_unlock(&serial->disc_mutex);
  660. spin_lock_irq(&intfdata->susp_lock);
  661. portdata->opened = 0;
  662. spin_unlock_irq(&intfdata->susp_lock);
  663. /* Stop reading urbs */
  664. sierra_stop_rx_urbs(port);
  665. /* .. and release them */
  666. for (i = 0; i < N_IN_URB; i++) {
  667. sierra_release_urb(portdata->in_urbs[i]);
  668. portdata->in_urbs[i] = NULL;
  669. }
  670. usb_autopm_get_interface(serial->interface);
  671. serial->interface->needs_remote_wakeup = 0;
  672. }
  673. }
  674. static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
  675. {
  676. struct sierra_port_private *portdata;
  677. struct usb_serial *serial = port->serial;
  678. struct sierra_intf_private *intfdata = serial->private;
  679. int i;
  680. int err;
  681. int endpoint;
  682. struct urb *urb;
  683. portdata = usb_get_serial_port_data(port);
  684. dev_dbg(&port->dev, "%s\n", __func__);
  685. /* Set some sane defaults */
  686. portdata->rts_state = 1;
  687. portdata->dtr_state = 1;
  688. endpoint = port->bulk_in_endpointAddress;
  689. for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
  690. urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
  691. IN_BUFLEN, GFP_KERNEL,
  692. sierra_indat_callback);
  693. portdata->in_urbs[i] = urb;
  694. }
  695. /* clear halt condition */
  696. usb_clear_halt(serial->dev,
  697. usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
  698. err = sierra_submit_rx_urbs(port, GFP_KERNEL);
  699. if (err) {
  700. /* get rid of everything as in close */
  701. sierra_close(port);
  702. return err;
  703. }
  704. sierra_send_setup(port);
  705. serial->interface->needs_remote_wakeup = 1;
  706. spin_lock_irq(&intfdata->susp_lock);
  707. portdata->opened = 1;
  708. spin_unlock_irq(&intfdata->susp_lock);
  709. usb_autopm_put_interface(serial->interface);
  710. return 0;
  711. }
  712. static void sierra_dtr_rts(struct usb_serial_port *port, int on)
  713. {
  714. struct usb_serial *serial = port->serial;
  715. struct sierra_port_private *portdata;
  716. portdata = usb_get_serial_port_data(port);
  717. portdata->rts_state = on;
  718. portdata->dtr_state = on;
  719. if (serial->dev) {
  720. mutex_lock(&serial->disc_mutex);
  721. if (!serial->disconnected)
  722. sierra_send_setup(port);
  723. mutex_unlock(&serial->disc_mutex);
  724. }
  725. }
  726. static int sierra_startup(struct usb_serial *serial)
  727. {
  728. struct usb_serial_port *port;
  729. struct sierra_port_private *portdata;
  730. int i;
  731. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  732. /* Set Device mode to D0 */
  733. sierra_set_power_state(serial->dev, 0x0000);
  734. /* Check NMEA and set */
  735. if (nmea)
  736. sierra_vsc_set_nmea(serial->dev, 1);
  737. /* Now setup per port private data */
  738. for (i = 0; i < serial->num_ports; i++) {
  739. port = serial->port[i];
  740. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  741. if (!portdata) {
  742. dev_dbg(&port->dev, "%s: kmalloc for "
  743. "sierra_port_private (%d) failed!.\n",
  744. __func__, i);
  745. return -ENOMEM;
  746. }
  747. spin_lock_init(&portdata->lock);
  748. init_usb_anchor(&portdata->active);
  749. init_usb_anchor(&portdata->delayed);
  750. /* Set the port private data pointer */
  751. usb_set_serial_port_data(port, portdata);
  752. }
  753. return 0;
  754. }
  755. static void sierra_release(struct usb_serial *serial)
  756. {
  757. int i;
  758. struct usb_serial_port *port;
  759. struct sierra_port_private *portdata;
  760. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  761. for (i = 0; i < serial->num_ports; ++i) {
  762. port = serial->port[i];
  763. if (!port)
  764. continue;
  765. portdata = usb_get_serial_port_data(port);
  766. if (!portdata)
  767. continue;
  768. kfree(portdata);
  769. }
  770. }
  771. static void stop_read_write_urbs(struct usb_serial *serial)
  772. {
  773. int i, j;
  774. struct usb_serial_port *port;
  775. struct sierra_port_private *portdata;
  776. /* Stop reading/writing urbs */
  777. for (i = 0; i < serial->num_ports; ++i) {
  778. port = serial->port[i];
  779. portdata = usb_get_serial_port_data(port);
  780. for (j = 0; j < N_IN_URB; j++)
  781. usb_kill_urb(portdata->in_urbs[j]);
  782. usb_kill_anchored_urbs(&portdata->active);
  783. }
  784. }
  785. static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
  786. {
  787. struct sierra_intf_private *intfdata;
  788. int b;
  789. if (serial->dev->auto_pm) {
  790. intfdata = serial->private;
  791. spin_lock_irq(&intfdata->susp_lock);
  792. b = intfdata->in_flight;
  793. if (b) {
  794. spin_unlock_irq(&intfdata->susp_lock);
  795. return -EBUSY;
  796. } else {
  797. intfdata->suspended = 1;
  798. spin_unlock_irq(&intfdata->susp_lock);
  799. }
  800. }
  801. stop_read_write_urbs(serial);
  802. return 0;
  803. }
  804. static int sierra_resume(struct usb_serial *serial)
  805. {
  806. struct usb_serial_port *port;
  807. struct sierra_intf_private *intfdata = serial->private;
  808. struct sierra_port_private *portdata;
  809. struct urb *urb;
  810. int ec = 0;
  811. int i, err;
  812. spin_lock_irq(&intfdata->susp_lock);
  813. for (i = 0; i < serial->num_ports; i++) {
  814. port = serial->port[i];
  815. portdata = usb_get_serial_port_data(port);
  816. while ((urb = usb_get_from_anchor(&portdata->delayed))) {
  817. usb_anchor_urb(urb, &portdata->active);
  818. intfdata->in_flight++;
  819. err = usb_submit_urb(urb, GFP_ATOMIC);
  820. if (err < 0) {
  821. intfdata->in_flight--;
  822. usb_unanchor_urb(urb);
  823. usb_scuttle_anchored_urbs(&portdata->delayed);
  824. break;
  825. }
  826. }
  827. if (portdata->opened) {
  828. err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
  829. if (err)
  830. ec++;
  831. }
  832. }
  833. intfdata->suspended = 0;
  834. spin_unlock_irq(&intfdata->susp_lock);
  835. return ec ? -EIO : 0;
  836. }
  837. static struct usb_serial_driver sierra_device = {
  838. .driver = {
  839. .owner = THIS_MODULE,
  840. .name = "sierra",
  841. },
  842. .description = "Sierra USB modem",
  843. .id_table = id_table,
  844. .usb_driver = &sierra_driver,
  845. .calc_num_ports = sierra_calc_num_ports,
  846. .probe = sierra_probe,
  847. .open = sierra_open,
  848. .close = sierra_close,
  849. .dtr_rts = sierra_dtr_rts,
  850. .write = sierra_write,
  851. .write_room = sierra_write_room,
  852. .set_termios = sierra_set_termios,
  853. .tiocmget = sierra_tiocmget,
  854. .tiocmset = sierra_tiocmset,
  855. .attach = sierra_startup,
  856. .release = sierra_release,
  857. .suspend = sierra_suspend,
  858. .resume = sierra_resume,
  859. .read_int_callback = sierra_instat_callback,
  860. };
  861. /* Functions used by new usb-serial code. */
  862. static int __init sierra_init(void)
  863. {
  864. int retval;
  865. retval = usb_serial_register(&sierra_device);
  866. if (retval)
  867. goto failed_device_register;
  868. retval = usb_register(&sierra_driver);
  869. if (retval)
  870. goto failed_driver_register;
  871. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  872. DRIVER_DESC "\n");
  873. return 0;
  874. failed_driver_register:
  875. usb_serial_deregister(&sierra_device);
  876. failed_device_register:
  877. return retval;
  878. }
  879. static void __exit sierra_exit(void)
  880. {
  881. usb_deregister(&sierra_driver);
  882. usb_serial_deregister(&sierra_device);
  883. }
  884. module_init(sierra_init);
  885. module_exit(sierra_exit);
  886. MODULE_AUTHOR(DRIVER_AUTHOR);
  887. MODULE_DESCRIPTION(DRIVER_DESC);
  888. MODULE_VERSION(DRIVER_VERSION);
  889. MODULE_LICENSE("GPL");
  890. module_param(nmea, bool, S_IRUGO | S_IWUSR);
  891. MODULE_PARM_DESC(nmea, "NMEA streaming");
  892. module_param(debug, bool, S_IRUGO | S_IWUSR);
  893. MODULE_PARM_DESC(debug, "Debug messages");