ti_usb_3410_5052.c 40 KB

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