sierra.c 31 KB

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