sierra.c 29 KB

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