ti_usb_3410_5052.c 44 KB

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