sierra.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. intfdata = usb_get_serial_data(port->serial);
  354. /* free up the transfer buffer, as usb_free_urb() does not do this */
  355. kfree(urb->transfer_buffer);
  356. usb_autopm_put_interface_async(port->serial->interface);
  357. if (status)
  358. dev_dbg(&port->dev, "%s - nonzero write bulk status "
  359. "received: %d\n", __func__, status);
  360. spin_lock(&portdata->lock);
  361. --portdata->outstanding_urbs;
  362. spin_unlock(&portdata->lock);
  363. spin_lock(&intfdata->susp_lock);
  364. --intfdata->in_flight;
  365. spin_unlock(&intfdata->susp_lock);
  366. usb_serial_port_softint(port);
  367. }
  368. /* Write */
  369. static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
  370. const unsigned char *buf, int count)
  371. {
  372. struct sierra_port_private *portdata;
  373. struct sierra_intf_private *intfdata;
  374. struct usb_serial *serial = port->serial;
  375. unsigned long flags;
  376. unsigned char *buffer;
  377. struct urb *urb;
  378. size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
  379. int retval = 0;
  380. /* verify that we actually have some data to write */
  381. if (count == 0)
  382. return 0;
  383. portdata = usb_get_serial_port_data(port);
  384. intfdata = usb_get_serial_data(serial);
  385. dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
  386. spin_lock_irqsave(&portdata->lock, flags);
  387. dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
  388. portdata->outstanding_urbs);
  389. if (portdata->outstanding_urbs > portdata->num_out_urbs) {
  390. spin_unlock_irqrestore(&portdata->lock, flags);
  391. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  392. return 0;
  393. }
  394. portdata->outstanding_urbs++;
  395. dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
  396. portdata->outstanding_urbs);
  397. spin_unlock_irqrestore(&portdata->lock, flags);
  398. retval = usb_autopm_get_interface_async(serial->interface);
  399. if (retval < 0) {
  400. spin_lock_irqsave(&portdata->lock, flags);
  401. portdata->outstanding_urbs--;
  402. spin_unlock_irqrestore(&portdata->lock, flags);
  403. goto error_simple;
  404. }
  405. buffer = kmalloc(writesize, GFP_ATOMIC);
  406. if (!buffer) {
  407. retval = -ENOMEM;
  408. goto error_no_buffer;
  409. }
  410. urb = usb_alloc_urb(0, GFP_ATOMIC);
  411. if (!urb) {
  412. retval = -ENOMEM;
  413. goto error_no_urb;
  414. }
  415. memcpy(buffer, buf, writesize);
  416. usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
  417. usb_fill_bulk_urb(urb, serial->dev,
  418. usb_sndbulkpipe(serial->dev,
  419. port->bulk_out_endpointAddress),
  420. buffer, writesize, sierra_outdat_callback, port);
  421. /* Handle the need to send a zero length packet */
  422. urb->transfer_flags |= URB_ZERO_PACKET;
  423. spin_lock_irqsave(&intfdata->susp_lock, flags);
  424. if (intfdata->suspended) {
  425. usb_anchor_urb(urb, &portdata->delayed);
  426. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  427. goto skip_power;
  428. } else {
  429. usb_anchor_urb(urb, &portdata->active);
  430. }
  431. /* send it down the pipe */
  432. retval = usb_submit_urb(urb, GFP_ATOMIC);
  433. if (retval) {
  434. usb_unanchor_urb(urb);
  435. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  436. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  437. "with status = %d\n", __func__, retval);
  438. goto error;
  439. } else {
  440. intfdata->in_flight++;
  441. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  442. }
  443. skip_power:
  444. /* we are done with this urb, so let the host driver
  445. * really free it when it is finished with it */
  446. usb_free_urb(urb);
  447. return writesize;
  448. error:
  449. usb_free_urb(urb);
  450. error_no_urb:
  451. kfree(buffer);
  452. error_no_buffer:
  453. spin_lock_irqsave(&portdata->lock, flags);
  454. --portdata->outstanding_urbs;
  455. dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
  456. portdata->outstanding_urbs);
  457. spin_unlock_irqrestore(&portdata->lock, flags);
  458. usb_autopm_put_interface_async(serial->interface);
  459. error_simple:
  460. return retval;
  461. }
  462. static void sierra_indat_callback(struct urb *urb)
  463. {
  464. int err;
  465. int endpoint;
  466. struct usb_serial_port *port;
  467. unsigned char *data = urb->transfer_buffer;
  468. int status = urb->status;
  469. endpoint = usb_pipeendpoint(urb->pipe);
  470. port = urb->context;
  471. if (status) {
  472. dev_dbg(&port->dev, "%s: nonzero status: %d on"
  473. " endpoint %02x\n", __func__, status, endpoint);
  474. } else {
  475. if (urb->actual_length) {
  476. tty_insert_flip_string(&port->port, data,
  477. urb->actual_length);
  478. tty_flip_buffer_push(&port->port);
  479. usb_serial_debug_data(&port->dev, __func__,
  480. urb->actual_length, data);
  481. } else {
  482. dev_dbg(&port->dev, "%s: empty read urb"
  483. " received\n", __func__);
  484. }
  485. }
  486. /* Resubmit urb so we continue receiving */
  487. if (status != -ESHUTDOWN && status != -EPERM) {
  488. usb_mark_last_busy(port->serial->dev);
  489. err = usb_submit_urb(urb, GFP_ATOMIC);
  490. if (err && err != -EPERM)
  491. dev_err(&port->dev, "resubmit read urb failed."
  492. "(%d)\n", err);
  493. }
  494. }
  495. static void sierra_instat_callback(struct urb *urb)
  496. {
  497. int err;
  498. int status = urb->status;
  499. struct usb_serial_port *port = urb->context;
  500. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  501. struct usb_serial *serial = port->serial;
  502. dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
  503. urb, port, portdata);
  504. if (status == 0) {
  505. struct usb_ctrlrequest *req_pkt =
  506. (struct usb_ctrlrequest *)urb->transfer_buffer;
  507. if (!req_pkt) {
  508. dev_dbg(&port->dev, "%s: NULL req_pkt\n",
  509. __func__);
  510. return;
  511. }
  512. if ((req_pkt->bRequestType == 0xA1) &&
  513. (req_pkt->bRequest == 0x20)) {
  514. int old_dcd_state;
  515. unsigned char signals = *((unsigned char *)
  516. urb->transfer_buffer +
  517. sizeof(struct usb_ctrlrequest));
  518. dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
  519. signals);
  520. old_dcd_state = portdata->dcd_state;
  521. portdata->cts_state = 1;
  522. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  523. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  524. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  525. if (old_dcd_state && !portdata->dcd_state)
  526. tty_port_tty_hangup(&port->port, true);
  527. } else {
  528. dev_dbg(&port->dev, "%s: type %x req %x\n",
  529. __func__, req_pkt->bRequestType,
  530. req_pkt->bRequest);
  531. }
  532. } else
  533. dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
  534. /* Resubmit urb so we continue receiving IRQ data */
  535. if (status != -ESHUTDOWN && status != -ENOENT) {
  536. usb_mark_last_busy(serial->dev);
  537. err = usb_submit_urb(urb, GFP_ATOMIC);
  538. if (err && err != -EPERM)
  539. dev_err(&port->dev, "%s: resubmit intr urb "
  540. "failed. (%d)\n", __func__, err);
  541. }
  542. }
  543. static int sierra_write_room(struct tty_struct *tty)
  544. {
  545. struct usb_serial_port *port = tty->driver_data;
  546. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  547. unsigned long flags;
  548. /* try to give a good number back based on if we have any free urbs at
  549. * this point in time */
  550. spin_lock_irqsave(&portdata->lock, flags);
  551. if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
  552. spin_unlock_irqrestore(&portdata->lock, flags);
  553. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  554. return 0;
  555. }
  556. spin_unlock_irqrestore(&portdata->lock, flags);
  557. return 2048;
  558. }
  559. static int sierra_chars_in_buffer(struct tty_struct *tty)
  560. {
  561. struct usb_serial_port *port = tty->driver_data;
  562. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  563. unsigned long flags;
  564. int chars;
  565. /* NOTE: This overcounts somewhat. */
  566. spin_lock_irqsave(&portdata->lock, flags);
  567. chars = portdata->outstanding_urbs * MAX_TRANSFER;
  568. spin_unlock_irqrestore(&portdata->lock, flags);
  569. dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
  570. return chars;
  571. }
  572. static void sierra_stop_rx_urbs(struct usb_serial_port *port)
  573. {
  574. int i;
  575. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  576. for (i = 0; i < portdata->num_in_urbs; i++)
  577. usb_kill_urb(portdata->in_urbs[i]);
  578. usb_kill_urb(port->interrupt_in_urb);
  579. }
  580. static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
  581. {
  582. int ok_cnt;
  583. int err = -EINVAL;
  584. int i;
  585. struct urb *urb;
  586. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  587. ok_cnt = 0;
  588. for (i = 0; i < portdata->num_in_urbs; i++) {
  589. urb = portdata->in_urbs[i];
  590. if (!urb)
  591. continue;
  592. err = usb_submit_urb(urb, mem_flags);
  593. if (err) {
  594. dev_err(&port->dev, "%s: submit urb failed: %d\n",
  595. __func__, err);
  596. } else {
  597. ok_cnt++;
  598. }
  599. }
  600. if (ok_cnt && port->interrupt_in_urb) {
  601. err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
  602. if (err) {
  603. dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
  604. __func__, err);
  605. }
  606. }
  607. if (ok_cnt > 0) /* at least one rx urb submitted */
  608. return 0;
  609. else
  610. return err;
  611. }
  612. static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
  613. int dir, void *ctx, int len,
  614. gfp_t mem_flags,
  615. usb_complete_t callback)
  616. {
  617. struct urb *urb;
  618. u8 *buf;
  619. urb = usb_alloc_urb(0, mem_flags);
  620. if (!urb)
  621. return NULL;
  622. buf = kmalloc(len, mem_flags);
  623. if (buf) {
  624. /* Fill URB using supplied data */
  625. usb_fill_bulk_urb(urb, serial->dev,
  626. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  627. buf, len, callback, ctx);
  628. dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
  629. dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
  630. } else {
  631. sierra_release_urb(urb);
  632. urb = NULL;
  633. }
  634. return urb;
  635. }
  636. static void sierra_close(struct usb_serial_port *port)
  637. {
  638. int i;
  639. struct usb_serial *serial = port->serial;
  640. struct sierra_port_private *portdata;
  641. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  642. struct urb *urb;
  643. portdata = usb_get_serial_port_data(port);
  644. /*
  645. * Need to take susp_lock to make sure port is not already being
  646. * resumed, but no need to hold it due to initialized
  647. */
  648. spin_lock_irq(&intfdata->susp_lock);
  649. if (--intfdata->open_ports == 0)
  650. serial->interface->needs_remote_wakeup = 0;
  651. spin_unlock_irq(&intfdata->susp_lock);
  652. for (;;) {
  653. urb = usb_get_from_anchor(&portdata->delayed);
  654. if (!urb)
  655. break;
  656. kfree(urb->transfer_buffer);
  657. usb_free_urb(urb);
  658. usb_autopm_put_interface_async(serial->interface);
  659. spin_lock(&portdata->lock);
  660. portdata->outstanding_urbs--;
  661. spin_unlock(&portdata->lock);
  662. }
  663. sierra_stop_rx_urbs(port);
  664. usb_kill_anchored_urbs(&portdata->active);
  665. for (i = 0; i < portdata->num_in_urbs; i++) {
  666. sierra_release_urb(portdata->in_urbs[i]);
  667. portdata->in_urbs[i] = NULL;
  668. }
  669. usb_autopm_get_interface_no_resume(serial->interface);
  670. }
  671. static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
  672. {
  673. struct sierra_port_private *portdata;
  674. struct usb_serial *serial = port->serial;
  675. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  676. int i;
  677. int err;
  678. int endpoint;
  679. struct urb *urb;
  680. portdata = usb_get_serial_port_data(port);
  681. endpoint = port->bulk_in_endpointAddress;
  682. for (i = 0; i < portdata->num_in_urbs; i++) {
  683. urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
  684. IN_BUFLEN, GFP_KERNEL,
  685. sierra_indat_callback);
  686. portdata->in_urbs[i] = urb;
  687. }
  688. /* clear halt condition */
  689. usb_clear_halt(serial->dev,
  690. usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
  691. err = sierra_submit_rx_urbs(port, GFP_KERNEL);
  692. if (err)
  693. goto err_submit;
  694. spin_lock_irq(&intfdata->susp_lock);
  695. if (++intfdata->open_ports == 1)
  696. serial->interface->needs_remote_wakeup = 1;
  697. spin_unlock_irq(&intfdata->susp_lock);
  698. usb_autopm_put_interface(serial->interface);
  699. return 0;
  700. err_submit:
  701. sierra_stop_rx_urbs(port);
  702. for (i = 0; i < portdata->num_in_urbs; i++) {
  703. sierra_release_urb(portdata->in_urbs[i]);
  704. portdata->in_urbs[i] = NULL;
  705. }
  706. return err;
  707. }
  708. static void sierra_dtr_rts(struct usb_serial_port *port, int on)
  709. {
  710. struct sierra_port_private *portdata;
  711. portdata = usb_get_serial_port_data(port);
  712. portdata->rts_state = on;
  713. portdata->dtr_state = on;
  714. sierra_send_setup(port);
  715. }
  716. static int sierra_startup(struct usb_serial *serial)
  717. {
  718. struct sierra_intf_private *intfdata;
  719. intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
  720. if (!intfdata)
  721. return -ENOMEM;
  722. spin_lock_init(&intfdata->susp_lock);
  723. usb_set_serial_data(serial, intfdata);
  724. /* Set Device mode to D0 */
  725. sierra_set_power_state(serial->dev, 0x0000);
  726. /* Check NMEA and set */
  727. if (nmea)
  728. sierra_vsc_set_nmea(serial->dev, 1);
  729. return 0;
  730. }
  731. static void sierra_release(struct usb_serial *serial)
  732. {
  733. struct sierra_intf_private *intfdata;
  734. intfdata = usb_get_serial_data(serial);
  735. kfree(intfdata);
  736. }
  737. static int sierra_port_probe(struct usb_serial_port *port)
  738. {
  739. struct usb_serial *serial = port->serial;
  740. struct sierra_port_private *portdata;
  741. const struct sierra_iface_info *himemoryp;
  742. u8 ifnum;
  743. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  744. if (!portdata)
  745. return -ENOMEM;
  746. spin_lock_init(&portdata->lock);
  747. init_usb_anchor(&portdata->active);
  748. init_usb_anchor(&portdata->delayed);
  749. /* Assume low memory requirements */
  750. portdata->num_out_urbs = N_OUT_URB;
  751. portdata->num_in_urbs = N_IN_URB;
  752. /* Determine actual memory requirements */
  753. if (serial->num_ports == 1) {
  754. /* Get interface number for composite device */
  755. ifnum = sierra_interface_num(serial);
  756. himemoryp = &typeB_interface_list;
  757. } else {
  758. /* This is really the usb-serial port number of the interface
  759. * rather than the interface number.
  760. */
  761. ifnum = port->port_number;
  762. himemoryp = &typeA_interface_list;
  763. }
  764. if (is_himemory(ifnum, himemoryp)) {
  765. portdata->num_out_urbs = N_OUT_URB_HM;
  766. portdata->num_in_urbs = N_IN_URB_HM;
  767. }
  768. dev_dbg(&port->dev,
  769. "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
  770. ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
  771. usb_set_serial_port_data(port, portdata);
  772. return 0;
  773. }
  774. static int sierra_port_remove(struct usb_serial_port *port)
  775. {
  776. struct sierra_port_private *portdata;
  777. portdata = usb_get_serial_port_data(port);
  778. usb_set_serial_port_data(port, NULL);
  779. kfree(portdata);
  780. return 0;
  781. }
  782. #ifdef CONFIG_PM
  783. static void stop_read_write_urbs(struct usb_serial *serial)
  784. {
  785. int i;
  786. struct usb_serial_port *port;
  787. struct sierra_port_private *portdata;
  788. /* Stop reading/writing urbs */
  789. for (i = 0; i < serial->num_ports; ++i) {
  790. port = serial->port[i];
  791. portdata = usb_get_serial_port_data(port);
  792. if (!portdata)
  793. continue;
  794. sierra_stop_rx_urbs(port);
  795. usb_kill_anchored_urbs(&portdata->active);
  796. }
  797. }
  798. static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
  799. {
  800. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  801. spin_lock_irq(&intfdata->susp_lock);
  802. if (PMSG_IS_AUTO(message)) {
  803. if (intfdata->in_flight) {
  804. spin_unlock_irq(&intfdata->susp_lock);
  805. return -EBUSY;
  806. }
  807. }
  808. intfdata->suspended = 1;
  809. spin_unlock_irq(&intfdata->susp_lock);
  810. stop_read_write_urbs(serial);
  811. return 0;
  812. }
  813. /* Caller must hold susp_lock. */
  814. static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
  815. {
  816. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  817. struct sierra_intf_private *intfdata;
  818. struct urb *urb;
  819. int ec = 0;
  820. int err;
  821. intfdata = usb_get_serial_data(port->serial);
  822. for (;;) {
  823. urb = usb_get_from_anchor(&portdata->delayed);
  824. if (!urb)
  825. break;
  826. usb_anchor_urb(urb, &portdata->active);
  827. intfdata->in_flight++;
  828. err = usb_submit_urb(urb, GFP_ATOMIC);
  829. if (err) {
  830. dev_err(&port->dev, "%s - submit urb failed: %d",
  831. __func__, err);
  832. ec++;
  833. intfdata->in_flight--;
  834. usb_unanchor_urb(urb);
  835. kfree(urb->transfer_buffer);
  836. usb_free_urb(urb);
  837. spin_lock(&portdata->lock);
  838. portdata->outstanding_urbs--;
  839. spin_unlock(&portdata->lock);
  840. }
  841. }
  842. if (ec)
  843. return -EIO;
  844. return 0;
  845. }
  846. static int sierra_resume(struct usb_serial *serial)
  847. {
  848. struct usb_serial_port *port;
  849. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  850. int ec = 0;
  851. int i, err;
  852. spin_lock_irq(&intfdata->susp_lock);
  853. for (i = 0; i < serial->num_ports; i++) {
  854. port = serial->port[i];
  855. if (!tty_port_initialized(&port->port))
  856. continue;
  857. err = sierra_submit_delayed_urbs(port);
  858. if (err)
  859. ec++;
  860. err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
  861. if (err)
  862. ec++;
  863. }
  864. intfdata->suspended = 0;
  865. spin_unlock_irq(&intfdata->susp_lock);
  866. return ec ? -EIO : 0;
  867. }
  868. #else
  869. #define sierra_suspend NULL
  870. #define sierra_resume NULL
  871. #endif
  872. static struct usb_serial_driver sierra_device = {
  873. .driver = {
  874. .owner = THIS_MODULE,
  875. .name = "sierra",
  876. },
  877. .description = "Sierra USB modem",
  878. .id_table = id_table,
  879. .calc_num_ports = sierra_calc_num_ports,
  880. .probe = sierra_probe,
  881. .open = sierra_open,
  882. .close = sierra_close,
  883. .dtr_rts = sierra_dtr_rts,
  884. .write = sierra_write,
  885. .write_room = sierra_write_room,
  886. .chars_in_buffer = sierra_chars_in_buffer,
  887. .tiocmget = sierra_tiocmget,
  888. .tiocmset = sierra_tiocmset,
  889. .attach = sierra_startup,
  890. .release = sierra_release,
  891. .port_probe = sierra_port_probe,
  892. .port_remove = sierra_port_remove,
  893. .suspend = sierra_suspend,
  894. .resume = sierra_resume,
  895. .read_int_callback = sierra_instat_callback,
  896. };
  897. static struct usb_serial_driver * const serial_drivers[] = {
  898. &sierra_device, NULL
  899. };
  900. module_usb_serial_driver(serial_drivers, id_table);
  901. MODULE_AUTHOR(DRIVER_AUTHOR);
  902. MODULE_DESCRIPTION(DRIVER_DESC);
  903. MODULE_LICENSE("GPL v2");
  904. module_param(nmea, bool, S_IRUGO | S_IWUSR);
  905. MODULE_PARM_DESC(nmea, "NMEA streaming");