ti_usb_3410_5052.c 42 KB

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