sierra.c 32 KB

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