ti_usb_3410_5052.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /* vi: ts=8 sw=8
  2. *
  3. * TI 3410/5052 USB Serial Driver
  4. *
  5. * Copyright (C) 2004 Texas Instruments
  6. *
  7. * This driver is based on the Linux io_ti driver, which is
  8. * Copyright (C) 2000-2002 Inside Out Networks
  9. * Copyright (C) 2001-2002 Greg Kroah-Hartman
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * For questions or problems with this driver, contact Texas Instruments
  17. * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
  18. * Peter Berger <pberger@brimson.com>.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/firmware.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/module.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/serial.h>
  31. #include <linux/kfifo.h>
  32. #include <linux/mutex.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include "ti_usb_3410_5052.h"
  37. /* Defines */
  38. #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
  39. #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
  40. #define TI_FIRMWARE_BUF_SIZE 16284
  41. #define TI_TRANSFER_TIMEOUT 2
  42. #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
  43. /* supported setserial flags */
  44. #define TI_SET_SERIAL_FLAGS 0
  45. /* read urb states */
  46. #define TI_READ_URB_RUNNING 0
  47. #define TI_READ_URB_STOPPING 1
  48. #define TI_READ_URB_STOPPED 2
  49. #define TI_EXTRA_VID_PID_COUNT 5
  50. /* Structures */
  51. struct ti_port {
  52. int tp_is_open;
  53. __u8 tp_msr;
  54. __u8 tp_shadow_mcr;
  55. __u8 tp_uart_mode; /* 232 or 485 modes */
  56. unsigned int tp_uart_base_addr;
  57. int tp_flags;
  58. struct ti_device *tp_tdev;
  59. struct usb_serial_port *tp_port;
  60. spinlock_t tp_lock;
  61. int tp_read_urb_state;
  62. int tp_write_urb_in_use;
  63. };
  64. struct ti_device {
  65. struct mutex td_open_close_lock;
  66. int td_open_port_count;
  67. struct usb_serial *td_serial;
  68. int td_is_3410;
  69. int td_urb_error;
  70. };
  71. /* Function Declarations */
  72. static int ti_startup(struct usb_serial *serial);
  73. static void ti_release(struct usb_serial *serial);
  74. static int ti_port_probe(struct usb_serial_port *port);
  75. static int ti_port_remove(struct usb_serial_port *port);
  76. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
  77. static void ti_close(struct usb_serial_port *port);
  78. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  79. const unsigned char *data, int count);
  80. static int ti_write_room(struct tty_struct *tty);
  81. static int ti_chars_in_buffer(struct tty_struct *tty);
  82. static bool ti_tx_empty(struct usb_serial_port *port);
  83. static void ti_throttle(struct tty_struct *tty);
  84. static void ti_unthrottle(struct tty_struct *tty);
  85. static int ti_ioctl(struct tty_struct *tty,
  86. unsigned int cmd, unsigned long arg);
  87. static void ti_set_termios(struct tty_struct *tty,
  88. struct usb_serial_port *port, struct ktermios *old_termios);
  89. static int ti_tiocmget(struct tty_struct *tty);
  90. static int ti_tiocmset(struct tty_struct *tty,
  91. unsigned int set, unsigned int clear);
  92. static void ti_break(struct tty_struct *tty, int break_state);
  93. static void ti_interrupt_callback(struct urb *urb);
  94. static void ti_bulk_in_callback(struct urb *urb);
  95. static void ti_bulk_out_callback(struct urb *urb);
  96. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  97. int length);
  98. static void ti_send(struct ti_port *tport);
  99. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
  100. static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
  101. static int ti_get_serial_info(struct ti_port *tport,
  102. struct serial_struct __user *ret_arg);
  103. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  104. struct serial_struct __user *new_arg);
  105. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
  106. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
  107. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
  108. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  109. __u16 moduleid, __u16 value, __u8 *data, int size);
  110. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  111. __u16 moduleid, __u16 value, __u8 *data, int size);
  112. static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
  113. unsigned long addr, __u8 mask, __u8 byte);
  114. static int ti_download_firmware(struct ti_device *tdev);
  115. /* Data */
  116. /* module parameters */
  117. static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
  118. /* supported devices */
  119. static const struct usb_device_id ti_id_table_3410[] = {
  120. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  121. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  122. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  123. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  124. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  125. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  126. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  127. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  128. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  129. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  130. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  131. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  132. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  133. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
  134. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  135. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  136. { } /* terminator */
  137. };
  138. static const struct usb_device_id ti_id_table_5052[] = {
  139. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  140. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  141. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  142. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  143. { } /* terminator */
  144. };
  145. static const struct usb_device_id ti_id_table_combined[] = {
  146. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  147. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  148. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  149. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  150. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  151. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  152. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  153. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  154. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  155. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  156. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  157. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  158. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  159. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  160. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  161. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  162. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  163. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
  164. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  165. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  166. { } /* terminator */
  167. };
  168. static struct usb_serial_driver ti_1port_device = {
  169. .driver = {
  170. .owner = THIS_MODULE,
  171. .name = "ti_usb_3410_5052_1",
  172. },
  173. .description = "TI USB 3410 1 port adapter",
  174. .id_table = ti_id_table_3410,
  175. .num_ports = 1,
  176. .attach = ti_startup,
  177. .release = ti_release,
  178. .port_probe = ti_port_probe,
  179. .port_remove = ti_port_remove,
  180. .open = ti_open,
  181. .close = ti_close,
  182. .write = ti_write,
  183. .write_room = ti_write_room,
  184. .chars_in_buffer = ti_chars_in_buffer,
  185. .tx_empty = ti_tx_empty,
  186. .throttle = ti_throttle,
  187. .unthrottle = ti_unthrottle,
  188. .ioctl = ti_ioctl,
  189. .set_termios = ti_set_termios,
  190. .tiocmget = ti_tiocmget,
  191. .tiocmset = ti_tiocmset,
  192. .tiocmiwait = usb_serial_generic_tiocmiwait,
  193. .get_icount = usb_serial_generic_get_icount,
  194. .break_ctl = ti_break,
  195. .read_int_callback = ti_interrupt_callback,
  196. .read_bulk_callback = ti_bulk_in_callback,
  197. .write_bulk_callback = ti_bulk_out_callback,
  198. };
  199. static struct usb_serial_driver ti_2port_device = {
  200. .driver = {
  201. .owner = THIS_MODULE,
  202. .name = "ti_usb_3410_5052_2",
  203. },
  204. .description = "TI USB 5052 2 port adapter",
  205. .id_table = ti_id_table_5052,
  206. .num_ports = 2,
  207. .attach = ti_startup,
  208. .release = ti_release,
  209. .port_probe = ti_port_probe,
  210. .port_remove = ti_port_remove,
  211. .open = ti_open,
  212. .close = ti_close,
  213. .write = ti_write,
  214. .write_room = ti_write_room,
  215. .chars_in_buffer = ti_chars_in_buffer,
  216. .tx_empty = ti_tx_empty,
  217. .throttle = ti_throttle,
  218. .unthrottle = ti_unthrottle,
  219. .ioctl = ti_ioctl,
  220. .set_termios = ti_set_termios,
  221. .tiocmget = ti_tiocmget,
  222. .tiocmset = ti_tiocmset,
  223. .tiocmiwait = usb_serial_generic_tiocmiwait,
  224. .get_icount = usb_serial_generic_get_icount,
  225. .break_ctl = ti_break,
  226. .read_int_callback = ti_interrupt_callback,
  227. .read_bulk_callback = ti_bulk_in_callback,
  228. .write_bulk_callback = ti_bulk_out_callback,
  229. };
  230. static struct usb_serial_driver * const serial_drivers[] = {
  231. &ti_1port_device, &ti_2port_device, NULL
  232. };
  233. /* Module */
  234. MODULE_AUTHOR(TI_DRIVER_AUTHOR);
  235. MODULE_DESCRIPTION(TI_DRIVER_DESC);
  236. MODULE_LICENSE("GPL");
  237. MODULE_FIRMWARE("ti_3410.fw");
  238. MODULE_FIRMWARE("ti_5052.fw");
  239. MODULE_FIRMWARE("mts_cdma.fw");
  240. MODULE_FIRMWARE("mts_gsm.fw");
  241. MODULE_FIRMWARE("mts_edge.fw");
  242. MODULE_FIRMWARE("mts_mt9234mu.fw");
  243. MODULE_FIRMWARE("mts_mt9234zba.fw");
  244. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  245. MODULE_PARM_DESC(closing_wait,
  246. "Maximum wait for data to drain in close, in .01 secs, default is 4000");
  247. MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
  248. module_usb_serial_driver(serial_drivers, ti_id_table_combined);
  249. /* Functions */
  250. static int ti_startup(struct usb_serial *serial)
  251. {
  252. struct ti_device *tdev;
  253. struct usb_device *dev = serial->dev;
  254. int status;
  255. dev_dbg(&dev->dev,
  256. "%s - product 0x%4X, num configurations %d, configuration value %d\n",
  257. __func__, le16_to_cpu(dev->descriptor.idProduct),
  258. dev->descriptor.bNumConfigurations,
  259. dev->actconfig->desc.bConfigurationValue);
  260. /* create device structure */
  261. tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
  262. if (!tdev)
  263. return -ENOMEM;
  264. mutex_init(&tdev->td_open_close_lock);
  265. tdev->td_serial = serial;
  266. usb_set_serial_data(serial, tdev);
  267. /* determine device type */
  268. if (serial->type == &ti_1port_device)
  269. tdev->td_is_3410 = 1;
  270. dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
  271. tdev->td_is_3410 ? "3410" : "5052");
  272. /* if we have only 1 configuration, download firmware */
  273. if (dev->descriptor.bNumConfigurations == 1) {
  274. status = ti_download_firmware(tdev);
  275. if (status != 0)
  276. goto free_tdev;
  277. /* 3410 must be reset, 5052 resets itself */
  278. if (tdev->td_is_3410) {
  279. msleep_interruptible(100);
  280. usb_reset_device(dev);
  281. }
  282. status = -ENODEV;
  283. goto free_tdev;
  284. }
  285. /* the second configuration must be set */
  286. if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
  287. status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
  288. status = status ? status : -ENODEV;
  289. goto free_tdev;
  290. }
  291. return 0;
  292. free_tdev:
  293. kfree(tdev);
  294. usb_set_serial_data(serial, NULL);
  295. return status;
  296. }
  297. static void ti_release(struct usb_serial *serial)
  298. {
  299. struct ti_device *tdev = usb_get_serial_data(serial);
  300. kfree(tdev);
  301. }
  302. static int ti_port_probe(struct usb_serial_port *port)
  303. {
  304. struct ti_port *tport;
  305. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  306. if (!tport)
  307. return -ENOMEM;
  308. spin_lock_init(&tport->tp_lock);
  309. if (port == port->serial->port[0])
  310. tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
  311. else
  312. tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
  313. port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
  314. tport->tp_port = port;
  315. tport->tp_tdev = usb_get_serial_data(port->serial);
  316. tport->tp_uart_mode = 0; /* default is RS232 */
  317. usb_set_serial_port_data(port, tport);
  318. port->port.drain_delay = 3;
  319. return 0;
  320. }
  321. static int ti_port_remove(struct usb_serial_port *port)
  322. {
  323. struct ti_port *tport;
  324. tport = usb_get_serial_port_data(port);
  325. kfree(tport);
  326. return 0;
  327. }
  328. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
  329. {
  330. struct ti_port *tport = usb_get_serial_port_data(port);
  331. struct ti_device *tdev;
  332. struct usb_device *dev;
  333. struct urb *urb;
  334. int port_number;
  335. int status;
  336. __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
  337. TI_PIPE_TIMEOUT_ENABLE |
  338. (TI_TRANSFER_TIMEOUT << 2));
  339. if (tport == NULL)
  340. return -ENODEV;
  341. dev = port->serial->dev;
  342. tdev = tport->tp_tdev;
  343. /* only one open on any port on a device at a time */
  344. if (mutex_lock_interruptible(&tdev->td_open_close_lock))
  345. return -ERESTARTSYS;
  346. port_number = port->port_number;
  347. tport->tp_msr = 0;
  348. tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
  349. /* start interrupt urb the first time a port is opened on this device */
  350. if (tdev->td_open_port_count == 0) {
  351. dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
  352. urb = tdev->td_serial->port[0]->interrupt_in_urb;
  353. if (!urb) {
  354. dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
  355. status = -EINVAL;
  356. goto release_lock;
  357. }
  358. urb->context = tdev;
  359. status = usb_submit_urb(urb, GFP_KERNEL);
  360. if (status) {
  361. dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
  362. goto release_lock;
  363. }
  364. }
  365. if (tty)
  366. ti_set_termios(tty, port, &tty->termios);
  367. dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
  368. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  369. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  370. if (status) {
  371. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  372. __func__, status);
  373. goto unlink_int_urb;
  374. }
  375. dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
  376. status = ti_command_out_sync(tdev, TI_START_PORT,
  377. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  378. if (status) {
  379. dev_err(&port->dev, "%s - cannot send start command, %d\n",
  380. __func__, status);
  381. goto unlink_int_urb;
  382. }
  383. dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
  384. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  385. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
  386. if (status) {
  387. dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
  388. __func__, status);
  389. goto unlink_int_urb;
  390. }
  391. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  392. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
  393. if (status) {
  394. dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
  395. __func__, status);
  396. goto unlink_int_urb;
  397. }
  398. /* reset the data toggle on the bulk endpoints to work around bug in
  399. * host controllers where things get out of sync some times */
  400. usb_clear_halt(dev, port->write_urb->pipe);
  401. usb_clear_halt(dev, port->read_urb->pipe);
  402. if (tty)
  403. ti_set_termios(tty, port, &tty->termios);
  404. dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
  405. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  406. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  407. if (status) {
  408. dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
  409. __func__, status);
  410. goto unlink_int_urb;
  411. }
  412. dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
  413. status = ti_command_out_sync(tdev, TI_START_PORT,
  414. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  415. if (status) {
  416. dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
  417. __func__, status);
  418. goto unlink_int_urb;
  419. }
  420. /* start read urb */
  421. dev_dbg(&port->dev, "%s - start read urb\n", __func__);
  422. urb = port->read_urb;
  423. if (!urb) {
  424. dev_err(&port->dev, "%s - no read urb\n", __func__);
  425. status = -EINVAL;
  426. goto unlink_int_urb;
  427. }
  428. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  429. urb->context = tport;
  430. status = usb_submit_urb(urb, GFP_KERNEL);
  431. if (status) {
  432. dev_err(&port->dev, "%s - submit read urb failed, %d\n",
  433. __func__, status);
  434. goto unlink_int_urb;
  435. }
  436. tport->tp_is_open = 1;
  437. ++tdev->td_open_port_count;
  438. goto release_lock;
  439. unlink_int_urb:
  440. if (tdev->td_open_port_count == 0)
  441. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  442. release_lock:
  443. mutex_unlock(&tdev->td_open_close_lock);
  444. dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
  445. return status;
  446. }
  447. static void ti_close(struct usb_serial_port *port)
  448. {
  449. struct ti_device *tdev;
  450. struct ti_port *tport;
  451. int port_number;
  452. int status;
  453. int do_unlock;
  454. unsigned long flags;
  455. tdev = usb_get_serial_data(port->serial);
  456. tport = usb_get_serial_port_data(port);
  457. if (tdev == NULL || tport == NULL)
  458. return;
  459. tport->tp_is_open = 0;
  460. usb_kill_urb(port->read_urb);
  461. usb_kill_urb(port->write_urb);
  462. tport->tp_write_urb_in_use = 0;
  463. spin_lock_irqsave(&tport->tp_lock, flags);
  464. kfifo_reset_out(&port->write_fifo);
  465. spin_unlock_irqrestore(&tport->tp_lock, flags);
  466. port_number = port->port_number;
  467. dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
  468. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  469. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  470. if (status)
  471. dev_err(&port->dev,
  472. "%s - cannot send close port command, %d\n"
  473. , __func__, status);
  474. /* if mutex_lock is interrupted, continue anyway */
  475. do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
  476. --tport->tp_tdev->td_open_port_count;
  477. if (tport->tp_tdev->td_open_port_count <= 0) {
  478. /* last port is closed, shut down interrupt urb */
  479. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  480. tport->tp_tdev->td_open_port_count = 0;
  481. }
  482. if (do_unlock)
  483. mutex_unlock(&tdev->td_open_close_lock);
  484. }
  485. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  486. const unsigned char *data, int count)
  487. {
  488. struct ti_port *tport = usb_get_serial_port_data(port);
  489. if (count == 0) {
  490. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  491. return 0;
  492. }
  493. if (tport == NULL || !tport->tp_is_open)
  494. return -ENODEV;
  495. count = kfifo_in_locked(&port->write_fifo, data, count,
  496. &tport->tp_lock);
  497. ti_send(tport);
  498. return count;
  499. }
  500. static int ti_write_room(struct tty_struct *tty)
  501. {
  502. struct usb_serial_port *port = tty->driver_data;
  503. struct ti_port *tport = usb_get_serial_port_data(port);
  504. int room = 0;
  505. unsigned long flags;
  506. if (tport == NULL)
  507. return 0;
  508. spin_lock_irqsave(&tport->tp_lock, flags);
  509. room = kfifo_avail(&port->write_fifo);
  510. spin_unlock_irqrestore(&tport->tp_lock, flags);
  511. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  512. return room;
  513. }
  514. static int ti_chars_in_buffer(struct tty_struct *tty)
  515. {
  516. struct usb_serial_port *port = tty->driver_data;
  517. struct ti_port *tport = usb_get_serial_port_data(port);
  518. int chars = 0;
  519. unsigned long flags;
  520. if (tport == NULL)
  521. return 0;
  522. spin_lock_irqsave(&tport->tp_lock, flags);
  523. chars = kfifo_len(&port->write_fifo);
  524. spin_unlock_irqrestore(&tport->tp_lock, flags);
  525. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  526. return chars;
  527. }
  528. static bool ti_tx_empty(struct usb_serial_port *port)
  529. {
  530. struct ti_port *tport = usb_get_serial_port_data(port);
  531. int ret;
  532. u8 lsr;
  533. ret = ti_get_lsr(tport, &lsr);
  534. if (!ret && !(lsr & TI_LSR_TX_EMPTY))
  535. return false;
  536. return true;
  537. }
  538. static void ti_throttle(struct tty_struct *tty)
  539. {
  540. struct usb_serial_port *port = tty->driver_data;
  541. struct ti_port *tport = usb_get_serial_port_data(port);
  542. if (tport == NULL)
  543. return;
  544. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  545. ti_stop_read(tport, tty);
  546. }
  547. static void ti_unthrottle(struct tty_struct *tty)
  548. {
  549. struct usb_serial_port *port = tty->driver_data;
  550. struct ti_port *tport = usb_get_serial_port_data(port);
  551. int status;
  552. if (tport == NULL)
  553. return;
  554. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  555. status = ti_restart_read(tport, tty);
  556. if (status)
  557. dev_err(&port->dev, "%s - cannot restart read, %d\n",
  558. __func__, status);
  559. }
  560. }
  561. static int ti_ioctl(struct tty_struct *tty,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. struct usb_serial_port *port = tty->driver_data;
  565. struct ti_port *tport = usb_get_serial_port_data(port);
  566. if (tport == NULL)
  567. return -ENODEV;
  568. switch (cmd) {
  569. case TIOCGSERIAL:
  570. dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
  571. return ti_get_serial_info(tport,
  572. (struct serial_struct __user *)arg);
  573. case TIOCSSERIAL:
  574. dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
  575. return ti_set_serial_info(tty, tport,
  576. (struct serial_struct __user *)arg);
  577. }
  578. return -ENOIOCTLCMD;
  579. }
  580. static void ti_set_termios(struct tty_struct *tty,
  581. struct usb_serial_port *port, struct ktermios *old_termios)
  582. {
  583. struct ti_port *tport = usb_get_serial_port_data(port);
  584. struct ti_uart_config *config;
  585. tcflag_t cflag, iflag;
  586. int baud;
  587. int status;
  588. int port_number = port->port_number;
  589. unsigned int mcr;
  590. cflag = tty->termios.c_cflag;
  591. iflag = tty->termios.c_iflag;
  592. dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
  593. dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
  594. old_termios->c_cflag, old_termios->c_iflag);
  595. if (tport == NULL)
  596. return;
  597. config = kmalloc(sizeof(*config), GFP_KERNEL);
  598. if (!config)
  599. return;
  600. config->wFlags = 0;
  601. /* these flags must be set */
  602. config->wFlags |= TI_UART_ENABLE_MS_INTS;
  603. config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
  604. config->bUartMode = (__u8)(tport->tp_uart_mode);
  605. switch (cflag & CSIZE) {
  606. case CS5:
  607. config->bDataBits = TI_UART_5_DATA_BITS;
  608. break;
  609. case CS6:
  610. config->bDataBits = TI_UART_6_DATA_BITS;
  611. break;
  612. case CS7:
  613. config->bDataBits = TI_UART_7_DATA_BITS;
  614. break;
  615. default:
  616. case CS8:
  617. config->bDataBits = TI_UART_8_DATA_BITS;
  618. break;
  619. }
  620. /* CMSPAR isn't supported by this driver */
  621. tty->termios.c_cflag &= ~CMSPAR;
  622. if (cflag & PARENB) {
  623. if (cflag & PARODD) {
  624. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  625. config->bParity = TI_UART_ODD_PARITY;
  626. } else {
  627. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  628. config->bParity = TI_UART_EVEN_PARITY;
  629. }
  630. } else {
  631. config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  632. config->bParity = TI_UART_NO_PARITY;
  633. }
  634. if (cflag & CSTOPB)
  635. config->bStopBits = TI_UART_2_STOP_BITS;
  636. else
  637. config->bStopBits = TI_UART_1_STOP_BITS;
  638. if (cflag & CRTSCTS) {
  639. /* RTS flow control must be off to drop RTS for baud rate B0 */
  640. if ((cflag & CBAUD) != B0)
  641. config->wFlags |= TI_UART_ENABLE_RTS_IN;
  642. config->wFlags |= TI_UART_ENABLE_CTS_OUT;
  643. } else {
  644. ti_restart_read(tport, tty);
  645. }
  646. if (I_IXOFF(tty) || I_IXON(tty)) {
  647. config->cXon = START_CHAR(tty);
  648. config->cXoff = STOP_CHAR(tty);
  649. if (I_IXOFF(tty))
  650. config->wFlags |= TI_UART_ENABLE_X_IN;
  651. else
  652. ti_restart_read(tport, tty);
  653. if (I_IXON(tty))
  654. config->wFlags |= TI_UART_ENABLE_X_OUT;
  655. }
  656. baud = tty_get_baud_rate(tty);
  657. if (!baud)
  658. baud = 9600;
  659. if (tport->tp_tdev->td_is_3410)
  660. config->wBaudRate = (__u16)((923077 + baud/2) / baud);
  661. else
  662. config->wBaudRate = (__u16)((461538 + baud/2) / baud);
  663. /* FIXME: Should calculate resulting baud here and report it back */
  664. if ((cflag & CBAUD) != B0)
  665. tty_encode_baud_rate(tty, baud, baud);
  666. dev_dbg(&port->dev,
  667. "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
  668. __func__, baud, config->wBaudRate, config->wFlags,
  669. config->bDataBits, config->bParity, config->bStopBits,
  670. config->cXon, config->cXoff, config->bUartMode);
  671. cpu_to_be16s(&config->wBaudRate);
  672. cpu_to_be16s(&config->wFlags);
  673. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  674. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  675. sizeof(*config));
  676. if (status)
  677. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
  678. __func__, port_number, status);
  679. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  680. mcr = tport->tp_shadow_mcr;
  681. /* if baud rate is B0, clear RTS and DTR */
  682. if ((cflag & CBAUD) == B0)
  683. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  684. status = ti_set_mcr(tport, mcr);
  685. if (status)
  686. dev_err(&port->dev,
  687. "%s - cannot set modem control on port %d, %d\n",
  688. __func__, port_number, status);
  689. kfree(config);
  690. }
  691. static int ti_tiocmget(struct tty_struct *tty)
  692. {
  693. struct usb_serial_port *port = tty->driver_data;
  694. struct ti_port *tport = usb_get_serial_port_data(port);
  695. unsigned int result;
  696. unsigned int msr;
  697. unsigned int mcr;
  698. unsigned long flags;
  699. if (tport == NULL)
  700. return -ENODEV;
  701. spin_lock_irqsave(&tport->tp_lock, flags);
  702. msr = tport->tp_msr;
  703. mcr = tport->tp_shadow_mcr;
  704. spin_unlock_irqrestore(&tport->tp_lock, flags);
  705. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  706. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  707. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  708. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  709. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  710. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  711. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  712. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  713. return result;
  714. }
  715. static int ti_tiocmset(struct tty_struct *tty,
  716. unsigned int set, unsigned int clear)
  717. {
  718. struct usb_serial_port *port = tty->driver_data;
  719. struct ti_port *tport = usb_get_serial_port_data(port);
  720. unsigned int mcr;
  721. unsigned long flags;
  722. if (tport == NULL)
  723. return -ENODEV;
  724. spin_lock_irqsave(&tport->tp_lock, flags);
  725. mcr = tport->tp_shadow_mcr;
  726. if (set & TIOCM_RTS)
  727. mcr |= TI_MCR_RTS;
  728. if (set & TIOCM_DTR)
  729. mcr |= TI_MCR_DTR;
  730. if (set & TIOCM_LOOP)
  731. mcr |= TI_MCR_LOOP;
  732. if (clear & TIOCM_RTS)
  733. mcr &= ~TI_MCR_RTS;
  734. if (clear & TIOCM_DTR)
  735. mcr &= ~TI_MCR_DTR;
  736. if (clear & TIOCM_LOOP)
  737. mcr &= ~TI_MCR_LOOP;
  738. spin_unlock_irqrestore(&tport->tp_lock, flags);
  739. return ti_set_mcr(tport, mcr);
  740. }
  741. static void ti_break(struct tty_struct *tty, int break_state)
  742. {
  743. struct usb_serial_port *port = tty->driver_data;
  744. struct ti_port *tport = usb_get_serial_port_data(port);
  745. int status;
  746. dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
  747. if (tport == NULL)
  748. return;
  749. status = ti_write_byte(port, tport->tp_tdev,
  750. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  751. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  752. if (status)
  753. dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
  754. }
  755. static void ti_interrupt_callback(struct urb *urb)
  756. {
  757. struct ti_device *tdev = urb->context;
  758. struct usb_serial_port *port;
  759. struct usb_serial *serial = tdev->td_serial;
  760. struct ti_port *tport;
  761. struct device *dev = &urb->dev->dev;
  762. unsigned char *data = urb->transfer_buffer;
  763. int length = urb->actual_length;
  764. int port_number;
  765. int function;
  766. int status = urb->status;
  767. int retval;
  768. __u8 msr;
  769. switch (status) {
  770. case 0:
  771. break;
  772. case -ECONNRESET:
  773. case -ENOENT:
  774. case -ESHUTDOWN:
  775. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  776. tdev->td_urb_error = 1;
  777. return;
  778. default:
  779. dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
  780. tdev->td_urb_error = 1;
  781. goto exit;
  782. }
  783. if (length != 2) {
  784. dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
  785. goto exit;
  786. }
  787. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  788. dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
  789. goto exit;
  790. }
  791. port_number = TI_GET_PORT_FROM_CODE(data[0]);
  792. function = TI_GET_FUNC_FROM_CODE(data[0]);
  793. dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
  794. __func__, port_number, function, data[1]);
  795. if (port_number >= serial->num_ports) {
  796. dev_err(dev, "%s - bad port number, %d\n",
  797. __func__, port_number);
  798. goto exit;
  799. }
  800. port = serial->port[port_number];
  801. tport = usb_get_serial_port_data(port);
  802. if (!tport)
  803. goto exit;
  804. switch (function) {
  805. case TI_CODE_DATA_ERROR:
  806. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
  807. __func__, port_number, data[1]);
  808. break;
  809. case TI_CODE_MODEM_STATUS:
  810. msr = data[1];
  811. dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
  812. ti_handle_new_msr(tport, msr);
  813. break;
  814. default:
  815. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
  816. __func__, data[1]);
  817. break;
  818. }
  819. exit:
  820. retval = usb_submit_urb(urb, GFP_ATOMIC);
  821. if (retval)
  822. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
  823. __func__, retval);
  824. }
  825. static void ti_bulk_in_callback(struct urb *urb)
  826. {
  827. struct ti_port *tport = urb->context;
  828. struct usb_serial_port *port = tport->tp_port;
  829. struct device *dev = &urb->dev->dev;
  830. int status = urb->status;
  831. int retval = 0;
  832. switch (status) {
  833. case 0:
  834. break;
  835. case -ECONNRESET:
  836. case -ENOENT:
  837. case -ESHUTDOWN:
  838. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  839. tport->tp_tdev->td_urb_error = 1;
  840. return;
  841. default:
  842. dev_err(dev, "%s - nonzero urb status, %d\n",
  843. __func__, status);
  844. tport->tp_tdev->td_urb_error = 1;
  845. }
  846. if (status == -EPIPE)
  847. goto exit;
  848. if (status) {
  849. dev_err(dev, "%s - stopping read!\n", __func__);
  850. return;
  851. }
  852. if (urb->actual_length) {
  853. usb_serial_debug_data(dev, __func__, urb->actual_length,
  854. urb->transfer_buffer);
  855. if (!tport->tp_is_open)
  856. dev_dbg(dev, "%s - port closed, dropping data\n",
  857. __func__);
  858. else
  859. ti_recv(port, urb->transfer_buffer, urb->actual_length);
  860. spin_lock(&tport->tp_lock);
  861. port->icount.rx += urb->actual_length;
  862. spin_unlock(&tport->tp_lock);
  863. }
  864. exit:
  865. /* continue to read unless stopping */
  866. spin_lock(&tport->tp_lock);
  867. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  868. retval = usb_submit_urb(urb, GFP_ATOMIC);
  869. else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
  870. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  871. spin_unlock(&tport->tp_lock);
  872. if (retval)
  873. dev_err(dev, "%s - resubmit read urb failed, %d\n",
  874. __func__, retval);
  875. }
  876. static void ti_bulk_out_callback(struct urb *urb)
  877. {
  878. struct ti_port *tport = urb->context;
  879. struct usb_serial_port *port = tport->tp_port;
  880. int status = urb->status;
  881. tport->tp_write_urb_in_use = 0;
  882. switch (status) {
  883. case 0:
  884. break;
  885. case -ECONNRESET:
  886. case -ENOENT:
  887. case -ESHUTDOWN:
  888. dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
  889. tport->tp_tdev->td_urb_error = 1;
  890. return;
  891. default:
  892. dev_err_console(port, "%s - nonzero urb status, %d\n",
  893. __func__, status);
  894. tport->tp_tdev->td_urb_error = 1;
  895. }
  896. /* send any buffered data */
  897. ti_send(tport);
  898. }
  899. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  900. int length)
  901. {
  902. int cnt;
  903. do {
  904. cnt = tty_insert_flip_string(&port->port, data, length);
  905. if (cnt < length) {
  906. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  907. __func__, length - cnt);
  908. if (cnt == 0)
  909. break;
  910. }
  911. tty_flip_buffer_push(&port->port);
  912. data += cnt;
  913. length -= cnt;
  914. } while (length > 0);
  915. }
  916. static void ti_send(struct ti_port *tport)
  917. {
  918. int count, result;
  919. struct usb_serial_port *port = tport->tp_port;
  920. unsigned long flags;
  921. spin_lock_irqsave(&tport->tp_lock, flags);
  922. if (tport->tp_write_urb_in_use)
  923. goto unlock;
  924. count = kfifo_out(&port->write_fifo,
  925. port->write_urb->transfer_buffer,
  926. port->bulk_out_size);
  927. if (count == 0)
  928. goto unlock;
  929. tport->tp_write_urb_in_use = 1;
  930. spin_unlock_irqrestore(&tport->tp_lock, flags);
  931. usb_serial_debug_data(&port->dev, __func__, count,
  932. port->write_urb->transfer_buffer);
  933. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  934. usb_sndbulkpipe(port->serial->dev,
  935. port->bulk_out_endpointAddress),
  936. port->write_urb->transfer_buffer, count,
  937. ti_bulk_out_callback, tport);
  938. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  939. if (result) {
  940. dev_err_console(port, "%s - submit write urb failed, %d\n",
  941. __func__, result);
  942. tport->tp_write_urb_in_use = 0;
  943. /* TODO: reschedule ti_send */
  944. } else {
  945. spin_lock_irqsave(&tport->tp_lock, flags);
  946. port->icount.tx += count;
  947. spin_unlock_irqrestore(&tport->tp_lock, flags);
  948. }
  949. /* more room in the buffer for new writes, wakeup */
  950. tty_port_tty_wakeup(&port->port);
  951. return;
  952. unlock:
  953. spin_unlock_irqrestore(&tport->tp_lock, flags);
  954. return;
  955. }
  956. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  957. {
  958. unsigned long flags;
  959. int status;
  960. status = ti_write_byte(tport->tp_port, tport->tp_tdev,
  961. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  962. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  963. spin_lock_irqsave(&tport->tp_lock, flags);
  964. if (!status)
  965. tport->tp_shadow_mcr = mcr;
  966. spin_unlock_irqrestore(&tport->tp_lock, flags);
  967. return status;
  968. }
  969. static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
  970. {
  971. int size, status;
  972. struct ti_device *tdev = tport->tp_tdev;
  973. struct usb_serial_port *port = tport->tp_port;
  974. int port_number = port->port_number;
  975. struct ti_port_status *data;
  976. size = sizeof(struct ti_port_status);
  977. data = kmalloc(size, GFP_KERNEL);
  978. if (!data)
  979. return -ENOMEM;
  980. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  981. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  982. if (status) {
  983. dev_err(&port->dev,
  984. "%s - get port status command failed, %d\n",
  985. __func__, status);
  986. goto free_data;
  987. }
  988. dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
  989. *lsr = data->bLSR;
  990. free_data:
  991. kfree(data);
  992. return status;
  993. }
  994. static int ti_get_serial_info(struct ti_port *tport,
  995. struct serial_struct __user *ret_arg)
  996. {
  997. struct usb_serial_port *port = tport->tp_port;
  998. struct serial_struct ret_serial;
  999. unsigned cwait;
  1000. if (!ret_arg)
  1001. return -EFAULT;
  1002. cwait = port->port.closing_wait;
  1003. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1004. cwait = jiffies_to_msecs(cwait) / 10;
  1005. memset(&ret_serial, 0, sizeof(ret_serial));
  1006. ret_serial.type = PORT_16550A;
  1007. ret_serial.line = port->minor;
  1008. ret_serial.port = port->port_number;
  1009. ret_serial.flags = tport->tp_flags;
  1010. ret_serial.xmit_fifo_size = kfifo_size(&port->write_fifo);
  1011. ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1012. ret_serial.closing_wait = cwait;
  1013. if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
  1014. return -EFAULT;
  1015. return 0;
  1016. }
  1017. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  1018. struct serial_struct __user *new_arg)
  1019. {
  1020. struct serial_struct new_serial;
  1021. unsigned cwait;
  1022. if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
  1023. return -EFAULT;
  1024. cwait = new_serial.closing_wait;
  1025. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1026. cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
  1027. tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
  1028. tport->tp_port->port.closing_wait = cwait;
  1029. return 0;
  1030. }
  1031. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
  1032. {
  1033. struct async_icount *icount;
  1034. struct tty_struct *tty;
  1035. unsigned long flags;
  1036. dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
  1037. if (msr & TI_MSR_DELTA_MASK) {
  1038. spin_lock_irqsave(&tport->tp_lock, flags);
  1039. icount = &tport->tp_port->icount;
  1040. if (msr & TI_MSR_DELTA_CTS)
  1041. icount->cts++;
  1042. if (msr & TI_MSR_DELTA_DSR)
  1043. icount->dsr++;
  1044. if (msr & TI_MSR_DELTA_CD)
  1045. icount->dcd++;
  1046. if (msr & TI_MSR_DELTA_RI)
  1047. icount->rng++;
  1048. wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
  1049. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1050. }
  1051. tport->tp_msr = msr & TI_MSR_MASK;
  1052. /* handle CTS flow control */
  1053. tty = tty_port_tty_get(&tport->tp_port->port);
  1054. if (tty && C_CRTSCTS(tty)) {
  1055. if (msr & TI_MSR_CTS)
  1056. tty_wakeup(tty);
  1057. }
  1058. tty_kref_put(tty);
  1059. }
  1060. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1061. {
  1062. unsigned long flags;
  1063. spin_lock_irqsave(&tport->tp_lock, flags);
  1064. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1065. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1066. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1067. }
  1068. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1069. {
  1070. struct urb *urb;
  1071. int status = 0;
  1072. unsigned long flags;
  1073. spin_lock_irqsave(&tport->tp_lock, flags);
  1074. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1075. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1076. urb = tport->tp_port->read_urb;
  1077. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1078. urb->context = tport;
  1079. status = usb_submit_urb(urb, GFP_KERNEL);
  1080. } else {
  1081. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1082. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1083. }
  1084. return status;
  1085. }
  1086. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1087. __u16 moduleid, __u16 value, __u8 *data, int size)
  1088. {
  1089. int status;
  1090. status = usb_control_msg(tdev->td_serial->dev,
  1091. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1092. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1093. value, moduleid, data, size, 1000);
  1094. if (status == size)
  1095. status = 0;
  1096. if (status > 0)
  1097. status = -ECOMM;
  1098. return status;
  1099. }
  1100. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1101. __u16 moduleid, __u16 value, __u8 *data, int size)
  1102. {
  1103. int status;
  1104. status = usb_control_msg(tdev->td_serial->dev,
  1105. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1106. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1107. value, moduleid, data, size, 1000);
  1108. if (status == size)
  1109. status = 0;
  1110. if (status > 0)
  1111. status = -ECOMM;
  1112. return status;
  1113. }
  1114. static int ti_write_byte(struct usb_serial_port *port,
  1115. struct ti_device *tdev, unsigned long addr,
  1116. __u8 mask, __u8 byte)
  1117. {
  1118. int status;
  1119. unsigned int size;
  1120. struct ti_write_data_bytes *data;
  1121. dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
  1122. addr, mask, byte);
  1123. size = sizeof(struct ti_write_data_bytes) + 2;
  1124. data = kmalloc(size, GFP_KERNEL);
  1125. if (!data)
  1126. return -ENOMEM;
  1127. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1128. data->bDataType = TI_RW_DATA_BYTE;
  1129. data->bDataCounter = 1;
  1130. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1131. data->wBaseAddrLo = cpu_to_be16(addr);
  1132. data->bData[0] = mask;
  1133. data->bData[1] = byte;
  1134. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1135. (__u8 *)data, size);
  1136. if (status < 0)
  1137. dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
  1138. kfree(data);
  1139. return status;
  1140. }
  1141. static int ti_do_download(struct usb_device *dev, int pipe,
  1142. u8 *buffer, int size)
  1143. {
  1144. int pos;
  1145. u8 cs = 0;
  1146. int done;
  1147. struct ti_firmware_header *header;
  1148. int status = 0;
  1149. int len;
  1150. for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
  1151. cs = (__u8)(cs + buffer[pos]);
  1152. header = (struct ti_firmware_header *)buffer;
  1153. header->wLength = cpu_to_le16((__u16)(size
  1154. - sizeof(struct ti_firmware_header)));
  1155. header->bCheckSum = cs;
  1156. dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
  1157. for (pos = 0; pos < size; pos += done) {
  1158. len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1159. status = usb_bulk_msg(dev, pipe, buffer + pos, len,
  1160. &done, 1000);
  1161. if (status)
  1162. break;
  1163. }
  1164. return status;
  1165. }
  1166. static int ti_download_firmware(struct ti_device *tdev)
  1167. {
  1168. int status;
  1169. int buffer_size;
  1170. __u8 *buffer;
  1171. struct usb_device *dev = tdev->td_serial->dev;
  1172. unsigned int pipe = usb_sndbulkpipe(dev,
  1173. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1174. const struct firmware *fw_p;
  1175. char buf[32];
  1176. /* try ID specific firmware first, then try generic firmware */
  1177. sprintf(buf, "ti_usb-v%04x-p%04x.fw",
  1178. le16_to_cpu(dev->descriptor.idVendor),
  1179. le16_to_cpu(dev->descriptor.idProduct));
  1180. status = request_firmware(&fw_p, buf, &dev->dev);
  1181. if (status != 0) {
  1182. buf[0] = '\0';
  1183. if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
  1184. switch (le16_to_cpu(dev->descriptor.idProduct)) {
  1185. case MTS_CDMA_PRODUCT_ID:
  1186. strcpy(buf, "mts_cdma.fw");
  1187. break;
  1188. case MTS_GSM_PRODUCT_ID:
  1189. strcpy(buf, "mts_gsm.fw");
  1190. break;
  1191. case MTS_EDGE_PRODUCT_ID:
  1192. strcpy(buf, "mts_edge.fw");
  1193. break;
  1194. case MTS_MT9234MU_PRODUCT_ID:
  1195. strcpy(buf, "mts_mt9234mu.fw");
  1196. break;
  1197. case MTS_MT9234ZBA_PRODUCT_ID:
  1198. strcpy(buf, "mts_mt9234zba.fw");
  1199. break;
  1200. case MTS_MT9234ZBAOLD_PRODUCT_ID:
  1201. strcpy(buf, "mts_mt9234zba.fw");
  1202. break; }
  1203. }
  1204. if (buf[0] == '\0') {
  1205. if (tdev->td_is_3410)
  1206. strcpy(buf, "ti_3410.fw");
  1207. else
  1208. strcpy(buf, "ti_5052.fw");
  1209. }
  1210. status = request_firmware(&fw_p, buf, &dev->dev);
  1211. }
  1212. if (status) {
  1213. dev_err(&dev->dev, "%s - firmware not found\n", __func__);
  1214. return -ENOENT;
  1215. }
  1216. if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
  1217. dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
  1218. release_firmware(fw_p);
  1219. return -ENOENT;
  1220. }
  1221. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1222. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1223. if (buffer) {
  1224. memcpy(buffer, fw_p->data, fw_p->size);
  1225. memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
  1226. status = ti_do_download(dev, pipe, buffer, fw_p->size);
  1227. kfree(buffer);
  1228. } else {
  1229. status = -ENOMEM;
  1230. }
  1231. release_firmware(fw_p);
  1232. if (status) {
  1233. dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
  1234. __func__, status);
  1235. return status;
  1236. }
  1237. dev_dbg(&dev->dev, "%s - download successful\n", __func__);
  1238. return 0;
  1239. }