sierra.c 25 KB

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