sierra.c 32 KB

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