ti_usb_3410_5052.c 48 KB

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