sierra.c 32 KB

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