ti_usb_3410_5052.c 48 KB

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