sierra.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. USB Driver for Sierra Wireless
  4. Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
  5. Copyright (C) 2008, 2009 Elina Pasheva, Matthew Safar, Rory Filer
  6. <linux@sierrawireless.com>
  7. IMPORTANT DISCLAIMER: This driver is not commercially supported by
  8. Sierra Wireless. Use at your own risk.
  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. /* Uncomment to log function calls */
  13. /* #define DEBUG */
  14. #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
  15. #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
  16. #include <linux/kernel.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/errno.h>
  19. #include <linux/tty.h>
  20. #include <linux/slab.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_HM 8
  28. #define N_OUT_URB_HM 64
  29. #define N_IN_URB 4
  30. #define N_OUT_URB 4
  31. #define IN_BUFLEN 4096
  32. #define MAX_TRANSFER (PAGE_SIZE - 512)
  33. /* MAX_TRANSFER is chosen so that the VM is not stressed by
  34. allocations > PAGE_SIZE and the number of packets in a page
  35. is an integer 512 is the largest possible packet on EHCI */
  36. static bool nmea;
  37. /* Used in interface blacklisting */
  38. struct sierra_iface_info {
  39. const u32 infolen; /* number of interface numbers on blacklist */
  40. const u8 *ifaceinfo; /* pointer to the array holding the numbers */
  41. };
  42. struct sierra_intf_private {
  43. spinlock_t susp_lock;
  44. unsigned int suspended:1;
  45. int in_flight;
  46. unsigned int open_ports;
  47. };
  48. static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
  49. {
  50. return 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. }
  59. static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
  60. {
  61. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  62. SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
  63. USB_TYPE_VENDOR, /* __u8 request type */
  64. enable, /* __u16 value */
  65. 0x0000, /* __u16 index */
  66. NULL, /* void *data */
  67. 0, /* __u16 size */
  68. USB_CTRL_SET_TIMEOUT); /* int timeout */
  69. }
  70. static int sierra_calc_num_ports(struct usb_serial *serial,
  71. struct usb_serial_endpoints *epds)
  72. {
  73. int num_ports = 0;
  74. u8 ifnum, numendpoints;
  75. ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
  76. numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
  77. /* Dummy interface present on some SKUs should be ignored */
  78. if (ifnum == 0x99)
  79. num_ports = 0;
  80. else if (numendpoints <= 3)
  81. num_ports = 1;
  82. else
  83. num_ports = (numendpoints-1)/2;
  84. return num_ports;
  85. }
  86. static int is_blacklisted(const u8 ifnum,
  87. const struct sierra_iface_info *blacklist)
  88. {
  89. const u8 *info;
  90. int i;
  91. if (blacklist) {
  92. info = blacklist->ifaceinfo;
  93. for (i = 0; i < blacklist->infolen; i++) {
  94. if (info[i] == ifnum)
  95. return 1;
  96. }
  97. }
  98. return 0;
  99. }
  100. static int is_himemory(const u8 ifnum,
  101. const struct sierra_iface_info *himemorylist)
  102. {
  103. const u8 *info;
  104. int i;
  105. if (himemorylist) {
  106. info = himemorylist->ifaceinfo;
  107. for (i=0; i < himemorylist->infolen; i++) {
  108. if (info[i] == ifnum)
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114. static u8 sierra_interface_num(struct usb_serial *serial)
  115. {
  116. return serial->interface->cur_altsetting->desc.bInterfaceNumber;
  117. }
  118. static int sierra_probe(struct usb_serial *serial,
  119. const struct usb_device_id *id)
  120. {
  121. int result = 0;
  122. struct usb_device *udev;
  123. u8 ifnum;
  124. udev = serial->dev;
  125. ifnum = sierra_interface_num(serial);
  126. /*
  127. * If this interface supports more than 1 alternate
  128. * select the 2nd one
  129. */
  130. if (serial->interface->num_altsetting == 2) {
  131. dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
  132. ifnum);
  133. /* We know the alternate setting is 1 for the MC8785 */
  134. usb_set_interface(udev, ifnum, 1);
  135. }
  136. if (is_blacklisted(ifnum,
  137. (struct sierra_iface_info *)id->driver_info)) {
  138. dev_dbg(&serial->dev->dev,
  139. "Ignoring blacklisted interface #%d\n", ifnum);
  140. return -ENODEV;
  141. }
  142. return result;
  143. }
  144. /* interfaces with higher memory requirements */
  145. static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
  146. static const struct sierra_iface_info typeA_interface_list = {
  147. .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
  148. .ifaceinfo = hi_memory_typeA_ifaces,
  149. };
  150. static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
  151. static const struct sierra_iface_info typeB_interface_list = {
  152. .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
  153. .ifaceinfo = hi_memory_typeB_ifaces,
  154. };
  155. /* 'blacklist' of interfaces not served by this driver */
  156. static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11, 19, 20 };
  157. static const struct sierra_iface_info direct_ip_interface_blacklist = {
  158. .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
  159. .ifaceinfo = direct_ip_non_serial_ifaces,
  160. };
  161. static const struct usb_device_id id_table[] = {
  162. { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
  163. { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
  164. { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
  165. { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
  166. { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
  167. { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
  168. { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
  169. { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
  170. { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
  171. { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
  172. { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
  173. { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
  174. { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
  175. { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
  176. { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
  177. { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
  178. { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U */
  179. /* Sierra Wireless C597 */
  180. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
  181. /* Sierra Wireless T598 */
  182. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
  183. { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
  184. { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
  185. { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
  186. { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
  187. { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
  188. { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
  189. { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
  190. { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
  191. { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
  192. { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
  193. { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
  194. { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
  195. { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
  196. { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
  197. { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
  198. { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
  199. { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
  200. { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
  201. { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
  202. { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
  203. { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
  204. { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
  205. { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
  206. { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
  207. { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
  208. /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
  209. { USB_DEVICE(0x1199, 0x683C) },
  210. { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
  211. /* Sierra Wireless MC8790, MC8791, MC8792 */
  212. { USB_DEVICE(0x1199, 0x683E) },
  213. { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
  214. { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
  215. { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
  216. { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
  217. { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
  218. { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
  219. { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
  220. { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
  221. /* Sierra Wireless C885 */
  222. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
  223. /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
  224. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
  225. /* Sierra Wireless C22/C33 */
  226. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
  227. /* Sierra Wireless HSPA Non-Composite Device */
  228. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
  229. { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
  230. /* Sierra Wireless Direct IP modems */
  231. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
  232. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  233. },
  234. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
  235. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  236. },
  237. { USB_DEVICE(0x1199, 0x68AB) }, /* Sierra Wireless AR8550 */
  238. /* AT&T Direct IP LTE modems */
  239. { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
  240. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  241. },
  242. /* Airprime/Sierra Wireless Direct IP modems */
  243. { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
  244. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  245. },
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(usb, id_table);
  249. struct sierra_port_private {
  250. spinlock_t lock; /* lock the structure */
  251. int outstanding_urbs; /* number of out urbs in flight */
  252. struct usb_anchor active;
  253. struct usb_anchor delayed;
  254. int num_out_urbs;
  255. int num_in_urbs;
  256. /* Input endpoints and buffers for this port */
  257. struct urb *in_urbs[N_IN_URB_HM];
  258. /* Settings for the port */
  259. int rts_state; /* Handshaking pins (outputs) */
  260. int dtr_state;
  261. int cts_state; /* Handshaking pins (inputs) */
  262. int dsr_state;
  263. int dcd_state;
  264. int ri_state;
  265. };
  266. static int sierra_send_setup(struct usb_serial_port *port)
  267. {
  268. struct usb_serial *serial = port->serial;
  269. struct sierra_port_private *portdata;
  270. __u16 interface = 0;
  271. int val = 0;
  272. int do_send = 0;
  273. int retval;
  274. portdata = usb_get_serial_port_data(port);
  275. if (portdata->dtr_state)
  276. val |= 0x01;
  277. if (portdata->rts_state)
  278. val |= 0x02;
  279. /* If composite device then properly report interface */
  280. if (serial->num_ports == 1) {
  281. interface = sierra_interface_num(serial);
  282. /* Control message is sent only to interfaces with
  283. * interrupt_in endpoints
  284. */
  285. if (port->interrupt_in_urb) {
  286. /* send control message */
  287. do_send = 1;
  288. }
  289. }
  290. /* Otherwise the need to do non-composite mapping */
  291. else {
  292. if (port->bulk_out_endpointAddress == 2)
  293. interface = 0;
  294. else if (port->bulk_out_endpointAddress == 4)
  295. interface = 1;
  296. else if (port->bulk_out_endpointAddress == 5)
  297. interface = 2;
  298. do_send = 1;
  299. }
  300. if (!do_send)
  301. return 0;
  302. retval = usb_autopm_get_interface(serial->interface);
  303. if (retval < 0)
  304. return retval;
  305. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  306. 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
  307. usb_autopm_put_interface(serial->interface);
  308. return retval;
  309. }
  310. static int sierra_tiocmget(struct tty_struct *tty)
  311. {
  312. struct usb_serial_port *port = tty->driver_data;
  313. unsigned int value;
  314. struct sierra_port_private *portdata;
  315. portdata = usb_get_serial_port_data(port);
  316. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  317. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  318. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  319. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  320. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  321. ((portdata->ri_state) ? TIOCM_RNG : 0);
  322. return value;
  323. }
  324. static int sierra_tiocmset(struct tty_struct *tty,
  325. unsigned int set, unsigned int clear)
  326. {
  327. struct usb_serial_port *port = tty->driver_data;
  328. struct sierra_port_private *portdata;
  329. portdata = usb_get_serial_port_data(port);
  330. if (set & TIOCM_RTS)
  331. portdata->rts_state = 1;
  332. if (set & TIOCM_DTR)
  333. portdata->dtr_state = 1;
  334. if (clear & TIOCM_RTS)
  335. portdata->rts_state = 0;
  336. if (clear & TIOCM_DTR)
  337. portdata->dtr_state = 0;
  338. return sierra_send_setup(port);
  339. }
  340. static void sierra_release_urb(struct urb *urb)
  341. {
  342. if (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. unsigned long flags;
  354. intfdata = usb_get_serial_data(port->serial);
  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_irqsave(&portdata->lock, flags);
  362. --portdata->outstanding_urbs;
  363. spin_unlock_irqrestore(&portdata->lock, flags);
  364. spin_lock_irqsave(&intfdata->susp_lock, flags);
  365. --intfdata->in_flight;
  366. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  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;
  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 = usb_get_serial_data(serial);
  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 > portdata->num_out_urbs) {
  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. retval = -ENOMEM;
  409. goto error_no_buffer;
  410. }
  411. urb = usb_alloc_urb(0, GFP_ATOMIC);
  412. if (!urb) {
  413. retval = -ENOMEM;
  414. goto error_no_urb;
  415. }
  416. memcpy(buffer, buf, writesize);
  417. usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
  418. usb_fill_bulk_urb(urb, serial->dev,
  419. usb_sndbulkpipe(serial->dev,
  420. port->bulk_out_endpointAddress),
  421. buffer, writesize, sierra_outdat_callback, port);
  422. /* Handle the need to send a zero length packet */
  423. urb->transfer_flags |= URB_ZERO_PACKET;
  424. spin_lock_irqsave(&intfdata->susp_lock, flags);
  425. if (intfdata->suspended) {
  426. usb_anchor_urb(urb, &portdata->delayed);
  427. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  428. goto skip_power;
  429. } else {
  430. usb_anchor_urb(urb, &portdata->active);
  431. }
  432. /* send it down the pipe */
  433. retval = usb_submit_urb(urb, GFP_ATOMIC);
  434. if (retval) {
  435. usb_unanchor_urb(urb);
  436. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  437. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  438. "with status = %d\n", __func__, retval);
  439. goto error;
  440. } else {
  441. intfdata->in_flight++;
  442. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  443. }
  444. skip_power:
  445. /* we are done with this urb, so let the host driver
  446. * really free it when it is finished with it */
  447. usb_free_urb(urb);
  448. return writesize;
  449. error:
  450. usb_free_urb(urb);
  451. error_no_urb:
  452. kfree(buffer);
  453. error_no_buffer:
  454. spin_lock_irqsave(&portdata->lock, flags);
  455. --portdata->outstanding_urbs;
  456. dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
  457. portdata->outstanding_urbs);
  458. spin_unlock_irqrestore(&portdata->lock, flags);
  459. usb_autopm_put_interface_async(serial->interface);
  460. error_simple:
  461. return retval;
  462. }
  463. static void sierra_indat_callback(struct urb *urb)
  464. {
  465. int err;
  466. int endpoint;
  467. struct usb_serial_port *port;
  468. unsigned char *data = urb->transfer_buffer;
  469. int status = urb->status;
  470. endpoint = usb_pipeendpoint(urb->pipe);
  471. port = urb->context;
  472. if (status) {
  473. dev_dbg(&port->dev, "%s: nonzero status: %d on"
  474. " endpoint %02x\n", __func__, status, endpoint);
  475. } else {
  476. if (urb->actual_length) {
  477. tty_insert_flip_string(&port->port, data,
  478. urb->actual_length);
  479. tty_flip_buffer_push(&port->port);
  480. usb_serial_debug_data(&port->dev, __func__,
  481. urb->actual_length, data);
  482. } else {
  483. dev_dbg(&port->dev, "%s: empty read urb"
  484. " received\n", __func__);
  485. }
  486. }
  487. /* Resubmit urb so we continue receiving */
  488. if (status != -ESHUTDOWN && status != -EPERM) {
  489. usb_mark_last_busy(port->serial->dev);
  490. err = usb_submit_urb(urb, GFP_ATOMIC);
  491. if (err && err != -EPERM)
  492. dev_err(&port->dev, "resubmit read urb failed."
  493. "(%d)\n", err);
  494. }
  495. }
  496. static void sierra_instat_callback(struct urb *urb)
  497. {
  498. int err;
  499. int status = urb->status;
  500. struct usb_serial_port *port = urb->context;
  501. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  502. struct usb_serial *serial = port->serial;
  503. dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
  504. urb, port, portdata);
  505. if (status == 0) {
  506. struct usb_ctrlrequest *req_pkt =
  507. (struct usb_ctrlrequest *)urb->transfer_buffer;
  508. if (!req_pkt) {
  509. dev_dbg(&port->dev, "%s: NULL req_pkt\n",
  510. __func__);
  511. return;
  512. }
  513. if ((req_pkt->bRequestType == 0xA1) &&
  514. (req_pkt->bRequest == 0x20)) {
  515. int old_dcd_state;
  516. unsigned char signals = *((unsigned char *)
  517. urb->transfer_buffer +
  518. sizeof(struct usb_ctrlrequest));
  519. dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
  520. signals);
  521. old_dcd_state = portdata->dcd_state;
  522. portdata->cts_state = 1;
  523. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  524. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  525. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  526. if (old_dcd_state && !portdata->dcd_state)
  527. tty_port_tty_hangup(&port->port, true);
  528. } else {
  529. dev_dbg(&port->dev, "%s: type %x req %x\n",
  530. __func__, req_pkt->bRequestType,
  531. req_pkt->bRequest);
  532. }
  533. } else
  534. dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
  535. /* Resubmit urb so we continue receiving IRQ data */
  536. if (status != -ESHUTDOWN && status != -ENOENT) {
  537. usb_mark_last_busy(serial->dev);
  538. err = usb_submit_urb(urb, GFP_ATOMIC);
  539. if (err && err != -EPERM)
  540. dev_err(&port->dev, "%s: resubmit intr urb "
  541. "failed. (%d)\n", __func__, err);
  542. }
  543. }
  544. static int sierra_write_room(struct tty_struct *tty)
  545. {
  546. struct usb_serial_port *port = tty->driver_data;
  547. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  548. unsigned long flags;
  549. /* try to give a good number back based on if we have any free urbs at
  550. * this point in time */
  551. spin_lock_irqsave(&portdata->lock, flags);
  552. if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
  553. spin_unlock_irqrestore(&portdata->lock, flags);
  554. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  555. return 0;
  556. }
  557. spin_unlock_irqrestore(&portdata->lock, flags);
  558. return 2048;
  559. }
  560. static int sierra_chars_in_buffer(struct tty_struct *tty)
  561. {
  562. struct usb_serial_port *port = tty->driver_data;
  563. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  564. unsigned long flags;
  565. int chars;
  566. /* NOTE: This overcounts somewhat. */
  567. spin_lock_irqsave(&portdata->lock, flags);
  568. chars = portdata->outstanding_urbs * MAX_TRANSFER;
  569. spin_unlock_irqrestore(&portdata->lock, flags);
  570. dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
  571. return chars;
  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 < portdata->num_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 < portdata->num_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. urb = usb_alloc_urb(0, mem_flags);
  621. if (!urb)
  622. return NULL;
  623. buf = kmalloc(len, mem_flags);
  624. if (buf) {
  625. /* Fill URB using supplied data */
  626. usb_fill_bulk_urb(urb, serial->dev,
  627. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  628. buf, len, callback, ctx);
  629. dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
  630. dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
  631. } else {
  632. sierra_release_urb(urb);
  633. urb = NULL;
  634. }
  635. return urb;
  636. }
  637. static void sierra_close(struct usb_serial_port *port)
  638. {
  639. int i;
  640. struct usb_serial *serial = port->serial;
  641. struct sierra_port_private *portdata;
  642. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  643. struct urb *urb;
  644. portdata = usb_get_serial_port_data(port);
  645. /*
  646. * Need to take susp_lock to make sure port is not already being
  647. * resumed, but no need to hold it due to initialized
  648. */
  649. spin_lock_irq(&intfdata->susp_lock);
  650. if (--intfdata->open_ports == 0)
  651. serial->interface->needs_remote_wakeup = 0;
  652. spin_unlock_irq(&intfdata->susp_lock);
  653. for (;;) {
  654. urb = usb_get_from_anchor(&portdata->delayed);
  655. if (!urb)
  656. break;
  657. kfree(urb->transfer_buffer);
  658. usb_free_urb(urb);
  659. usb_autopm_put_interface_async(serial->interface);
  660. spin_lock_irq(&portdata->lock);
  661. portdata->outstanding_urbs--;
  662. spin_unlock_irq(&portdata->lock);
  663. }
  664. sierra_stop_rx_urbs(port);
  665. usb_kill_anchored_urbs(&portdata->active);
  666. for (i = 0; i < portdata->num_in_urbs; i++) {
  667. sierra_release_urb(portdata->in_urbs[i]);
  668. portdata->in_urbs[i] = NULL;
  669. }
  670. usb_autopm_get_interface_no_resume(serial->interface);
  671. }
  672. static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
  673. {
  674. struct sierra_port_private *portdata;
  675. struct usb_serial *serial = port->serial;
  676. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  677. int i;
  678. int err;
  679. int endpoint;
  680. struct urb *urb;
  681. portdata = usb_get_serial_port_data(port);
  682. endpoint = port->bulk_in_endpointAddress;
  683. for (i = 0; i < portdata->num_in_urbs; i++) {
  684. urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
  685. IN_BUFLEN, GFP_KERNEL,
  686. sierra_indat_callback);
  687. portdata->in_urbs[i] = urb;
  688. }
  689. /* clear halt condition */
  690. usb_clear_halt(serial->dev,
  691. usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
  692. err = sierra_submit_rx_urbs(port, GFP_KERNEL);
  693. if (err)
  694. goto err_submit;
  695. spin_lock_irq(&intfdata->susp_lock);
  696. if (++intfdata->open_ports == 1)
  697. serial->interface->needs_remote_wakeup = 1;
  698. spin_unlock_irq(&intfdata->susp_lock);
  699. usb_autopm_put_interface(serial->interface);
  700. return 0;
  701. err_submit:
  702. sierra_stop_rx_urbs(port);
  703. for (i = 0; i < portdata->num_in_urbs; i++) {
  704. sierra_release_urb(portdata->in_urbs[i]);
  705. portdata->in_urbs[i] = NULL;
  706. }
  707. return err;
  708. }
  709. static void sierra_dtr_rts(struct usb_serial_port *port, int on)
  710. {
  711. struct sierra_port_private *portdata;
  712. portdata = usb_get_serial_port_data(port);
  713. portdata->rts_state = on;
  714. portdata->dtr_state = on;
  715. sierra_send_setup(port);
  716. }
  717. static int sierra_startup(struct usb_serial *serial)
  718. {
  719. struct sierra_intf_private *intfdata;
  720. intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
  721. if (!intfdata)
  722. return -ENOMEM;
  723. spin_lock_init(&intfdata->susp_lock);
  724. usb_set_serial_data(serial, intfdata);
  725. /* Set Device mode to D0 */
  726. sierra_set_power_state(serial->dev, 0x0000);
  727. /* Check NMEA and set */
  728. if (nmea)
  729. sierra_vsc_set_nmea(serial->dev, 1);
  730. return 0;
  731. }
  732. static void sierra_release(struct usb_serial *serial)
  733. {
  734. struct sierra_intf_private *intfdata;
  735. intfdata = usb_get_serial_data(serial);
  736. kfree(intfdata);
  737. }
  738. static int sierra_port_probe(struct usb_serial_port *port)
  739. {
  740. struct usb_serial *serial = port->serial;
  741. struct sierra_port_private *portdata;
  742. const struct sierra_iface_info *himemoryp;
  743. u8 ifnum;
  744. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  745. if (!portdata)
  746. return -ENOMEM;
  747. spin_lock_init(&portdata->lock);
  748. init_usb_anchor(&portdata->active);
  749. init_usb_anchor(&portdata->delayed);
  750. /* Assume low memory requirements */
  751. portdata->num_out_urbs = N_OUT_URB;
  752. portdata->num_in_urbs = N_IN_URB;
  753. /* Determine actual memory requirements */
  754. if (serial->num_ports == 1) {
  755. /* Get interface number for composite device */
  756. ifnum = sierra_interface_num(serial);
  757. himemoryp = &typeB_interface_list;
  758. } else {
  759. /* This is really the usb-serial port number of the interface
  760. * rather than the interface number.
  761. */
  762. ifnum = port->port_number;
  763. himemoryp = &typeA_interface_list;
  764. }
  765. if (is_himemory(ifnum, himemoryp)) {
  766. portdata->num_out_urbs = N_OUT_URB_HM;
  767. portdata->num_in_urbs = N_IN_URB_HM;
  768. }
  769. dev_dbg(&port->dev,
  770. "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
  771. ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
  772. usb_set_serial_port_data(port, portdata);
  773. return 0;
  774. }
  775. static int sierra_port_remove(struct usb_serial_port *port)
  776. {
  777. struct sierra_port_private *portdata;
  778. portdata = usb_get_serial_port_data(port);
  779. usb_set_serial_port_data(port, NULL);
  780. kfree(portdata);
  781. return 0;
  782. }
  783. #ifdef CONFIG_PM
  784. static void stop_read_write_urbs(struct usb_serial *serial)
  785. {
  786. int i;
  787. struct usb_serial_port *port;
  788. struct sierra_port_private *portdata;
  789. /* Stop reading/writing urbs */
  790. for (i = 0; i < serial->num_ports; ++i) {
  791. port = serial->port[i];
  792. portdata = usb_get_serial_port_data(port);
  793. if (!portdata)
  794. continue;
  795. sierra_stop_rx_urbs(port);
  796. usb_kill_anchored_urbs(&portdata->active);
  797. }
  798. }
  799. static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
  800. {
  801. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  802. spin_lock_irq(&intfdata->susp_lock);
  803. if (PMSG_IS_AUTO(message)) {
  804. if (intfdata->in_flight) {
  805. spin_unlock_irq(&intfdata->susp_lock);
  806. return -EBUSY;
  807. }
  808. }
  809. intfdata->suspended = 1;
  810. spin_unlock_irq(&intfdata->susp_lock);
  811. stop_read_write_urbs(serial);
  812. return 0;
  813. }
  814. /* Caller must hold susp_lock. */
  815. static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
  816. {
  817. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  818. struct sierra_intf_private *intfdata;
  819. struct urb *urb;
  820. int ec = 0;
  821. int err;
  822. intfdata = usb_get_serial_data(port->serial);
  823. for (;;) {
  824. urb = usb_get_from_anchor(&portdata->delayed);
  825. if (!urb)
  826. break;
  827. usb_anchor_urb(urb, &portdata->active);
  828. intfdata->in_flight++;
  829. err = usb_submit_urb(urb, GFP_ATOMIC);
  830. if (err) {
  831. dev_err(&port->dev, "%s - submit urb failed: %d",
  832. __func__, err);
  833. ec++;
  834. intfdata->in_flight--;
  835. usb_unanchor_urb(urb);
  836. kfree(urb->transfer_buffer);
  837. usb_free_urb(urb);
  838. spin_lock(&portdata->lock);
  839. portdata->outstanding_urbs--;
  840. spin_unlock(&portdata->lock);
  841. }
  842. }
  843. if (ec)
  844. return -EIO;
  845. return 0;
  846. }
  847. static int sierra_resume(struct usb_serial *serial)
  848. {
  849. struct usb_serial_port *port;
  850. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  851. int ec = 0;
  852. int i, err;
  853. spin_lock_irq(&intfdata->susp_lock);
  854. for (i = 0; i < serial->num_ports; i++) {
  855. port = serial->port[i];
  856. if (!tty_port_initialized(&port->port))
  857. continue;
  858. err = sierra_submit_delayed_urbs(port);
  859. if (err)
  860. ec++;
  861. err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
  862. if (err)
  863. ec++;
  864. }
  865. intfdata->suspended = 0;
  866. spin_unlock_irq(&intfdata->susp_lock);
  867. return ec ? -EIO : 0;
  868. }
  869. #else
  870. #define sierra_suspend NULL
  871. #define sierra_resume NULL
  872. #endif
  873. static struct usb_serial_driver sierra_device = {
  874. .driver = {
  875. .owner = THIS_MODULE,
  876. .name = "sierra",
  877. },
  878. .description = "Sierra USB modem",
  879. .id_table = id_table,
  880. .calc_num_ports = sierra_calc_num_ports,
  881. .probe = sierra_probe,
  882. .open = sierra_open,
  883. .close = sierra_close,
  884. .dtr_rts = sierra_dtr_rts,
  885. .write = sierra_write,
  886. .write_room = sierra_write_room,
  887. .chars_in_buffer = sierra_chars_in_buffer,
  888. .tiocmget = sierra_tiocmget,
  889. .tiocmset = sierra_tiocmset,
  890. .attach = sierra_startup,
  891. .release = sierra_release,
  892. .port_probe = sierra_port_probe,
  893. .port_remove = sierra_port_remove,
  894. .suspend = sierra_suspend,
  895. .resume = sierra_resume,
  896. .read_int_callback = sierra_instat_callback,
  897. };
  898. static struct usb_serial_driver * const serial_drivers[] = {
  899. &sierra_device, NULL
  900. };
  901. module_usb_serial_driver(serial_drivers, id_table);
  902. MODULE_AUTHOR(DRIVER_AUTHOR);
  903. MODULE_DESCRIPTION(DRIVER_DESC);
  904. MODULE_LICENSE("GPL v2");
  905. module_param(nmea, bool, S_IRUGO | S_IWUSR);
  906. MODULE_PARM_DESC(nmea, "NMEA streaming");