sierra.c 32 KB

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