sierra.c 31 KB

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