sierra.c 30 KB

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