ti_usb_3410_5052.c 48 KB

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