ti_usb_3410_5052.c 43 KB

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