option.c 19 KB

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