option.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. USB Driver for GSM modems
  3. Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
  4. This driver is free software; you can redistribute it and/or modify
  5. it under the terms of Version 2 of the GNU General Public License as
  6. published by the Free Software Foundation.
  7. Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
  8. History:
  9. 2005-05-19 v0.1 Initial version, based on incomplete docs
  10. and analysis of misbehavior with the standard driver
  11. 2005-05-20 v0.2 Extended the input buffer to avoid losing
  12. random 64-byte chunks of data
  13. 2005-05-21 v0.3 implemented chars_in_buffer()
  14. turned on low_latency
  15. simplified the code somewhat
  16. 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load
  17. removed some dead code
  18. added sponsor notice
  19. coding style clean-up
  20. 2005-06-20 v0.4.1 add missing braces :-/
  21. killed end-of-line whitespace
  22. 2005-07-15 v0.4.2 rename WLAN product to FUSION, add FUSION2
  23. 2005-09-10 v0.4.3 added HUAWEI E600 card and Audiovox AirCard
  24. 2005-09-20 v0.4.4 increased recv buffer size: the card sometimes
  25. wants to send >2000 bytes.
  26. 2006-04-10 v0.5 fixed two array overrun errors :-/
  27. 2006-04-21 v0.5.1 added support for Sierra Wireless MC8755
  28. 2006-05-15 v0.6 re-enable multi-port support
  29. 2006-06-01 v0.6.1 add COBRA
  30. 2006-06-01 v0.6.2 add backwards-compatibility stuff
  31. 2006-06-01 v0.6.3 add Novatel Wireless
  32. 2006-06-01 v0.7 Option => GSM
  33. Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
  34. This driver exists because the "normal" serial driver doesn't work too well
  35. with GSM modems. Issues:
  36. - data loss -- one single Receive URB is not nearly enough
  37. - nonstandard flow (Option devices) and multiplex (Sierra) control
  38. - controlling the baud rate doesn't make sense
  39. This driver is named "option" because the most common device it's
  40. used for is a PC-Card (with an internal OHCI-USB interface, behind
  41. which the GSM interface sits), made by Option Inc.
  42. Some of the "one port" devices actually exhibit multiple USB instances
  43. on the USB bus. This is not a bug, these ports are used for different
  44. device features.
  45. */
  46. #define DRIVER_VERSION "v0.7.0"
  47. #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
  48. #define DRIVER_DESC "USB Driver for GSM modems"
  49. #include <linux/kernel.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/errno.h>
  52. #include <linux/tty.h>
  53. #include <linux/tty_flip.h>
  54. #include <linux/module.h>
  55. #include <linux/usb.h>
  56. #include "usb-serial.h"
  57. /* Function prototypes */
  58. static int option_open(struct usb_serial_port *port, struct file *filp);
  59. static void option_close(struct usb_serial_port *port, struct file *filp);
  60. static int option_startup(struct usb_serial *serial);
  61. static void option_shutdown(struct usb_serial *serial);
  62. static void option_rx_throttle(struct usb_serial_port *port);
  63. static void option_rx_unthrottle(struct usb_serial_port *port);
  64. static int option_write_room(struct usb_serial_port *port);
  65. static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
  66. static int option_write(struct usb_serial_port *port,
  67. const unsigned char *buf, int count);
  68. static int option_chars_in_buffer(struct usb_serial_port *port);
  69. static int option_ioctl(struct usb_serial_port *port, struct file *file,
  70. unsigned int cmd, unsigned long arg);
  71. static void option_set_termios(struct usb_serial_port *port,
  72. struct termios *old);
  73. static void option_break_ctl(struct usb_serial_port *port, int break_state);
  74. static int option_tiocmget(struct usb_serial_port *port, struct file *file);
  75. static int option_tiocmset(struct usb_serial_port *port, struct file *file,
  76. unsigned int set, unsigned int clear);
  77. static int option_send_setup(struct usb_serial_port *port);
  78. /* Vendor and product IDs */
  79. #define OPTION_VENDOR_ID 0x0AF0
  80. #define HUAWEI_VENDOR_ID 0x12D1
  81. #define AUDIOVOX_VENDOR_ID 0x0F3D
  82. #define SIERRAWIRELESS_VENDOR_ID 0x1199
  83. #define NOVATELWIRELESS_VENDOR_ID 0x1410
  84. #define OPTION_PRODUCT_OLD 0x5000
  85. #define OPTION_PRODUCT_FUSION 0x6000
  86. #define OPTION_PRODUCT_FUSION2 0x6300
  87. #define OPTION_PRODUCT_COBRA 0x6500
  88. #define HUAWEI_PRODUCT_E600 0x1001
  89. #define AUDIOVOX_PRODUCT_AIRCARD 0x0112
  90. #define SIERRAWIRELESS_PRODUCT_MC8755 0x6802
  91. #define NOVATELWIRELESS_PRODUCT_U740 0x1400
  92. static struct usb_device_id option_ids[] = {
  93. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
  94. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
  95. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
  96. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
  97. { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
  98. { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
  99. { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
  100. { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
  101. { } /* Terminating entry */
  102. };
  103. static struct usb_device_id option_ids1[] = {
  104. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
  105. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
  106. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
  107. { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
  108. { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
  109. { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
  110. { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
  111. { } /* Terminating entry */
  112. };
  113. static struct usb_device_id option_ids3[] = {
  114. { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
  115. { } /* Terminating entry */
  116. };
  117. MODULE_DEVICE_TABLE(usb, option_ids);
  118. static struct usb_driver option_driver = {
  119. .name = "option",
  120. .probe = usb_serial_probe,
  121. .disconnect = usb_serial_disconnect,
  122. .id_table = option_ids,
  123. .no_dynamic_id = 1,
  124. };
  125. /* The card has three separate interfaces, which the serial driver
  126. * recognizes separately, thus num_port=1.
  127. */
  128. static struct usb_serial_driver option_3port_device = {
  129. .driver = {
  130. .owner = THIS_MODULE,
  131. .name = "option",
  132. },
  133. .description = "GSM modem (3-port)",
  134. .id_table = option_ids3,
  135. .num_interrupt_in = NUM_DONT_CARE,
  136. .num_bulk_in = NUM_DONT_CARE,
  137. .num_bulk_out = NUM_DONT_CARE,
  138. .num_ports = 3,
  139. .open = option_open,
  140. .close = option_close,
  141. .write = option_write,
  142. .write_room = option_write_room,
  143. .chars_in_buffer = option_chars_in_buffer,
  144. .throttle = option_rx_throttle,
  145. .unthrottle = option_rx_unthrottle,
  146. .set_termios = option_set_termios,
  147. .break_ctl = option_break_ctl,
  148. .tiocmget = option_tiocmget,
  149. .tiocmset = option_tiocmset,
  150. .attach = option_startup,
  151. .shutdown = option_shutdown,
  152. .read_int_callback = option_instat_callback,
  153. };
  154. static struct usb_serial_driver option_1port_device = {
  155. .driver = {
  156. .owner = THIS_MODULE,
  157. .name = "option",
  158. },
  159. .description = "GSM modem (1-port)",
  160. .id_table = option_ids1,
  161. .num_interrupt_in = NUM_DONT_CARE,
  162. .num_bulk_in = NUM_DONT_CARE,
  163. .num_bulk_out = NUM_DONT_CARE,
  164. .num_ports = 1,
  165. .open = option_open,
  166. .close = option_close,
  167. .write = option_write,
  168. .write_room = option_write_room,
  169. .chars_in_buffer = option_chars_in_buffer,
  170. .throttle = option_rx_throttle,
  171. .unthrottle = option_rx_unthrottle,
  172. .ioctl = option_ioctl,
  173. .set_termios = option_set_termios,
  174. .break_ctl = option_break_ctl,
  175. .tiocmget = option_tiocmget,
  176. .tiocmset = option_tiocmset,
  177. .attach = option_startup,
  178. .shutdown = option_shutdown,
  179. .read_int_callback = option_instat_callback,
  180. };
  181. #ifdef CONFIG_USB_DEBUG
  182. static int debug;
  183. #else
  184. #define debug 0
  185. #endif
  186. /* per port private data */
  187. #define N_IN_URB 4
  188. #define N_OUT_URB 1
  189. #define IN_BUFLEN 4096
  190. #define OUT_BUFLEN 128
  191. struct option_port_private {
  192. /* Input endpoints and buffer for this port */
  193. struct urb *in_urbs[N_IN_URB];
  194. char in_buffer[N_IN_URB][IN_BUFLEN];
  195. /* Output endpoints and buffer for this port */
  196. struct urb *out_urbs[N_OUT_URB];
  197. char out_buffer[N_OUT_URB][OUT_BUFLEN];
  198. /* Settings for the port */
  199. int rts_state; /* Handshaking pins (outputs) */
  200. int dtr_state;
  201. int cts_state; /* Handshaking pins (inputs) */
  202. int dsr_state;
  203. int dcd_state;
  204. int ri_state;
  205. unsigned long tx_start_time[N_OUT_URB];
  206. };
  207. /* Functions used by new usb-serial code. */
  208. static int __init option_init(void)
  209. {
  210. int retval;
  211. retval = usb_serial_register(&option_1port_device);
  212. if (retval)
  213. goto failed_1port_device_register;
  214. retval = usb_serial_register(&option_3port_device);
  215. if (retval)
  216. goto failed_3port_device_register;
  217. retval = usb_register(&option_driver);
  218. if (retval)
  219. goto failed_driver_register;
  220. info(DRIVER_DESC ": " DRIVER_VERSION);
  221. return 0;
  222. failed_driver_register:
  223. usb_serial_deregister (&option_3port_device);
  224. failed_3port_device_register:
  225. usb_serial_deregister (&option_1port_device);
  226. failed_1port_device_register:
  227. return retval;
  228. }
  229. static void __exit option_exit(void)
  230. {
  231. usb_deregister (&option_driver);
  232. usb_serial_deregister (&option_3port_device);
  233. usb_serial_deregister (&option_1port_device);
  234. }
  235. module_init(option_init);
  236. module_exit(option_exit);
  237. static void option_rx_throttle(struct usb_serial_port *port)
  238. {
  239. dbg("%s", __FUNCTION__);
  240. }
  241. static void option_rx_unthrottle(struct usb_serial_port *port)
  242. {
  243. dbg("%s", __FUNCTION__);
  244. }
  245. static void option_break_ctl(struct usb_serial_port *port, int break_state)
  246. {
  247. /* Unfortunately, I don't know how to send a break */
  248. dbg("%s", __FUNCTION__);
  249. }
  250. static void option_set_termios(struct usb_serial_port *port,
  251. struct termios *old_termios)
  252. {
  253. dbg("%s", __FUNCTION__);
  254. option_send_setup(port);
  255. }
  256. static int option_tiocmget(struct usb_serial_port *port, struct file *file)
  257. {
  258. unsigned int value;
  259. struct option_port_private *portdata;
  260. portdata = usb_get_serial_port_data(port);
  261. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  262. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  263. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  264. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  265. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  266. ((portdata->ri_state) ? TIOCM_RNG : 0);
  267. return value;
  268. }
  269. static int option_tiocmset(struct usb_serial_port *port, struct file *file,
  270. unsigned int set, unsigned int clear)
  271. {
  272. struct option_port_private *portdata;
  273. portdata = usb_get_serial_port_data(port);
  274. if (set & TIOCM_RTS)
  275. portdata->rts_state = 1;
  276. if (set & TIOCM_DTR)
  277. portdata->dtr_state = 1;
  278. if (clear & TIOCM_RTS)
  279. portdata->rts_state = 0;
  280. if (clear & TIOCM_DTR)
  281. portdata->dtr_state = 0;
  282. return option_send_setup(port);
  283. }
  284. static int option_ioctl(struct usb_serial_port *port, struct file *file,
  285. unsigned int cmd, unsigned long arg)
  286. {
  287. return -ENOIOCTLCMD;
  288. }
  289. /* Write */
  290. static int option_write(struct usb_serial_port *port,
  291. const unsigned char *buf, int count)
  292. {
  293. struct option_port_private *portdata;
  294. int i;
  295. int left, todo;
  296. struct urb *this_urb = NULL; /* spurious */
  297. int err;
  298. portdata = usb_get_serial_port_data(port);
  299. dbg("%s: write (%d chars)", __FUNCTION__, count);
  300. i = 0;
  301. left = count;
  302. for (i=0; left > 0 && i < N_OUT_URB; i++) {
  303. todo = left;
  304. if (todo > OUT_BUFLEN)
  305. todo = OUT_BUFLEN;
  306. this_urb = portdata->out_urbs[i];
  307. if (this_urb->status == -EINPROGRESS) {
  308. if (time_before(jiffies,
  309. portdata->tx_start_time[i] + 10 * HZ))
  310. continue;
  311. usb_unlink_urb(this_urb);
  312. continue;
  313. }
  314. if (this_urb->status != 0)
  315. dbg("usb_write %p failed (err=%d)",
  316. this_urb, this_urb->status);
  317. dbg("%s: endpoint %d buf %d", __FUNCTION__,
  318. usb_pipeendpoint(this_urb->pipe), i);
  319. /* send the data */
  320. memcpy (this_urb->transfer_buffer, buf, todo);
  321. this_urb->transfer_buffer_length = todo;
  322. this_urb->dev = port->serial->dev;
  323. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  324. if (err) {
  325. dbg("usb_submit_urb %p (write bulk) failed "
  326. "(%d, has %d)", this_urb,
  327. err, this_urb->status);
  328. continue;
  329. }
  330. portdata->tx_start_time[i] = jiffies;
  331. buf += todo;
  332. left -= todo;
  333. }
  334. count -= left;
  335. dbg("%s: wrote (did %d)", __FUNCTION__, count);
  336. return count;
  337. }
  338. static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
  339. {
  340. int err;
  341. int endpoint;
  342. struct usb_serial_port *port;
  343. struct tty_struct *tty;
  344. unsigned char *data = urb->transfer_buffer;
  345. dbg("%s: %p", __FUNCTION__, urb);
  346. endpoint = usb_pipeendpoint(urb->pipe);
  347. port = (struct usb_serial_port *) urb->context;
  348. if (urb->status) {
  349. dbg("%s: nonzero status: %d on endpoint %02x.",
  350. __FUNCTION__, urb->status, endpoint);
  351. } else {
  352. tty = port->tty;
  353. if (urb->actual_length) {
  354. tty_buffer_request_room(tty, urb->actual_length);
  355. tty_insert_flip_string(tty, data, urb->actual_length);
  356. tty_flip_buffer_push(tty);
  357. } else {
  358. dbg("%s: empty read urb received", __FUNCTION__);
  359. }
  360. /* Resubmit urb so we continue receiving */
  361. if (port->open_count && urb->status != -ESHUTDOWN) {
  362. err = usb_submit_urb(urb, GFP_ATOMIC);
  363. if (err)
  364. printk(KERN_ERR "%s: resubmit read urb failed. "
  365. "(%d)", __FUNCTION__, err);
  366. }
  367. }
  368. return;
  369. }
  370. static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
  371. {
  372. struct usb_serial_port *port;
  373. dbg("%s", __FUNCTION__);
  374. port = (struct usb_serial_port *) urb->context;
  375. usb_serial_port_softint(port);
  376. }
  377. static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
  378. {
  379. int err;
  380. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  381. struct option_port_private *portdata = usb_get_serial_port_data(port);
  382. struct usb_serial *serial = port->serial;
  383. dbg("%s", __FUNCTION__);
  384. dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
  385. if (urb->status == 0) {
  386. struct usb_ctrlrequest *req_pkt =
  387. (struct usb_ctrlrequest *)urb->transfer_buffer;
  388. if (!req_pkt) {
  389. dbg("%s: NULL req_pkt\n", __FUNCTION__);
  390. return;
  391. }
  392. if ((req_pkt->bRequestType == 0xA1) &&
  393. (req_pkt->bRequest == 0x20)) {
  394. int old_dcd_state;
  395. unsigned char signals = *((unsigned char *)
  396. urb->transfer_buffer +
  397. sizeof(struct usb_ctrlrequest));
  398. dbg("%s: signal x%x", __FUNCTION__, signals);
  399. old_dcd_state = portdata->dcd_state;
  400. portdata->cts_state = 1;
  401. portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
  402. portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
  403. portdata->ri_state = ((signals & 0x08) ? 1 : 0);
  404. if (port->tty && !C_CLOCAL(port->tty) &&
  405. old_dcd_state && !portdata->dcd_state)
  406. tty_hangup(port->tty);
  407. } else {
  408. dbg("%s: type %x req %x", __FUNCTION__,
  409. req_pkt->bRequestType,req_pkt->bRequest);
  410. }
  411. } else
  412. dbg("%s: error %d", __FUNCTION__, urb->status);
  413. /* Resubmit urb so we continue receiving IRQ data */
  414. if (urb->status != -ESHUTDOWN) {
  415. urb->dev = serial->dev;
  416. err = usb_submit_urb(urb, GFP_ATOMIC);
  417. if (err)
  418. dbg("%s: resubmit intr urb failed. (%d)",
  419. __FUNCTION__, err);
  420. }
  421. }
  422. static int option_write_room(struct usb_serial_port *port)
  423. {
  424. struct option_port_private *portdata;
  425. int i;
  426. int data_len = 0;
  427. struct urb *this_urb;
  428. portdata = usb_get_serial_port_data(port);
  429. for (i=0; i < N_OUT_URB; i++) {
  430. this_urb = portdata->out_urbs[i];
  431. if (this_urb && this_urb->status != -EINPROGRESS)
  432. data_len += OUT_BUFLEN;
  433. }
  434. dbg("%s: %d", __FUNCTION__, data_len);
  435. return data_len;
  436. }
  437. static int option_chars_in_buffer(struct usb_serial_port *port)
  438. {
  439. struct option_port_private *portdata;
  440. int i;
  441. int data_len = 0;
  442. struct urb *this_urb;
  443. portdata = usb_get_serial_port_data(port);
  444. for (i=0; i < N_OUT_URB; i++) {
  445. this_urb = portdata->out_urbs[i];
  446. if (this_urb && this_urb->status == -EINPROGRESS)
  447. data_len += this_urb->transfer_buffer_length;
  448. }
  449. dbg("%s: %d", __FUNCTION__, data_len);
  450. return data_len;
  451. }
  452. static int option_open(struct usb_serial_port *port, struct file *filp)
  453. {
  454. struct option_port_private *portdata;
  455. struct usb_serial *serial = port->serial;
  456. int i, err;
  457. struct urb *urb;
  458. portdata = usb_get_serial_port_data(port);
  459. dbg("%s", __FUNCTION__);
  460. /* Set some sane defaults */
  461. portdata->rts_state = 1;
  462. portdata->dtr_state = 1;
  463. /* Reset low level data toggle and start reading from endpoints */
  464. for (i = 0; i < N_IN_URB; i++) {
  465. urb = portdata->in_urbs[i];
  466. if (! urb)
  467. continue;
  468. if (urb->dev != serial->dev) {
  469. dbg("%s: dev %p != %p", __FUNCTION__,
  470. urb->dev, serial->dev);
  471. continue;
  472. }
  473. /*
  474. * make sure endpoint data toggle is synchronized with the
  475. * device
  476. */
  477. usb_clear_halt(urb->dev, urb->pipe);
  478. err = usb_submit_urb(urb, GFP_KERNEL);
  479. if (err) {
  480. dbg("%s: submit urb %d failed (%d) %d",
  481. __FUNCTION__, i, err,
  482. urb->transfer_buffer_length);
  483. }
  484. }
  485. /* Reset low level data toggle on out endpoints */
  486. for (i = 0; i < N_OUT_URB; i++) {
  487. urb = portdata->out_urbs[i];
  488. if (! urb)
  489. continue;
  490. urb->dev = serial->dev;
  491. /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  492. usb_pipeout(urb->pipe), 0); */
  493. }
  494. port->tty->low_latency = 1;
  495. option_send_setup(port);
  496. return (0);
  497. }
  498. static inline void stop_urb(struct urb *urb)
  499. {
  500. if (urb && urb->status == -EINPROGRESS)
  501. usb_kill_urb(urb);
  502. }
  503. static void option_close(struct usb_serial_port *port, struct file *filp)
  504. {
  505. int i;
  506. struct usb_serial *serial = port->serial;
  507. struct option_port_private *portdata;
  508. dbg("%s", __FUNCTION__);
  509. portdata = usb_get_serial_port_data(port);
  510. portdata->rts_state = 0;
  511. portdata->dtr_state = 0;
  512. if (serial->dev) {
  513. option_send_setup(port);
  514. /* Stop reading/writing urbs */
  515. for (i = 0; i < N_IN_URB; i++)
  516. stop_urb(portdata->in_urbs[i]);
  517. for (i = 0; i < N_OUT_URB; i++)
  518. stop_urb(portdata->out_urbs[i]);
  519. }
  520. port->tty = NULL;
  521. }
  522. /* Helper functions used by option_setup_urbs */
  523. static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
  524. int dir, void *ctx, char *buf, int len,
  525. void (*callback)(struct urb *, struct pt_regs *regs))
  526. {
  527. struct urb *urb;
  528. if (endpoint == -1)
  529. return NULL; /* endpoint not needed */
  530. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  531. if (urb == NULL) {
  532. dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
  533. return NULL;
  534. }
  535. /* Fill URB using supplied data. */
  536. usb_fill_bulk_urb(urb, serial->dev,
  537. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  538. buf, len, callback, ctx);
  539. return urb;
  540. }
  541. /* Setup urbs */
  542. static void option_setup_urbs(struct usb_serial *serial)
  543. {
  544. int i,j;
  545. struct usb_serial_port *port;
  546. struct option_port_private *portdata;
  547. dbg("%s", __FUNCTION__);
  548. for (i = 0; i < serial->num_ports; i++) {
  549. port = serial->port[i];
  550. portdata = usb_get_serial_port_data(port);
  551. /* Do indat endpoints first */
  552. for (j = 0; j < N_IN_URB; ++j) {
  553. portdata->in_urbs[j] = option_setup_urb (serial,
  554. port->bulk_in_endpointAddress, USB_DIR_IN, port,
  555. portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
  556. }
  557. /* outdat endpoints */
  558. for (j = 0; j < N_OUT_URB; ++j) {
  559. portdata->out_urbs[j] = option_setup_urb (serial,
  560. port->bulk_out_endpointAddress, USB_DIR_OUT, port,
  561. portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
  562. }
  563. }
  564. }
  565. static int option_send_setup(struct usb_serial_port *port)
  566. {
  567. struct usb_serial *serial = port->serial;
  568. struct option_port_private *portdata;
  569. dbg("%s", __FUNCTION__);
  570. portdata = usb_get_serial_port_data(port);
  571. if (port->tty) {
  572. int val = 0;
  573. if (portdata->dtr_state)
  574. val |= 0x01;
  575. if (portdata->rts_state)
  576. val |= 0x02;
  577. return usb_control_msg(serial->dev,
  578. usb_rcvctrlpipe(serial->dev, 0),
  579. 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
  580. }
  581. return 0;
  582. }
  583. static int option_startup(struct usb_serial *serial)
  584. {
  585. int i, err;
  586. struct usb_serial_port *port;
  587. struct option_port_private *portdata;
  588. dbg("%s", __FUNCTION__);
  589. /* Now setup per port private data */
  590. for (i = 0; i < serial->num_ports; i++) {
  591. port = serial->port[i];
  592. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  593. if (!portdata) {
  594. dbg("%s: kmalloc for option_port_private (%d) failed!.",
  595. __FUNCTION__, i);
  596. return (1);
  597. }
  598. usb_set_serial_port_data(port, portdata);
  599. if (! port->interrupt_in_urb)
  600. continue;
  601. err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  602. if (err)
  603. dbg("%s: submit irq_in urb failed %d",
  604. __FUNCTION__, err);
  605. }
  606. option_setup_urbs(serial);
  607. return (0);
  608. }
  609. static void option_shutdown(struct usb_serial *serial)
  610. {
  611. int i, j;
  612. struct usb_serial_port *port;
  613. struct option_port_private *portdata;
  614. dbg("%s", __FUNCTION__);
  615. /* Stop reading/writing urbs */
  616. for (i = 0; i < serial->num_ports; ++i) {
  617. port = serial->port[i];
  618. portdata = usb_get_serial_port_data(port);
  619. for (j = 0; j < N_IN_URB; j++)
  620. stop_urb(portdata->in_urbs[j]);
  621. for (j = 0; j < N_OUT_URB; j++)
  622. stop_urb(portdata->out_urbs[j]);
  623. }
  624. /* Now free them */
  625. for (i = 0; i < serial->num_ports; ++i) {
  626. port = serial->port[i];
  627. portdata = usb_get_serial_port_data(port);
  628. for (j = 0; j < N_IN_URB; j++) {
  629. if (portdata->in_urbs[j]) {
  630. usb_free_urb(portdata->in_urbs[j]);
  631. portdata->in_urbs[j] = NULL;
  632. }
  633. }
  634. for (j = 0; j < N_OUT_URB; j++) {
  635. if (portdata->out_urbs[j]) {
  636. usb_free_urb(portdata->out_urbs[j]);
  637. portdata->out_urbs[j] = NULL;
  638. }
  639. }
  640. }
  641. /* Now free per port private data */
  642. for (i = 0; i < serial->num_ports; i++) {
  643. port = serial->port[i];
  644. kfree(usb_get_serial_port_data(port));
  645. }
  646. }
  647. MODULE_AUTHOR(DRIVER_AUTHOR);
  648. MODULE_DESCRIPTION(DRIVER_DESC);
  649. MODULE_VERSION(DRIVER_VERSION);
  650. MODULE_LICENSE("GPL");
  651. #ifdef CONFIG_USB_DEBUG
  652. module_param(debug, bool, S_IRUGO | S_IWUSR);
  653. MODULE_PARM_DESC(debug, "Debug messages");
  654. #endif