sierra.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. /* Sierra Wireless Direct IP modems */
  245. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
  246. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  247. },
  248. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
  249. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  250. },
  251. /* AT&T Direct IP LTE modems */
  252. { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
  253. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  254. },
  255. /* Airprime/Sierra Wireless Direct IP modems */
  256. { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
  257. .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
  258. },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(usb, id_table);
  262. struct sierra_port_private {
  263. spinlock_t lock; /* lock the structure */
  264. int outstanding_urbs; /* number of out urbs in flight */
  265. struct usb_anchor active;
  266. struct usb_anchor delayed;
  267. int num_out_urbs;
  268. int num_in_urbs;
  269. /* Input endpoints and buffers for this port */
  270. struct urb *in_urbs[N_IN_URB_HM];
  271. /* Settings for the port */
  272. int rts_state; /* Handshaking pins (outputs) */
  273. int dtr_state;
  274. int cts_state; /* Handshaking pins (inputs) */
  275. int dsr_state;
  276. int dcd_state;
  277. int ri_state;
  278. };
  279. static int sierra_send_setup(struct usb_serial_port *port)
  280. {
  281. struct usb_serial *serial = port->serial;
  282. struct sierra_port_private *portdata;
  283. __u16 interface = 0;
  284. int val = 0;
  285. int do_send = 0;
  286. int retval;
  287. portdata = usb_get_serial_port_data(port);
  288. if (portdata->dtr_state)
  289. val |= 0x01;
  290. if (portdata->rts_state)
  291. val |= 0x02;
  292. /* If composite device then properly report interface */
  293. if (serial->num_ports == 1) {
  294. interface = sierra_calc_interface(serial);
  295. /* Control message is sent only to interfaces with
  296. * interrupt_in endpoints
  297. */
  298. if (port->interrupt_in_urb) {
  299. /* send control message */
  300. do_send = 1;
  301. }
  302. }
  303. /* Otherwise the need to do non-composite mapping */
  304. else {
  305. if (port->bulk_out_endpointAddress == 2)
  306. interface = 0;
  307. else if (port->bulk_out_endpointAddress == 4)
  308. interface = 1;
  309. else if (port->bulk_out_endpointAddress == 5)
  310. interface = 2;
  311. do_send = 1;
  312. }
  313. if (!do_send)
  314. return 0;
  315. retval = usb_autopm_get_interface(serial->interface);
  316. if (retval < 0)
  317. return retval;
  318. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  319. 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
  320. usb_autopm_put_interface(serial->interface);
  321. return retval;
  322. }
  323. static int sierra_tiocmget(struct tty_struct *tty)
  324. {
  325. struct usb_serial_port *port = tty->driver_data;
  326. unsigned int value;
  327. struct sierra_port_private *portdata;
  328. portdata = usb_get_serial_port_data(port);
  329. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  330. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  331. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  332. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  333. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  334. ((portdata->ri_state) ? TIOCM_RNG : 0);
  335. return value;
  336. }
  337. static int sierra_tiocmset(struct tty_struct *tty,
  338. unsigned int set, unsigned int clear)
  339. {
  340. struct usb_serial_port *port = tty->driver_data;
  341. struct sierra_port_private *portdata;
  342. portdata = usb_get_serial_port_data(port);
  343. if (set & TIOCM_RTS)
  344. portdata->rts_state = 1;
  345. if (set & TIOCM_DTR)
  346. portdata->dtr_state = 1;
  347. if (clear & TIOCM_RTS)
  348. portdata->rts_state = 0;
  349. if (clear & TIOCM_DTR)
  350. portdata->dtr_state = 0;
  351. return sierra_send_setup(port);
  352. }
  353. static void sierra_release_urb(struct urb *urb)
  354. {
  355. if (urb) {
  356. kfree(urb->transfer_buffer);
  357. usb_free_urb(urb);
  358. }
  359. }
  360. static void sierra_outdat_callback(struct urb *urb)
  361. {
  362. struct usb_serial_port *port = urb->context;
  363. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  364. struct sierra_intf_private *intfdata;
  365. int status = urb->status;
  366. intfdata = usb_get_serial_data(port->serial);
  367. /* free up the transfer buffer, as usb_free_urb() does not do this */
  368. kfree(urb->transfer_buffer);
  369. usb_autopm_put_interface_async(port->serial->interface);
  370. if (status)
  371. dev_dbg(&port->dev, "%s - nonzero write bulk status "
  372. "received: %d\n", __func__, status);
  373. spin_lock(&portdata->lock);
  374. --portdata->outstanding_urbs;
  375. spin_unlock(&portdata->lock);
  376. spin_lock(&intfdata->susp_lock);
  377. --intfdata->in_flight;
  378. spin_unlock(&intfdata->susp_lock);
  379. usb_serial_port_softint(port);
  380. }
  381. /* Write */
  382. static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
  383. const unsigned char *buf, int count)
  384. {
  385. struct sierra_port_private *portdata;
  386. struct sierra_intf_private *intfdata;
  387. struct usb_serial *serial = port->serial;
  388. unsigned long flags;
  389. unsigned char *buffer;
  390. struct urb *urb;
  391. size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
  392. int retval = 0;
  393. /* verify that we actually have some data to write */
  394. if (count == 0)
  395. return 0;
  396. portdata = usb_get_serial_port_data(port);
  397. intfdata = usb_get_serial_data(serial);
  398. dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
  399. spin_lock_irqsave(&portdata->lock, flags);
  400. dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
  401. portdata->outstanding_urbs);
  402. if (portdata->outstanding_urbs > portdata->num_out_urbs) {
  403. spin_unlock_irqrestore(&portdata->lock, flags);
  404. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  405. return 0;
  406. }
  407. portdata->outstanding_urbs++;
  408. dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
  409. portdata->outstanding_urbs);
  410. spin_unlock_irqrestore(&portdata->lock, flags);
  411. retval = usb_autopm_get_interface_async(serial->interface);
  412. if (retval < 0) {
  413. spin_lock_irqsave(&portdata->lock, flags);
  414. portdata->outstanding_urbs--;
  415. spin_unlock_irqrestore(&portdata->lock, flags);
  416. goto error_simple;
  417. }
  418. buffer = kmalloc(writesize, GFP_ATOMIC);
  419. if (!buffer) {
  420. retval = -ENOMEM;
  421. goto error_no_buffer;
  422. }
  423. urb = usb_alloc_urb(0, GFP_ATOMIC);
  424. if (!urb) {
  425. retval = -ENOMEM;
  426. goto error_no_urb;
  427. }
  428. memcpy(buffer, buf, writesize);
  429. usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
  430. usb_fill_bulk_urb(urb, serial->dev,
  431. usb_sndbulkpipe(serial->dev,
  432. port->bulk_out_endpointAddress),
  433. buffer, writesize, sierra_outdat_callback, port);
  434. /* Handle the need to send a zero length packet */
  435. urb->transfer_flags |= URB_ZERO_PACKET;
  436. spin_lock_irqsave(&intfdata->susp_lock, flags);
  437. if (intfdata->suspended) {
  438. usb_anchor_urb(urb, &portdata->delayed);
  439. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  440. goto skip_power;
  441. } else {
  442. usb_anchor_urb(urb, &portdata->active);
  443. }
  444. /* send it down the pipe */
  445. retval = usb_submit_urb(urb, GFP_ATOMIC);
  446. if (retval) {
  447. usb_unanchor_urb(urb);
  448. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  449. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  450. "with status = %d\n", __func__, retval);
  451. goto error;
  452. } else {
  453. intfdata->in_flight++;
  454. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  455. }
  456. skip_power:
  457. /* we are done with this urb, so let the host driver
  458. * really free it when it is finished with it */
  459. usb_free_urb(urb);
  460. return writesize;
  461. error:
  462. usb_free_urb(urb);
  463. error_no_urb:
  464. kfree(buffer);
  465. error_no_buffer:
  466. spin_lock_irqsave(&portdata->lock, flags);
  467. --portdata->outstanding_urbs;
  468. dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
  469. portdata->outstanding_urbs);
  470. spin_unlock_irqrestore(&portdata->lock, flags);
  471. usb_autopm_put_interface_async(serial->interface);
  472. error_simple:
  473. return retval;
  474. }
  475. static void sierra_indat_callback(struct urb *urb)
  476. {
  477. int err;
  478. int endpoint;
  479. struct usb_serial_port *port;
  480. unsigned char *data = urb->transfer_buffer;
  481. int status = urb->status;
  482. endpoint = usb_pipeendpoint(urb->pipe);
  483. port = urb->context;
  484. if (status) {
  485. dev_dbg(&port->dev, "%s: nonzero status: %d on"
  486. " endpoint %02x\n", __func__, status, endpoint);
  487. } else {
  488. if (urb->actual_length) {
  489. tty_insert_flip_string(&port->port, data,
  490. urb->actual_length);
  491. tty_flip_buffer_push(&port->port);
  492. usb_serial_debug_data(&port->dev, __func__,
  493. urb->actual_length, data);
  494. } else {
  495. dev_dbg(&port->dev, "%s: empty read urb"
  496. " received\n", __func__);
  497. }
  498. }
  499. /* Resubmit urb so we continue receiving */
  500. if (status != -ESHUTDOWN && status != -EPERM) {
  501. usb_mark_last_busy(port->serial->dev);
  502. err = usb_submit_urb(urb, GFP_ATOMIC);
  503. if (err && err != -EPERM)
  504. dev_err(&port->dev, "resubmit read urb failed."
  505. "(%d)\n", err);
  506. }
  507. }
  508. static void sierra_instat_callback(struct urb *urb)
  509. {
  510. int err;
  511. int status = urb->status;
  512. struct usb_serial_port *port = urb->context;
  513. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  514. struct usb_serial *serial = port->serial;
  515. dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
  516. urb, port, portdata);
  517. if (status == 0) {
  518. struct usb_ctrlrequest *req_pkt =
  519. (struct usb_ctrlrequest *)urb->transfer_buffer;
  520. if (!req_pkt) {
  521. dev_dbg(&port->dev, "%s: NULL req_pkt\n",
  522. __func__);
  523. return;
  524. }
  525. if ((req_pkt->bRequestType == 0xA1) &&
  526. (req_pkt->bRequest == 0x20)) {
  527. int old_dcd_state;
  528. unsigned char signals = *((unsigned char *)
  529. urb->transfer_buffer +
  530. sizeof(struct usb_ctrlrequest));
  531. dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
  532. signals);
  533. old_dcd_state = portdata->dcd_state;
  534. portdata->cts_state = 1;
  535. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  536. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  537. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  538. if (old_dcd_state && !portdata->dcd_state)
  539. tty_port_tty_hangup(&port->port, true);
  540. } else {
  541. dev_dbg(&port->dev, "%s: type %x req %x\n",
  542. __func__, req_pkt->bRequestType,
  543. req_pkt->bRequest);
  544. }
  545. } else
  546. dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
  547. /* Resubmit urb so we continue receiving IRQ data */
  548. if (status != -ESHUTDOWN && status != -ENOENT) {
  549. usb_mark_last_busy(serial->dev);
  550. err = usb_submit_urb(urb, GFP_ATOMIC);
  551. if (err && err != -EPERM)
  552. dev_err(&port->dev, "%s: resubmit intr urb "
  553. "failed. (%d)\n", __func__, err);
  554. }
  555. }
  556. static int sierra_write_room(struct tty_struct *tty)
  557. {
  558. struct usb_serial_port *port = tty->driver_data;
  559. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  560. unsigned long flags;
  561. /* try to give a good number back based on if we have any free urbs at
  562. * this point in time */
  563. spin_lock_irqsave(&portdata->lock, flags);
  564. if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
  565. spin_unlock_irqrestore(&portdata->lock, flags);
  566. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  567. return 0;
  568. }
  569. spin_unlock_irqrestore(&portdata->lock, flags);
  570. return 2048;
  571. }
  572. static int sierra_chars_in_buffer(struct tty_struct *tty)
  573. {
  574. struct usb_serial_port *port = tty->driver_data;
  575. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  576. unsigned long flags;
  577. int chars;
  578. /* NOTE: This overcounts somewhat. */
  579. spin_lock_irqsave(&portdata->lock, flags);
  580. chars = portdata->outstanding_urbs * MAX_TRANSFER;
  581. spin_unlock_irqrestore(&portdata->lock, flags);
  582. dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
  583. return chars;
  584. }
  585. static void sierra_stop_rx_urbs(struct usb_serial_port *port)
  586. {
  587. int i;
  588. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  589. for (i = 0; i < portdata->num_in_urbs; i++)
  590. usb_kill_urb(portdata->in_urbs[i]);
  591. usb_kill_urb(port->interrupt_in_urb);
  592. }
  593. static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
  594. {
  595. int ok_cnt;
  596. int err = -EINVAL;
  597. int i;
  598. struct urb *urb;
  599. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  600. ok_cnt = 0;
  601. for (i = 0; i < portdata->num_in_urbs; i++) {
  602. urb = portdata->in_urbs[i];
  603. if (!urb)
  604. continue;
  605. err = usb_submit_urb(urb, mem_flags);
  606. if (err) {
  607. dev_err(&port->dev, "%s: submit urb failed: %d\n",
  608. __func__, err);
  609. } else {
  610. ok_cnt++;
  611. }
  612. }
  613. if (ok_cnt && port->interrupt_in_urb) {
  614. err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
  615. if (err) {
  616. dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
  617. __func__, err);
  618. }
  619. }
  620. if (ok_cnt > 0) /* at least one rx urb submitted */
  621. return 0;
  622. else
  623. return err;
  624. }
  625. static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
  626. int dir, void *ctx, int len,
  627. gfp_t mem_flags,
  628. usb_complete_t callback)
  629. {
  630. struct urb *urb;
  631. u8 *buf;
  632. urb = usb_alloc_urb(0, mem_flags);
  633. if (!urb)
  634. return NULL;
  635. buf = kmalloc(len, mem_flags);
  636. if (buf) {
  637. /* Fill URB using supplied data */
  638. usb_fill_bulk_urb(urb, serial->dev,
  639. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  640. buf, len, callback, ctx);
  641. dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
  642. dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
  643. } else {
  644. sierra_release_urb(urb);
  645. urb = NULL;
  646. }
  647. return urb;
  648. }
  649. static void sierra_close(struct usb_serial_port *port)
  650. {
  651. int i;
  652. struct usb_serial *serial = port->serial;
  653. struct sierra_port_private *portdata;
  654. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  655. struct urb *urb;
  656. portdata = usb_get_serial_port_data(port);
  657. /*
  658. * Need to take susp_lock to make sure port is not already being
  659. * resumed, but no need to hold it due to ASYNC_INITIALIZED.
  660. */
  661. spin_lock_irq(&intfdata->susp_lock);
  662. if (--intfdata->open_ports == 0)
  663. serial->interface->needs_remote_wakeup = 0;
  664. spin_unlock_irq(&intfdata->susp_lock);
  665. for (;;) {
  666. urb = usb_get_from_anchor(&portdata->delayed);
  667. if (!urb)
  668. break;
  669. kfree(urb->transfer_buffer);
  670. usb_free_urb(urb);
  671. usb_autopm_put_interface_async(serial->interface);
  672. spin_lock(&portdata->lock);
  673. portdata->outstanding_urbs--;
  674. spin_unlock(&portdata->lock);
  675. }
  676. sierra_stop_rx_urbs(port);
  677. usb_kill_anchored_urbs(&portdata->active);
  678. for (i = 0; i < portdata->num_in_urbs; i++) {
  679. sierra_release_urb(portdata->in_urbs[i]);
  680. portdata->in_urbs[i] = NULL;
  681. }
  682. usb_autopm_get_interface_no_resume(serial->interface);
  683. }
  684. static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
  685. {
  686. struct sierra_port_private *portdata;
  687. struct usb_serial *serial = port->serial;
  688. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  689. int i;
  690. int err;
  691. int endpoint;
  692. struct urb *urb;
  693. portdata = usb_get_serial_port_data(port);
  694. endpoint = port->bulk_in_endpointAddress;
  695. for (i = 0; i < portdata->num_in_urbs; i++) {
  696. urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
  697. IN_BUFLEN, GFP_KERNEL,
  698. sierra_indat_callback);
  699. portdata->in_urbs[i] = urb;
  700. }
  701. /* clear halt condition */
  702. usb_clear_halt(serial->dev,
  703. usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
  704. err = sierra_submit_rx_urbs(port, GFP_KERNEL);
  705. if (err)
  706. goto err_submit;
  707. spin_lock_irq(&intfdata->susp_lock);
  708. if (++intfdata->open_ports == 1)
  709. serial->interface->needs_remote_wakeup = 1;
  710. spin_unlock_irq(&intfdata->susp_lock);
  711. usb_autopm_put_interface(serial->interface);
  712. return 0;
  713. err_submit:
  714. sierra_stop_rx_urbs(port);
  715. for (i = 0; i < portdata->num_in_urbs; i++) {
  716. sierra_release_urb(portdata->in_urbs[i]);
  717. portdata->in_urbs[i] = NULL;
  718. }
  719. return err;
  720. }
  721. static void sierra_dtr_rts(struct usb_serial_port *port, int on)
  722. {
  723. struct sierra_port_private *portdata;
  724. portdata = usb_get_serial_port_data(port);
  725. portdata->rts_state = on;
  726. portdata->dtr_state = on;
  727. sierra_send_setup(port);
  728. }
  729. static int sierra_startup(struct usb_serial *serial)
  730. {
  731. struct sierra_intf_private *intfdata;
  732. intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
  733. if (!intfdata)
  734. return -ENOMEM;
  735. spin_lock_init(&intfdata->susp_lock);
  736. usb_set_serial_data(serial, intfdata);
  737. /* Set Device mode to D0 */
  738. sierra_set_power_state(serial->dev, 0x0000);
  739. /* Check NMEA and set */
  740. if (nmea)
  741. sierra_vsc_set_nmea(serial->dev, 1);
  742. return 0;
  743. }
  744. static void sierra_release(struct usb_serial *serial)
  745. {
  746. struct sierra_intf_private *intfdata;
  747. intfdata = usb_get_serial_data(serial);
  748. kfree(intfdata);
  749. }
  750. static int sierra_port_probe(struct usb_serial_port *port)
  751. {
  752. struct usb_serial *serial = port->serial;
  753. struct sierra_port_private *portdata;
  754. const struct sierra_iface_info *himemoryp;
  755. u8 ifnum;
  756. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  757. if (!portdata)
  758. return -ENOMEM;
  759. spin_lock_init(&portdata->lock);
  760. init_usb_anchor(&portdata->active);
  761. init_usb_anchor(&portdata->delayed);
  762. /* Assume low memory requirements */
  763. portdata->num_out_urbs = N_OUT_URB;
  764. portdata->num_in_urbs = N_IN_URB;
  765. /* Determine actual memory requirements */
  766. if (serial->num_ports == 1) {
  767. /* Get interface number for composite device */
  768. ifnum = sierra_calc_interface(serial);
  769. himemoryp = &typeB_interface_list;
  770. } else {
  771. /* This is really the usb-serial port number of the interface
  772. * rather than the interface number.
  773. */
  774. ifnum = port->port_number;
  775. himemoryp = &typeA_interface_list;
  776. }
  777. if (is_himemory(ifnum, himemoryp)) {
  778. portdata->num_out_urbs = N_OUT_URB_HM;
  779. portdata->num_in_urbs = N_IN_URB_HM;
  780. }
  781. dev_dbg(&port->dev,
  782. "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
  783. ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
  784. usb_set_serial_port_data(port, portdata);
  785. return 0;
  786. }
  787. static int sierra_port_remove(struct usb_serial_port *port)
  788. {
  789. struct sierra_port_private *portdata;
  790. portdata = usb_get_serial_port_data(port);
  791. usb_set_serial_port_data(port, NULL);
  792. kfree(portdata);
  793. return 0;
  794. }
  795. #ifdef CONFIG_PM
  796. static void stop_read_write_urbs(struct usb_serial *serial)
  797. {
  798. int i;
  799. struct usb_serial_port *port;
  800. struct sierra_port_private *portdata;
  801. /* Stop reading/writing urbs */
  802. for (i = 0; i < serial->num_ports; ++i) {
  803. port = serial->port[i];
  804. portdata = usb_get_serial_port_data(port);
  805. if (!portdata)
  806. continue;
  807. sierra_stop_rx_urbs(port);
  808. usb_kill_anchored_urbs(&portdata->active);
  809. }
  810. }
  811. static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
  812. {
  813. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  814. spin_lock_irq(&intfdata->susp_lock);
  815. if (PMSG_IS_AUTO(message)) {
  816. if (intfdata->in_flight) {
  817. spin_unlock_irq(&intfdata->susp_lock);
  818. return -EBUSY;
  819. }
  820. }
  821. intfdata->suspended = 1;
  822. spin_unlock_irq(&intfdata->susp_lock);
  823. stop_read_write_urbs(serial);
  824. return 0;
  825. }
  826. /* Caller must hold susp_lock. */
  827. static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
  828. {
  829. struct sierra_port_private *portdata = usb_get_serial_port_data(port);
  830. struct sierra_intf_private *intfdata;
  831. struct urb *urb;
  832. int ec = 0;
  833. int err;
  834. intfdata = usb_get_serial_data(port->serial);
  835. for (;;) {
  836. urb = usb_get_from_anchor(&portdata->delayed);
  837. if (!urb)
  838. break;
  839. usb_anchor_urb(urb, &portdata->active);
  840. intfdata->in_flight++;
  841. err = usb_submit_urb(urb, GFP_ATOMIC);
  842. if (err) {
  843. dev_err(&port->dev, "%s - submit urb failed: %d",
  844. __func__, err);
  845. ec++;
  846. intfdata->in_flight--;
  847. usb_unanchor_urb(urb);
  848. kfree(urb->transfer_buffer);
  849. usb_free_urb(urb);
  850. spin_lock(&portdata->lock);
  851. portdata->outstanding_urbs--;
  852. spin_unlock(&portdata->lock);
  853. }
  854. }
  855. if (ec)
  856. return -EIO;
  857. return 0;
  858. }
  859. static int sierra_resume(struct usb_serial *serial)
  860. {
  861. struct usb_serial_port *port;
  862. struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
  863. int ec = 0;
  864. int i, err;
  865. spin_lock_irq(&intfdata->susp_lock);
  866. for (i = 0; i < serial->num_ports; i++) {
  867. port = serial->port[i];
  868. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  869. continue;
  870. err = sierra_submit_delayed_urbs(port);
  871. if (err)
  872. ec++;
  873. err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
  874. if (err)
  875. ec++;
  876. }
  877. intfdata->suspended = 0;
  878. spin_unlock_irq(&intfdata->susp_lock);
  879. return ec ? -EIO : 0;
  880. }
  881. #else
  882. #define sierra_suspend NULL
  883. #define sierra_resume NULL
  884. #endif
  885. static struct usb_serial_driver sierra_device = {
  886. .driver = {
  887. .owner = THIS_MODULE,
  888. .name = "sierra",
  889. },
  890. .description = "Sierra USB modem",
  891. .id_table = id_table,
  892. .calc_num_ports = sierra_calc_num_ports,
  893. .probe = sierra_probe,
  894. .open = sierra_open,
  895. .close = sierra_close,
  896. .dtr_rts = sierra_dtr_rts,
  897. .write = sierra_write,
  898. .write_room = sierra_write_room,
  899. .chars_in_buffer = sierra_chars_in_buffer,
  900. .tiocmget = sierra_tiocmget,
  901. .tiocmset = sierra_tiocmset,
  902. .attach = sierra_startup,
  903. .release = sierra_release,
  904. .port_probe = sierra_port_probe,
  905. .port_remove = sierra_port_remove,
  906. .suspend = sierra_suspend,
  907. .resume = sierra_resume,
  908. .read_int_callback = sierra_instat_callback,
  909. };
  910. static struct usb_serial_driver * const serial_drivers[] = {
  911. &sierra_device, NULL
  912. };
  913. module_usb_serial_driver(serial_drivers, id_table);
  914. MODULE_AUTHOR(DRIVER_AUTHOR);
  915. MODULE_DESCRIPTION(DRIVER_DESC);
  916. MODULE_LICENSE("GPL");
  917. module_param(nmea, bool, S_IRUGO | S_IWUSR);
  918. MODULE_PARM_DESC(nmea, "NMEA streaming");