sierra.c 30 KB

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