sierra.c 29 KB

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