ti_usb_3410_5052.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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_lsr;
  57. __u8 tp_shadow_mcr;
  58. __u8 tp_uart_mode; /* 232 or 485 modes */
  59. unsigned int tp_uart_base_addr;
  60. int tp_flags;
  61. int tp_closing_wait;/* in .01 secs */
  62. struct async_icount tp_icount;
  63. wait_queue_head_t tp_write_wait;
  64. struct ti_device *tp_tdev;
  65. struct usb_serial_port *tp_port;
  66. spinlock_t tp_lock;
  67. int tp_read_urb_state;
  68. int tp_write_urb_in_use;
  69. struct kfifo write_fifo;
  70. };
  71. struct ti_device {
  72. struct mutex td_open_close_lock;
  73. int td_open_port_count;
  74. struct usb_serial *td_serial;
  75. int td_is_3410;
  76. int td_urb_error;
  77. };
  78. /* Function Declarations */
  79. static int ti_startup(struct usb_serial *serial);
  80. static void ti_release(struct usb_serial *serial);
  81. static int ti_port_probe(struct usb_serial_port *port);
  82. static int ti_port_remove(struct usb_serial_port *port);
  83. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
  84. static void ti_close(struct usb_serial_port *port);
  85. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  86. const unsigned char *data, int count);
  87. static int ti_write_room(struct tty_struct *tty);
  88. static int ti_chars_in_buffer(struct tty_struct *tty);
  89. static void ti_throttle(struct tty_struct *tty);
  90. static void ti_unthrottle(struct tty_struct *tty);
  91. static int ti_ioctl(struct tty_struct *tty,
  92. unsigned int cmd, unsigned long arg);
  93. static int ti_get_icount(struct tty_struct *tty,
  94. struct serial_icounter_struct *icount);
  95. static void ti_set_termios(struct tty_struct *tty,
  96. struct usb_serial_port *port, struct ktermios *old_termios);
  97. static int ti_tiocmget(struct tty_struct *tty);
  98. static int ti_tiocmset(struct tty_struct *tty,
  99. unsigned int set, unsigned int clear);
  100. static void ti_break(struct tty_struct *tty, int break_state);
  101. static void ti_interrupt_callback(struct urb *urb);
  102. static void ti_bulk_in_callback(struct urb *urb);
  103. static void ti_bulk_out_callback(struct urb *urb);
  104. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  105. int length);
  106. static void ti_send(struct ti_port *tport);
  107. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
  108. static int ti_get_lsr(struct ti_port *tport);
  109. static int ti_get_serial_info(struct ti_port *tport,
  110. struct serial_struct __user *ret_arg);
  111. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  112. struct serial_struct __user *new_arg);
  113. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
  114. static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
  115. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
  116. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
  117. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  118. __u16 moduleid, __u16 value, __u8 *data, int size);
  119. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  120. __u16 moduleid, __u16 value, __u8 *data, int size);
  121. static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
  122. unsigned long addr, __u8 mask, __u8 byte);
  123. static int ti_download_firmware(struct ti_device *tdev);
  124. /* Data */
  125. /* module parameters */
  126. static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
  127. static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
  128. static unsigned int vendor_3410_count;
  129. static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
  130. static unsigned int product_3410_count;
  131. static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
  132. static unsigned int vendor_5052_count;
  133. static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
  134. static unsigned int product_5052_count;
  135. /* supported devices */
  136. /* the array dimension is the number of default entries plus */
  137. /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
  138. /* null entry */
  139. static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
  140. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  141. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  142. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  143. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  144. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  145. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  146. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  147. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  148. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  149. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  150. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  151. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  152. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  153. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
  154. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  155. };
  156. static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
  157. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  158. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  159. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  160. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  161. };
  162. static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
  163. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  164. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  165. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  166. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  167. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  168. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  169. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  170. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  171. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  172. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  173. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  174. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  175. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  176. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  177. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  178. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  179. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  180. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
  181. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  182. { }
  183. };
  184. static struct usb_serial_driver ti_1port_device = {
  185. .driver = {
  186. .owner = THIS_MODULE,
  187. .name = "ti_usb_3410_5052_1",
  188. },
  189. .description = "TI USB 3410 1 port adapter",
  190. .id_table = ti_id_table_3410,
  191. .num_ports = 1,
  192. .attach = ti_startup,
  193. .release = ti_release,
  194. .port_probe = ti_port_probe,
  195. .port_remove = ti_port_remove,
  196. .open = ti_open,
  197. .close = ti_close,
  198. .write = ti_write,
  199. .write_room = ti_write_room,
  200. .chars_in_buffer = ti_chars_in_buffer,
  201. .throttle = ti_throttle,
  202. .unthrottle = ti_unthrottle,
  203. .ioctl = ti_ioctl,
  204. .set_termios = ti_set_termios,
  205. .tiocmget = ti_tiocmget,
  206. .tiocmset = ti_tiocmset,
  207. .get_icount = ti_get_icount,
  208. .break_ctl = ti_break,
  209. .read_int_callback = ti_interrupt_callback,
  210. .read_bulk_callback = ti_bulk_in_callback,
  211. .write_bulk_callback = ti_bulk_out_callback,
  212. };
  213. static struct usb_serial_driver ti_2port_device = {
  214. .driver = {
  215. .owner = THIS_MODULE,
  216. .name = "ti_usb_3410_5052_2",
  217. },
  218. .description = "TI USB 5052 2 port adapter",
  219. .id_table = ti_id_table_5052,
  220. .num_ports = 2,
  221. .attach = ti_startup,
  222. .release = ti_release,
  223. .port_probe = ti_port_probe,
  224. .port_remove = ti_port_remove,
  225. .open = ti_open,
  226. .close = ti_close,
  227. .write = ti_write,
  228. .write_room = ti_write_room,
  229. .chars_in_buffer = ti_chars_in_buffer,
  230. .throttle = ti_throttle,
  231. .unthrottle = ti_unthrottle,
  232. .ioctl = ti_ioctl,
  233. .set_termios = ti_set_termios,
  234. .tiocmget = ti_tiocmget,
  235. .tiocmset = ti_tiocmset,
  236. .get_icount = ti_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. tport->tp_closing_wait = 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. return 0;
  380. }
  381. static int ti_port_remove(struct usb_serial_port *port)
  382. {
  383. struct ti_port *tport;
  384. tport = usb_get_serial_port_data(port);
  385. kfifo_free(&tport->write_fifo);
  386. kfree(tport);
  387. return 0;
  388. }
  389. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
  390. {
  391. struct ti_port *tport = usb_get_serial_port_data(port);
  392. struct ti_device *tdev;
  393. struct usb_device *dev;
  394. struct urb *urb;
  395. int port_number;
  396. int status;
  397. __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
  398. TI_PIPE_TIMEOUT_ENABLE |
  399. (TI_TRANSFER_TIMEOUT << 2));
  400. if (tport == NULL)
  401. return -ENODEV;
  402. dev = port->serial->dev;
  403. tdev = tport->tp_tdev;
  404. /* only one open on any port on a device at a time */
  405. if (mutex_lock_interruptible(&tdev->td_open_close_lock))
  406. return -ERESTARTSYS;
  407. port_number = port->number - port->serial->minor;
  408. memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
  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. tdev = usb_get_serial_data(port->serial);
  517. tport = usb_get_serial_port_data(port);
  518. if (tdev == NULL || tport == NULL)
  519. return;
  520. tport->tp_is_open = 0;
  521. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
  522. usb_kill_urb(port->read_urb);
  523. usb_kill_urb(port->write_urb);
  524. tport->tp_write_urb_in_use = 0;
  525. port_number = port->number - port->serial->minor;
  526. dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
  527. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  528. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  529. if (status)
  530. dev_err(&port->dev,
  531. "%s - cannot send close port command, %d\n"
  532. , __func__, status);
  533. /* if mutex_lock is interrupted, continue anyway */
  534. do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
  535. --tport->tp_tdev->td_open_port_count;
  536. if (tport->tp_tdev->td_open_port_count <= 0) {
  537. /* last port is closed, shut down interrupt urb */
  538. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  539. tport->tp_tdev->td_open_port_count = 0;
  540. }
  541. if (do_unlock)
  542. mutex_unlock(&tdev->td_open_close_lock);
  543. }
  544. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  545. const unsigned char *data, int count)
  546. {
  547. struct ti_port *tport = usb_get_serial_port_data(port);
  548. if (count == 0) {
  549. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  550. return 0;
  551. }
  552. if (tport == NULL || !tport->tp_is_open)
  553. return -ENODEV;
  554. count = kfifo_in_locked(&tport->write_fifo, data, count,
  555. &tport->tp_lock);
  556. ti_send(tport);
  557. return count;
  558. }
  559. static int ti_write_room(struct tty_struct *tty)
  560. {
  561. struct usb_serial_port *port = tty->driver_data;
  562. struct ti_port *tport = usb_get_serial_port_data(port);
  563. int room = 0;
  564. unsigned long flags;
  565. if (tport == NULL)
  566. return 0;
  567. spin_lock_irqsave(&tport->tp_lock, flags);
  568. room = kfifo_avail(&tport->write_fifo);
  569. spin_unlock_irqrestore(&tport->tp_lock, flags);
  570. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  571. return room;
  572. }
  573. static int ti_chars_in_buffer(struct tty_struct *tty)
  574. {
  575. struct usb_serial_port *port = tty->driver_data;
  576. struct ti_port *tport = usb_get_serial_port_data(port);
  577. int chars = 0;
  578. unsigned long flags;
  579. if (tport == NULL)
  580. return 0;
  581. spin_lock_irqsave(&tport->tp_lock, flags);
  582. chars = kfifo_len(&tport->write_fifo);
  583. spin_unlock_irqrestore(&tport->tp_lock, flags);
  584. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  585. return chars;
  586. }
  587. static void ti_throttle(struct tty_struct *tty)
  588. {
  589. struct usb_serial_port *port = tty->driver_data;
  590. struct ti_port *tport = usb_get_serial_port_data(port);
  591. if (tport == NULL)
  592. return;
  593. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  594. ti_stop_read(tport, tty);
  595. }
  596. static void ti_unthrottle(struct tty_struct *tty)
  597. {
  598. struct usb_serial_port *port = tty->driver_data;
  599. struct ti_port *tport = usb_get_serial_port_data(port);
  600. int status;
  601. if (tport == NULL)
  602. return;
  603. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  604. status = ti_restart_read(tport, tty);
  605. if (status)
  606. dev_err(&port->dev, "%s - cannot restart read, %d\n",
  607. __func__, status);
  608. }
  609. }
  610. static int ti_get_icount(struct tty_struct *tty,
  611. struct serial_icounter_struct *icount)
  612. {
  613. struct usb_serial_port *port = tty->driver_data;
  614. struct ti_port *tport = usb_get_serial_port_data(port);
  615. struct async_icount cnow = tport->tp_icount;
  616. dev_dbg(&port->dev, "%s - TIOCGICOUNT RX=%d, TX=%d\n", __func__,
  617. cnow.rx, cnow.tx);
  618. icount->cts = cnow.cts;
  619. icount->dsr = cnow.dsr;
  620. icount->rng = cnow.rng;
  621. icount->dcd = cnow.dcd;
  622. icount->rx = cnow.rx;
  623. icount->tx = cnow.tx;
  624. icount->frame = cnow.frame;
  625. icount->overrun = cnow.overrun;
  626. icount->parity = cnow.parity;
  627. icount->brk = cnow.brk;
  628. icount->buf_overrun = cnow.buf_overrun;
  629. return 0;
  630. }
  631. static int ti_ioctl(struct tty_struct *tty,
  632. unsigned int cmd, unsigned long arg)
  633. {
  634. struct usb_serial_port *port = tty->driver_data;
  635. struct ti_port *tport = usb_get_serial_port_data(port);
  636. struct async_icount cnow;
  637. struct async_icount cprev;
  638. dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
  639. if (tport == NULL)
  640. return -ENODEV;
  641. switch (cmd) {
  642. case TIOCGSERIAL:
  643. dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
  644. return ti_get_serial_info(tport,
  645. (struct serial_struct __user *)arg);
  646. case TIOCSSERIAL:
  647. dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
  648. return ti_set_serial_info(tty, tport,
  649. (struct serial_struct __user *)arg);
  650. case TIOCMIWAIT:
  651. dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
  652. cprev = tport->tp_icount;
  653. while (1) {
  654. interruptible_sleep_on(&port->delta_msr_wait);
  655. if (signal_pending(current))
  656. return -ERESTARTSYS;
  657. if (port->serial->disconnected)
  658. return -EIO;
  659. cnow = tport->tp_icount;
  660. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  661. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  662. return -EIO; /* no change => error */
  663. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  664. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  665. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  666. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
  667. return 0;
  668. cprev = cnow;
  669. }
  670. break;
  671. }
  672. return -ENOIOCTLCMD;
  673. }
  674. static void ti_set_termios(struct tty_struct *tty,
  675. struct usb_serial_port *port, struct ktermios *old_termios)
  676. {
  677. struct ti_port *tport = usb_get_serial_port_data(port);
  678. struct ti_uart_config *config;
  679. tcflag_t cflag, iflag;
  680. int baud;
  681. int status;
  682. int port_number = port->number - port->serial->minor;
  683. unsigned int mcr;
  684. cflag = tty->termios.c_cflag;
  685. iflag = tty->termios.c_iflag;
  686. dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
  687. dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
  688. old_termios->c_cflag, old_termios->c_iflag);
  689. if (tport == NULL)
  690. return;
  691. config = kmalloc(sizeof(*config), GFP_KERNEL);
  692. if (!config) {
  693. dev_err(&port->dev, "%s - out of memory\n", __func__);
  694. return;
  695. }
  696. config->wFlags = 0;
  697. /* these flags must be set */
  698. config->wFlags |= TI_UART_ENABLE_MS_INTS;
  699. config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
  700. config->bUartMode = (__u8)(tport->tp_uart_mode);
  701. switch (cflag & CSIZE) {
  702. case CS5:
  703. config->bDataBits = TI_UART_5_DATA_BITS;
  704. break;
  705. case CS6:
  706. config->bDataBits = TI_UART_6_DATA_BITS;
  707. break;
  708. case CS7:
  709. config->bDataBits = TI_UART_7_DATA_BITS;
  710. break;
  711. default:
  712. case CS8:
  713. config->bDataBits = TI_UART_8_DATA_BITS;
  714. break;
  715. }
  716. /* CMSPAR isn't supported by this driver */
  717. tty->termios.c_cflag &= ~CMSPAR;
  718. if (cflag & PARENB) {
  719. if (cflag & PARODD) {
  720. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  721. config->bParity = TI_UART_ODD_PARITY;
  722. } else {
  723. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  724. config->bParity = TI_UART_EVEN_PARITY;
  725. }
  726. } else {
  727. config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  728. config->bParity = TI_UART_NO_PARITY;
  729. }
  730. if (cflag & CSTOPB)
  731. config->bStopBits = TI_UART_2_STOP_BITS;
  732. else
  733. config->bStopBits = TI_UART_1_STOP_BITS;
  734. if (cflag & CRTSCTS) {
  735. /* RTS flow control must be off to drop RTS for baud rate B0 */
  736. if ((cflag & CBAUD) != B0)
  737. config->wFlags |= TI_UART_ENABLE_RTS_IN;
  738. config->wFlags |= TI_UART_ENABLE_CTS_OUT;
  739. } else {
  740. tty->hw_stopped = 0;
  741. ti_restart_read(tport, tty);
  742. }
  743. if (I_IXOFF(tty) || I_IXON(tty)) {
  744. config->cXon = START_CHAR(tty);
  745. config->cXoff = STOP_CHAR(tty);
  746. if (I_IXOFF(tty))
  747. config->wFlags |= TI_UART_ENABLE_X_IN;
  748. else
  749. ti_restart_read(tport, tty);
  750. if (I_IXON(tty))
  751. config->wFlags |= TI_UART_ENABLE_X_OUT;
  752. }
  753. baud = tty_get_baud_rate(tty);
  754. if (!baud)
  755. baud = 9600;
  756. if (tport->tp_tdev->td_is_3410)
  757. config->wBaudRate = (__u16)((923077 + baud/2) / baud);
  758. else
  759. config->wBaudRate = (__u16)((461538 + baud/2) / baud);
  760. /* FIXME: Should calculate resulting baud here and report it back */
  761. if ((cflag & CBAUD) != B0)
  762. tty_encode_baud_rate(tty, baud, baud);
  763. dev_dbg(&port->dev,
  764. "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
  765. __func__, baud, config->wBaudRate, config->wFlags,
  766. config->bDataBits, config->bParity, config->bStopBits,
  767. config->cXon, config->cXoff, config->bUartMode);
  768. cpu_to_be16s(&config->wBaudRate);
  769. cpu_to_be16s(&config->wFlags);
  770. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  771. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  772. sizeof(*config));
  773. if (status)
  774. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
  775. __func__, port_number, status);
  776. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  777. mcr = tport->tp_shadow_mcr;
  778. /* if baud rate is B0, clear RTS and DTR */
  779. if ((cflag & CBAUD) == B0)
  780. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  781. status = ti_set_mcr(tport, mcr);
  782. if (status)
  783. dev_err(&port->dev,
  784. "%s - cannot set modem control on port %d, %d\n",
  785. __func__, port_number, status);
  786. kfree(config);
  787. }
  788. static int ti_tiocmget(struct tty_struct *tty)
  789. {
  790. struct usb_serial_port *port = tty->driver_data;
  791. struct ti_port *tport = usb_get_serial_port_data(port);
  792. unsigned int result;
  793. unsigned int msr;
  794. unsigned int mcr;
  795. unsigned long flags;
  796. if (tport == NULL)
  797. return -ENODEV;
  798. spin_lock_irqsave(&tport->tp_lock, flags);
  799. msr = tport->tp_msr;
  800. mcr = tport->tp_shadow_mcr;
  801. spin_unlock_irqrestore(&tport->tp_lock, flags);
  802. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  803. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  804. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  805. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  806. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  807. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  808. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  809. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  810. return result;
  811. }
  812. static int ti_tiocmset(struct tty_struct *tty,
  813. unsigned int set, unsigned int clear)
  814. {
  815. struct usb_serial_port *port = tty->driver_data;
  816. struct ti_port *tport = usb_get_serial_port_data(port);
  817. unsigned int mcr;
  818. unsigned long flags;
  819. if (tport == NULL)
  820. return -ENODEV;
  821. spin_lock_irqsave(&tport->tp_lock, flags);
  822. mcr = tport->tp_shadow_mcr;
  823. if (set & TIOCM_RTS)
  824. mcr |= TI_MCR_RTS;
  825. if (set & TIOCM_DTR)
  826. mcr |= TI_MCR_DTR;
  827. if (set & TIOCM_LOOP)
  828. mcr |= TI_MCR_LOOP;
  829. if (clear & TIOCM_RTS)
  830. mcr &= ~TI_MCR_RTS;
  831. if (clear & TIOCM_DTR)
  832. mcr &= ~TI_MCR_DTR;
  833. if (clear & TIOCM_LOOP)
  834. mcr &= ~TI_MCR_LOOP;
  835. spin_unlock_irqrestore(&tport->tp_lock, flags);
  836. return ti_set_mcr(tport, mcr);
  837. }
  838. static void ti_break(struct tty_struct *tty, int break_state)
  839. {
  840. struct usb_serial_port *port = tty->driver_data;
  841. struct ti_port *tport = usb_get_serial_port_data(port);
  842. int status;
  843. dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
  844. if (tport == NULL)
  845. return;
  846. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
  847. status = ti_write_byte(port, tport->tp_tdev,
  848. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  849. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  850. if (status)
  851. dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
  852. }
  853. static void ti_interrupt_callback(struct urb *urb)
  854. {
  855. struct ti_device *tdev = urb->context;
  856. struct usb_serial_port *port;
  857. struct usb_serial *serial = tdev->td_serial;
  858. struct ti_port *tport;
  859. struct device *dev = &urb->dev->dev;
  860. unsigned char *data = urb->transfer_buffer;
  861. int length = urb->actual_length;
  862. int port_number;
  863. int function;
  864. int status = urb->status;
  865. int retval;
  866. __u8 msr;
  867. switch (status) {
  868. case 0:
  869. break;
  870. case -ECONNRESET:
  871. case -ENOENT:
  872. case -ESHUTDOWN:
  873. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  874. tdev->td_urb_error = 1;
  875. return;
  876. default:
  877. dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
  878. tdev->td_urb_error = 1;
  879. goto exit;
  880. }
  881. if (length != 2) {
  882. dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
  883. goto exit;
  884. }
  885. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  886. dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
  887. goto exit;
  888. }
  889. port_number = TI_GET_PORT_FROM_CODE(data[0]);
  890. function = TI_GET_FUNC_FROM_CODE(data[0]);
  891. dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
  892. __func__, port_number, function, data[1]);
  893. if (port_number >= serial->num_ports) {
  894. dev_err(dev, "%s - bad port number, %d\n",
  895. __func__, port_number);
  896. goto exit;
  897. }
  898. port = serial->port[port_number];
  899. tport = usb_get_serial_port_data(port);
  900. if (!tport)
  901. goto exit;
  902. switch (function) {
  903. case TI_CODE_DATA_ERROR:
  904. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
  905. __func__, port_number, data[1]);
  906. break;
  907. case TI_CODE_MODEM_STATUS:
  908. msr = data[1];
  909. dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
  910. ti_handle_new_msr(tport, msr);
  911. break;
  912. default:
  913. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
  914. __func__, data[1]);
  915. break;
  916. }
  917. exit:
  918. retval = usb_submit_urb(urb, GFP_ATOMIC);
  919. if (retval)
  920. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
  921. __func__, retval);
  922. }
  923. static void ti_bulk_in_callback(struct urb *urb)
  924. {
  925. struct ti_port *tport = urb->context;
  926. struct usb_serial_port *port = tport->tp_port;
  927. struct device *dev = &urb->dev->dev;
  928. int status = urb->status;
  929. int retval = 0;
  930. switch (status) {
  931. case 0:
  932. break;
  933. case -ECONNRESET:
  934. case -ENOENT:
  935. case -ESHUTDOWN:
  936. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  937. tport->tp_tdev->td_urb_error = 1;
  938. wake_up_interruptible(&tport->tp_write_wait);
  939. return;
  940. default:
  941. dev_err(dev, "%s - nonzero urb status, %d\n",
  942. __func__, status);
  943. tport->tp_tdev->td_urb_error = 1;
  944. wake_up_interruptible(&tport->tp_write_wait);
  945. }
  946. if (status == -EPIPE)
  947. goto exit;
  948. if (status) {
  949. dev_err(dev, "%s - stopping read!\n", __func__);
  950. return;
  951. }
  952. if (urb->actual_length) {
  953. usb_serial_debug_data(dev, __func__, urb->actual_length,
  954. urb->transfer_buffer);
  955. if (!tport->tp_is_open)
  956. dev_dbg(dev, "%s - port closed, dropping data\n",
  957. __func__);
  958. else
  959. ti_recv(port, urb->transfer_buffer, urb->actual_length);
  960. spin_lock(&tport->tp_lock);
  961. tport->tp_icount.rx += urb->actual_length;
  962. spin_unlock(&tport->tp_lock);
  963. }
  964. exit:
  965. /* continue to read unless stopping */
  966. spin_lock(&tport->tp_lock);
  967. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  968. retval = usb_submit_urb(urb, GFP_ATOMIC);
  969. else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
  970. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  971. spin_unlock(&tport->tp_lock);
  972. if (retval)
  973. dev_err(dev, "%s - resubmit read urb failed, %d\n",
  974. __func__, retval);
  975. }
  976. static void ti_bulk_out_callback(struct urb *urb)
  977. {
  978. struct ti_port *tport = urb->context;
  979. struct usb_serial_port *port = tport->tp_port;
  980. int status = urb->status;
  981. tport->tp_write_urb_in_use = 0;
  982. switch (status) {
  983. case 0:
  984. break;
  985. case -ECONNRESET:
  986. case -ENOENT:
  987. case -ESHUTDOWN:
  988. dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
  989. tport->tp_tdev->td_urb_error = 1;
  990. wake_up_interruptible(&tport->tp_write_wait);
  991. return;
  992. default:
  993. dev_err_console(port, "%s - nonzero urb status, %d\n",
  994. __func__, status);
  995. tport->tp_tdev->td_urb_error = 1;
  996. wake_up_interruptible(&tport->tp_write_wait);
  997. }
  998. /* send any buffered data */
  999. ti_send(tport);
  1000. }
  1001. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  1002. int length)
  1003. {
  1004. int cnt;
  1005. do {
  1006. cnt = tty_insert_flip_string(&port->port, data, length);
  1007. if (cnt < length) {
  1008. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  1009. __func__, length - cnt);
  1010. if (cnt == 0)
  1011. break;
  1012. }
  1013. tty_flip_buffer_push(&port->port);
  1014. data += cnt;
  1015. length -= cnt;
  1016. } while (length > 0);
  1017. }
  1018. static void ti_send(struct ti_port *tport)
  1019. {
  1020. int count, result;
  1021. struct usb_serial_port *port = tport->tp_port;
  1022. struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
  1023. unsigned long flags;
  1024. spin_lock_irqsave(&tport->tp_lock, flags);
  1025. if (tport->tp_write_urb_in_use)
  1026. goto unlock;
  1027. count = kfifo_out(&tport->write_fifo,
  1028. port->write_urb->transfer_buffer,
  1029. port->bulk_out_size);
  1030. if (count == 0)
  1031. goto unlock;
  1032. tport->tp_write_urb_in_use = 1;
  1033. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1034. usb_serial_debug_data(&port->dev, __func__, count,
  1035. port->write_urb->transfer_buffer);
  1036. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  1037. usb_sndbulkpipe(port->serial->dev,
  1038. port->bulk_out_endpointAddress),
  1039. port->write_urb->transfer_buffer, count,
  1040. ti_bulk_out_callback, tport);
  1041. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1042. if (result) {
  1043. dev_err_console(port, "%s - submit write urb failed, %d\n",
  1044. __func__, result);
  1045. tport->tp_write_urb_in_use = 0;
  1046. /* TODO: reschedule ti_send */
  1047. } else {
  1048. spin_lock_irqsave(&tport->tp_lock, flags);
  1049. tport->tp_icount.tx += count;
  1050. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1051. }
  1052. /* more room in the buffer for new writes, wakeup */
  1053. if (tty)
  1054. tty_wakeup(tty);
  1055. tty_kref_put(tty);
  1056. wake_up_interruptible(&tport->tp_write_wait);
  1057. return;
  1058. unlock:
  1059. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1060. tty_kref_put(tty);
  1061. return;
  1062. }
  1063. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  1064. {
  1065. unsigned long flags;
  1066. int status;
  1067. status = ti_write_byte(tport->tp_port, tport->tp_tdev,
  1068. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  1069. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  1070. spin_lock_irqsave(&tport->tp_lock, flags);
  1071. if (!status)
  1072. tport->tp_shadow_mcr = mcr;
  1073. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1074. return status;
  1075. }
  1076. static int ti_get_lsr(struct ti_port *tport)
  1077. {
  1078. int size, status;
  1079. struct ti_device *tdev = tport->tp_tdev;
  1080. struct usb_serial_port *port = tport->tp_port;
  1081. int port_number = port->number - port->serial->minor;
  1082. struct ti_port_status *data;
  1083. size = sizeof(struct ti_port_status);
  1084. data = kmalloc(size, GFP_KERNEL);
  1085. if (!data) {
  1086. dev_err(&port->dev, "%s - out of memory\n", __func__);
  1087. return -ENOMEM;
  1088. }
  1089. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  1090. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  1091. if (status) {
  1092. dev_err(&port->dev,
  1093. "%s - get port status command failed, %d\n",
  1094. __func__, status);
  1095. goto free_data;
  1096. }
  1097. dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
  1098. tport->tp_lsr = data->bLSR;
  1099. free_data:
  1100. kfree(data);
  1101. return status;
  1102. }
  1103. static int ti_get_serial_info(struct ti_port *tport,
  1104. struct serial_struct __user *ret_arg)
  1105. {
  1106. struct usb_serial_port *port = tport->tp_port;
  1107. struct serial_struct ret_serial;
  1108. if (!ret_arg)
  1109. return -EFAULT;
  1110. memset(&ret_serial, 0, sizeof(ret_serial));
  1111. ret_serial.type = PORT_16550A;
  1112. ret_serial.line = port->serial->minor;
  1113. ret_serial.port = port->number - port->serial->minor;
  1114. ret_serial.flags = tport->tp_flags;
  1115. ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
  1116. ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1117. ret_serial.closing_wait = tport->tp_closing_wait;
  1118. if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
  1119. return -EFAULT;
  1120. return 0;
  1121. }
  1122. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  1123. struct serial_struct __user *new_arg)
  1124. {
  1125. struct serial_struct new_serial;
  1126. if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
  1127. return -EFAULT;
  1128. tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
  1129. tport->tp_closing_wait = new_serial.closing_wait;
  1130. return 0;
  1131. }
  1132. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
  1133. {
  1134. struct async_icount *icount;
  1135. struct tty_struct *tty;
  1136. unsigned long flags;
  1137. dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
  1138. if (msr & TI_MSR_DELTA_MASK) {
  1139. spin_lock_irqsave(&tport->tp_lock, flags);
  1140. icount = &tport->tp_icount;
  1141. if (msr & TI_MSR_DELTA_CTS)
  1142. icount->cts++;
  1143. if (msr & TI_MSR_DELTA_DSR)
  1144. icount->dsr++;
  1145. if (msr & TI_MSR_DELTA_CD)
  1146. icount->dcd++;
  1147. if (msr & TI_MSR_DELTA_RI)
  1148. icount->rng++;
  1149. wake_up_interruptible(&tport->tp_port->delta_msr_wait);
  1150. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1151. }
  1152. tport->tp_msr = msr & TI_MSR_MASK;
  1153. /* handle CTS flow control */
  1154. tty = tty_port_tty_get(&tport->tp_port->port);
  1155. if (tty && C_CRTSCTS(tty)) {
  1156. if (msr & TI_MSR_CTS) {
  1157. tty->hw_stopped = 0;
  1158. tty_wakeup(tty);
  1159. } else {
  1160. tty->hw_stopped = 1;
  1161. }
  1162. }
  1163. tty_kref_put(tty);
  1164. }
  1165. static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
  1166. {
  1167. struct ti_device *tdev = tport->tp_tdev;
  1168. struct usb_serial_port *port = tport->tp_port;
  1169. wait_queue_t wait;
  1170. spin_lock_irq(&tport->tp_lock);
  1171. /* wait for data to drain from the buffer */
  1172. tdev->td_urb_error = 0;
  1173. init_waitqueue_entry(&wait, current);
  1174. add_wait_queue(&tport->tp_write_wait, &wait);
  1175. for (;;) {
  1176. set_current_state(TASK_INTERRUPTIBLE);
  1177. if (kfifo_len(&tport->write_fifo) == 0
  1178. || timeout == 0 || signal_pending(current)
  1179. || tdev->td_urb_error
  1180. || port->serial->disconnected) /* disconnect */
  1181. break;
  1182. spin_unlock_irq(&tport->tp_lock);
  1183. timeout = schedule_timeout(timeout);
  1184. spin_lock_irq(&tport->tp_lock);
  1185. }
  1186. set_current_state(TASK_RUNNING);
  1187. remove_wait_queue(&tport->tp_write_wait, &wait);
  1188. /* flush any remaining data in the buffer */
  1189. if (flush)
  1190. kfifo_reset_out(&tport->write_fifo);
  1191. spin_unlock_irq(&tport->tp_lock);
  1192. mutex_lock(&port->serial->disc_mutex);
  1193. /* wait for data to drain from the device */
  1194. /* wait for empty tx register, plus 20 ms */
  1195. timeout += jiffies;
  1196. tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
  1197. while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
  1198. && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
  1199. && !port->serial->disconnected) {
  1200. if (ti_get_lsr(tport))
  1201. break;
  1202. mutex_unlock(&port->serial->disc_mutex);
  1203. msleep_interruptible(20);
  1204. mutex_lock(&port->serial->disc_mutex);
  1205. }
  1206. mutex_unlock(&port->serial->disc_mutex);
  1207. }
  1208. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1209. {
  1210. unsigned long flags;
  1211. spin_lock_irqsave(&tport->tp_lock, flags);
  1212. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1213. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1214. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1215. }
  1216. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1217. {
  1218. struct urb *urb;
  1219. int status = 0;
  1220. unsigned long flags;
  1221. spin_lock_irqsave(&tport->tp_lock, flags);
  1222. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1223. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1224. urb = tport->tp_port->read_urb;
  1225. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1226. urb->context = tport;
  1227. status = usb_submit_urb(urb, GFP_KERNEL);
  1228. } else {
  1229. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1230. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1231. }
  1232. return status;
  1233. }
  1234. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1235. __u16 moduleid, __u16 value, __u8 *data, int size)
  1236. {
  1237. int status;
  1238. status = usb_control_msg(tdev->td_serial->dev,
  1239. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1240. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1241. value, moduleid, data, size, 1000);
  1242. if (status == size)
  1243. status = 0;
  1244. if (status > 0)
  1245. status = -ECOMM;
  1246. return status;
  1247. }
  1248. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1249. __u16 moduleid, __u16 value, __u8 *data, int size)
  1250. {
  1251. int status;
  1252. status = usb_control_msg(tdev->td_serial->dev,
  1253. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1254. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1255. value, moduleid, data, size, 1000);
  1256. if (status == size)
  1257. status = 0;
  1258. if (status > 0)
  1259. status = -ECOMM;
  1260. return status;
  1261. }
  1262. static int ti_write_byte(struct usb_serial_port *port,
  1263. struct ti_device *tdev, unsigned long addr,
  1264. __u8 mask, __u8 byte)
  1265. {
  1266. int status;
  1267. unsigned int size;
  1268. struct ti_write_data_bytes *data;
  1269. dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
  1270. addr, mask, byte);
  1271. size = sizeof(struct ti_write_data_bytes) + 2;
  1272. data = kmalloc(size, GFP_KERNEL);
  1273. if (!data) {
  1274. dev_err(&port->dev, "%s - out of memory\n", __func__);
  1275. return -ENOMEM;
  1276. }
  1277. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1278. data->bDataType = TI_RW_DATA_BYTE;
  1279. data->bDataCounter = 1;
  1280. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1281. data->wBaseAddrLo = cpu_to_be16(addr);
  1282. data->bData[0] = mask;
  1283. data->bData[1] = byte;
  1284. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1285. (__u8 *)data, size);
  1286. if (status < 0)
  1287. dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
  1288. kfree(data);
  1289. return status;
  1290. }
  1291. static int ti_do_download(struct usb_device *dev, int pipe,
  1292. u8 *buffer, int size)
  1293. {
  1294. int pos;
  1295. u8 cs = 0;
  1296. int done;
  1297. struct ti_firmware_header *header;
  1298. int status = 0;
  1299. int len;
  1300. for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
  1301. cs = (__u8)(cs + buffer[pos]);
  1302. header = (struct ti_firmware_header *)buffer;
  1303. header->wLength = cpu_to_le16((__u16)(size
  1304. - sizeof(struct ti_firmware_header)));
  1305. header->bCheckSum = cs;
  1306. dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
  1307. for (pos = 0; pos < size; pos += done) {
  1308. len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1309. status = usb_bulk_msg(dev, pipe, buffer + pos, len,
  1310. &done, 1000);
  1311. if (status)
  1312. break;
  1313. }
  1314. return status;
  1315. }
  1316. static int ti_download_firmware(struct ti_device *tdev)
  1317. {
  1318. int status;
  1319. int buffer_size;
  1320. __u8 *buffer;
  1321. struct usb_device *dev = tdev->td_serial->dev;
  1322. unsigned int pipe = usb_sndbulkpipe(dev,
  1323. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1324. const struct firmware *fw_p;
  1325. char buf[32];
  1326. /* try ID specific firmware first, then try generic firmware */
  1327. sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
  1328. dev->descriptor.idProduct);
  1329. status = request_firmware(&fw_p, buf, &dev->dev);
  1330. if (status != 0) {
  1331. buf[0] = '\0';
  1332. if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
  1333. switch (dev->descriptor.idProduct) {
  1334. case MTS_CDMA_PRODUCT_ID:
  1335. strcpy(buf, "mts_cdma.fw");
  1336. break;
  1337. case MTS_GSM_PRODUCT_ID:
  1338. strcpy(buf, "mts_gsm.fw");
  1339. break;
  1340. case MTS_EDGE_PRODUCT_ID:
  1341. strcpy(buf, "mts_edge.fw");
  1342. break;
  1343. case MTS_MT9234MU_PRODUCT_ID:
  1344. strcpy(buf, "mts_mt9234mu.fw");
  1345. break;
  1346. case MTS_MT9234ZBA_PRODUCT_ID:
  1347. strcpy(buf, "mts_mt9234zba.fw");
  1348. break;
  1349. case MTS_MT9234ZBAOLD_PRODUCT_ID:
  1350. strcpy(buf, "mts_mt9234zba.fw");
  1351. break; }
  1352. }
  1353. if (buf[0] == '\0') {
  1354. if (tdev->td_is_3410)
  1355. strcpy(buf, "ti_3410.fw");
  1356. else
  1357. strcpy(buf, "ti_5052.fw");
  1358. }
  1359. status = request_firmware(&fw_p, buf, &dev->dev);
  1360. }
  1361. if (status) {
  1362. dev_err(&dev->dev, "%s - firmware not found\n", __func__);
  1363. return -ENOENT;
  1364. }
  1365. if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
  1366. dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
  1367. release_firmware(fw_p);
  1368. return -ENOENT;
  1369. }
  1370. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1371. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1372. if (buffer) {
  1373. memcpy(buffer, fw_p->data, fw_p->size);
  1374. memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
  1375. status = ti_do_download(dev, pipe, buffer, fw_p->size);
  1376. kfree(buffer);
  1377. } else {
  1378. dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
  1379. status = -ENOMEM;
  1380. }
  1381. release_firmware(fw_p);
  1382. if (status) {
  1383. dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
  1384. __func__, status);
  1385. return status;
  1386. }
  1387. dev_dbg(&dev->dev, "%s - download successful\n", __func__);
  1388. return 0;
  1389. }