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