sierra.c 24 KB

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