sierra.c 32 KB

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