sierra.c 30 KB

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