mos7720.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <asm/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port
  61. {
  62. __u8 shadowLCR; /* last LCR value received */
  63. __u8 shadowMCR; /* last MCR value received */
  64. __u8 shadowMSR; /* last MSR value received */
  65. char open;
  66. struct async_icount icount;
  67. struct usb_serial_port *port; /* loop back to the owner */
  68. struct urb *write_urb_pool[NUM_URBS];
  69. };
  70. /* This structure holds all of the individual serial device information */
  71. struct moschip_serial
  72. {
  73. int interrupt_started;
  74. };
  75. static int debug;
  76. #define USB_VENDOR_ID_MOSCHIP 0x9710
  77. #define MOSCHIP_DEVICE_ID_7720 0x7720
  78. #define MOSCHIP_DEVICE_ID_7715 0x7715
  79. static struct usb_device_id moschip_port_id_table [] = {
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. int status = urb->status;
  94. __u8 *data;
  95. __u8 sp1;
  96. __u8 sp2;
  97. dbg("%s"," : Entering\n");
  98. if (!urb) {
  99. dbg("%s","Invalid Pointer !!!!:\n");
  100. return;
  101. }
  102. switch (status) {
  103. case 0:
  104. /* success */
  105. break;
  106. case -ECONNRESET:
  107. case -ENOENT:
  108. case -ESHUTDOWN:
  109. /* this urb is terminated, clean up */
  110. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  111. status);
  112. return;
  113. default:
  114. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  115. status);
  116. goto exit;
  117. }
  118. length = urb->actual_length;
  119. data = urb->transfer_buffer;
  120. /* Moschip get 4 bytes
  121. * Byte 1 IIR Port 1 (port.number is 0)
  122. * Byte 2 IIR Port 2 (port.number is 1)
  123. * Byte 3 --------------
  124. * Byte 4 FIFO status for both */
  125. /* the above description is inverted
  126. * oneukum 2007-03-14 */
  127. if (unlikely(length != 4)) {
  128. dbg("Wrong data !!!");
  129. return;
  130. }
  131. sp1 = data[3];
  132. sp2 = data[2];
  133. if ((sp1 | sp2) & 0x01) {
  134. /* No Interrupt Pending in both the ports */
  135. dbg("No Interrupt !!!");
  136. } else {
  137. switch (sp1 & 0x0f) {
  138. case SERIAL_IIR_RLS:
  139. dbg("Serial Port 1: Receiver status error or address "
  140. "bit detected in 9-bit mode\n");
  141. break;
  142. case SERIAL_IIR_CTI:
  143. dbg("Serial Port 1: Receiver time out");
  144. break;
  145. case SERIAL_IIR_MS:
  146. dbg("Serial Port 1: Modem status change");
  147. break;
  148. }
  149. switch (sp2 & 0x0f) {
  150. case SERIAL_IIR_RLS:
  151. dbg("Serial Port 2: Receiver status error or address "
  152. "bit detected in 9-bit mode");
  153. break;
  154. case SERIAL_IIR_CTI:
  155. dbg("Serial Port 2: Receiver time out");
  156. break;
  157. case SERIAL_IIR_MS:
  158. dbg("Serial Port 2: Modem status change");
  159. break;
  160. }
  161. }
  162. exit:
  163. result = usb_submit_urb(urb, GFP_ATOMIC);
  164. if (result)
  165. dev_err(&urb->dev->dev,
  166. "%s - Error %d submitting control urb\n",
  167. __FUNCTION__, result);
  168. return;
  169. }
  170. /*
  171. * mos7720_bulk_in_callback
  172. * this is the callback function for when we have received data on the
  173. * bulk in endpoint.
  174. */
  175. static void mos7720_bulk_in_callback(struct urb *urb)
  176. {
  177. int retval;
  178. unsigned char *data ;
  179. struct usb_serial_port *port;
  180. struct moschip_port *mos7720_port;
  181. struct tty_struct *tty;
  182. int status = urb->status;
  183. if (status) {
  184. dbg("nonzero read bulk status received: %d", status);
  185. return;
  186. }
  187. mos7720_port = urb->context;
  188. if (!mos7720_port) {
  189. dbg("%s","NULL mos7720_port pointer \n");
  190. return ;
  191. }
  192. port = mos7720_port->port;
  193. dbg("Entering...%s", __FUNCTION__);
  194. data = urb->transfer_buffer;
  195. tty = port->tty;
  196. if (tty && urb->actual_length) {
  197. tty_buffer_request_room(tty, urb->actual_length);
  198. tty_insert_flip_string(tty, data, urb->actual_length);
  199. tty_flip_buffer_push(tty);
  200. }
  201. if (!port->read_urb) {
  202. dbg("URB KILLED !!!");
  203. return;
  204. }
  205. if (port->read_urb->status != -EINPROGRESS) {
  206. port->read_urb->dev = port->serial->dev;
  207. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  208. if (retval)
  209. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  210. retval);
  211. }
  212. }
  213. /*
  214. * mos7720_bulk_out_data_callback
  215. * this is the callback function for when we have finished sending serial
  216. * data on the bulk out endpoint.
  217. */
  218. static void mos7720_bulk_out_data_callback(struct urb *urb)
  219. {
  220. struct moschip_port *mos7720_port;
  221. struct tty_struct *tty;
  222. int status = urb->status;
  223. if (status) {
  224. dbg("nonzero write bulk status received:%d", status);
  225. return;
  226. }
  227. mos7720_port = urb->context;
  228. if (!mos7720_port) {
  229. dbg("NULL mos7720_port pointer");
  230. return ;
  231. }
  232. dbg("Entering .........");
  233. tty = mos7720_port->port->tty;
  234. if (tty && mos7720_port->open)
  235. tty_wakeup(tty);
  236. }
  237. /*
  238. * send_mos_cmd
  239. * this function will be used for sending command to device
  240. */
  241. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  242. __u16 index, void *data)
  243. {
  244. int status;
  245. unsigned int pipe;
  246. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  247. __u8 requesttype;
  248. __u16 size = 0x0000;
  249. if (value < MOS_MAX_PORT) {
  250. if (product == MOSCHIP_DEVICE_ID_7715) {
  251. value = value*0x100+0x100;
  252. } else {
  253. value = value*0x100+0x200;
  254. }
  255. } else {
  256. value = 0x0000;
  257. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  258. (index != 0x08)) {
  259. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  260. //index = 0x01 ;
  261. }
  262. }
  263. if (request == MOS_WRITE) {
  264. request = (__u8)MOS_WRITE;
  265. requesttype = (__u8)0x40;
  266. value = value + (__u16)*((unsigned char *)data);
  267. data = NULL;
  268. pipe = usb_sndctrlpipe(serial->dev, 0);
  269. } else {
  270. request = (__u8)MOS_READ;
  271. requesttype = (__u8)0xC0;
  272. size = 0x01;
  273. pipe = usb_rcvctrlpipe(serial->dev,0);
  274. }
  275. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  276. value, index, data, size, MOS_WDR_TIMEOUT);
  277. if (status < 0)
  278. dbg("Command Write failed Value %x index %x\n",value,index);
  279. return status;
  280. }
  281. static int mos7720_open(struct usb_serial_port *port, struct file * filp)
  282. {
  283. struct usb_serial *serial;
  284. struct usb_serial_port *port0;
  285. struct urb *urb;
  286. struct moschip_serial *mos7720_serial;
  287. struct moschip_port *mos7720_port;
  288. int response;
  289. int port_number;
  290. char data;
  291. int allocated_urbs = 0;
  292. int j;
  293. serial = port->serial;
  294. mos7720_port = usb_get_serial_port_data(port);
  295. if (mos7720_port == NULL)
  296. return -ENODEV;
  297. port0 = serial->port[0];
  298. mos7720_serial = usb_get_serial_data(serial);
  299. if (mos7720_serial == NULL || port0 == NULL)
  300. return -ENODEV;
  301. usb_clear_halt(serial->dev, port->write_urb->pipe);
  302. usb_clear_halt(serial->dev, port->read_urb->pipe);
  303. /* Initialising the write urb pool */
  304. for (j = 0; j < NUM_URBS; ++j) {
  305. urb = usb_alloc_urb(0,GFP_KERNEL);
  306. mos7720_port->write_urb_pool[j] = urb;
  307. if (urb == NULL) {
  308. err("No more urbs???");
  309. continue;
  310. }
  311. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  312. GFP_KERNEL);
  313. if (!urb->transfer_buffer) {
  314. err("%s-out of memory for urb buffers.", __FUNCTION__);
  315. usb_free_urb(mos7720_port->write_urb_pool[j]);
  316. mos7720_port->write_urb_pool[j] = NULL;
  317. continue;
  318. }
  319. allocated_urbs++;
  320. }
  321. if (!allocated_urbs)
  322. return -ENOMEM;
  323. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  324. *
  325. * Register Index
  326. * 1 : IER
  327. * 2 : FCR
  328. * 3 : LCR
  329. * 4 : MCR
  330. *
  331. * 0x08 : SP1/2 Control Reg
  332. */
  333. port_number = port->number - port->serial->minor;
  334. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  335. dbg("SS::%p LSR:%x\n",mos7720_port, data);
  336. dbg("Check:Sending Command ..........");
  337. data = 0x02;
  338. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  339. data = 0x02;
  340. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  341. data = 0x00;
  342. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  343. data = 0x00;
  344. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  345. data = 0xCF;
  346. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  347. data = 0x03;
  348. mos7720_port->shadowLCR = data;
  349. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  350. data = 0x0b;
  351. mos7720_port->shadowMCR = data;
  352. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  353. data = 0x0b;
  354. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  355. data = 0x00;
  356. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  357. data = 0x00;
  358. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  359. /* data = 0x00;
  360. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  361. data = 0x03;
  362. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  363. data = 0x00;
  364. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  365. */
  366. data = 0x00;
  367. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  368. data = data | (port->number - port->serial->minor + 1);
  369. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  370. data = 0x83;
  371. mos7720_port->shadowLCR = data;
  372. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  373. data = 0x0c;
  374. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  375. data = 0x00;
  376. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  377. data = 0x03;
  378. mos7720_port->shadowLCR = data;
  379. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  380. data = 0x0c;
  381. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  382. data = 0x0c;
  383. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  384. //Matrix
  385. /* force low_latency on so that our tty_push actually forces *
  386. * the data through,otherwise it is scheduled, and with *
  387. * high data rates (like with OHCI) data can get lost. */
  388. if (port->tty)
  389. port->tty->low_latency = 1;
  390. /* see if we've set up our endpoint info yet *
  391. * (can't set it up in mos7720_startup as the *
  392. * structures were not set up at that time.) */
  393. if (!mos7720_serial->interrupt_started) {
  394. dbg("Interrupt buffer NULL !!!");
  395. /* not set up yet, so do it now */
  396. mos7720_serial->interrupt_started = 1;
  397. dbg("To Submit URB !!!");
  398. /* set up our interrupt urb */
  399. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  400. usb_rcvintpipe(serial->dev,
  401. port->interrupt_in_endpointAddress),
  402. port0->interrupt_in_buffer,
  403. port0->interrupt_in_urb->transfer_buffer_length,
  404. mos7720_interrupt_callback, mos7720_port,
  405. port0->interrupt_in_urb->interval);
  406. /* start interrupt read for this mos7720 this interrupt *
  407. * will continue as long as the mos7720 is connected */
  408. dbg("Submit URB over !!!");
  409. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  410. if (response)
  411. dev_err(&port->dev,
  412. "%s - Error %d submitting control urb",
  413. __FUNCTION__, response);
  414. }
  415. /* set up our bulk in urb */
  416. usb_fill_bulk_urb(port->read_urb, serial->dev,
  417. usb_rcvbulkpipe(serial->dev,
  418. port->bulk_in_endpointAddress),
  419. port->bulk_in_buffer,
  420. port->read_urb->transfer_buffer_length,
  421. mos7720_bulk_in_callback, mos7720_port);
  422. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  423. if (response)
  424. dev_err(&port->dev,
  425. "%s - Error %d submitting read urb", __FUNCTION__, response);
  426. /* initialize our icount structure */
  427. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  428. /* initialize our port settings */
  429. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  430. /* send a open port command */
  431. mos7720_port->open = 1;
  432. return 0;
  433. }
  434. /*
  435. * mos7720_chars_in_buffer
  436. * this function is called by the tty driver when it wants to know how many
  437. * bytes of data we currently have outstanding in the port (data that has
  438. * been written, but hasn't made it out the port yet)
  439. * If successful, we return the number of bytes left to be written in the
  440. * system,
  441. * Otherwise we return a negative error number.
  442. */
  443. static int mos7720_chars_in_buffer(struct usb_serial_port *port)
  444. {
  445. int i;
  446. int chars = 0;
  447. struct moschip_port *mos7720_port;
  448. dbg("%s:entering ...........", __FUNCTION__);
  449. mos7720_port = usb_get_serial_port_data(port);
  450. if (mos7720_port == NULL) {
  451. dbg("%s:leaving ...........", __FUNCTION__);
  452. return -ENODEV;
  453. }
  454. for (i = 0; i < NUM_URBS; ++i) {
  455. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  456. chars += URB_TRANSFER_BUFFER_SIZE;
  457. }
  458. dbg("%s - returns %d", __FUNCTION__, chars);
  459. return chars;
  460. }
  461. static void mos7720_close(struct usb_serial_port *port, struct file *filp)
  462. {
  463. struct usb_serial *serial;
  464. struct moschip_port *mos7720_port;
  465. char data;
  466. int j;
  467. dbg("mos7720_close:entering...");
  468. serial = port->serial;
  469. mos7720_port = usb_get_serial_port_data(port);
  470. if (mos7720_port == NULL)
  471. return;
  472. for (j = 0; j < NUM_URBS; ++j)
  473. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  474. /* Freeing Write URBs */
  475. for (j = 0; j < NUM_URBS; ++j) {
  476. if (mos7720_port->write_urb_pool[j]) {
  477. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  478. usb_free_urb(mos7720_port->write_urb_pool[j]);
  479. }
  480. }
  481. /* While closing port, shutdown all bulk read, write *
  482. * and interrupt read if they exists */
  483. if (serial->dev) {
  484. dbg("Shutdown bulk write");
  485. usb_kill_urb(port->write_urb);
  486. dbg("Shutdown bulk read");
  487. usb_kill_urb(port->read_urb);
  488. }
  489. data = 0x00;
  490. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  491. 0x04, &data);
  492. data = 0x00;
  493. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  494. 0x01, &data);
  495. mos7720_port->open = 0;
  496. dbg("Leaving %s", __FUNCTION__);
  497. }
  498. static void mos7720_break(struct usb_serial_port *port, int break_state)
  499. {
  500. unsigned char data;
  501. struct usb_serial *serial;
  502. struct moschip_port *mos7720_port;
  503. dbg("Entering %s", __FUNCTION__);
  504. serial = port->serial;
  505. mos7720_port = usb_get_serial_port_data(port);
  506. if (mos7720_port == NULL)
  507. return;
  508. if (break_state == -1)
  509. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  510. else
  511. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  512. mos7720_port->shadowLCR = data;
  513. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  514. 0x03, &data);
  515. return;
  516. }
  517. /*
  518. * mos7720_write_room
  519. * this function is called by the tty driver when it wants to know how many
  520. * bytes of data we can accept for a specific port.
  521. * If successful, we return the amount of room that we have for this port
  522. * Otherwise we return a negative error number.
  523. */
  524. static int mos7720_write_room(struct usb_serial_port *port)
  525. {
  526. struct moschip_port *mos7720_port;
  527. int room = 0;
  528. int i;
  529. dbg("%s:entering ...........", __FUNCTION__);
  530. mos7720_port = usb_get_serial_port_data(port);
  531. if (mos7720_port == NULL) {
  532. dbg("%s:leaving ...........", __FUNCTION__);
  533. return -ENODEV;
  534. }
  535. for (i = 0; i < NUM_URBS; ++i) {
  536. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  537. room += URB_TRANSFER_BUFFER_SIZE;
  538. }
  539. dbg("%s - returns %d", __FUNCTION__, room);
  540. return room;
  541. }
  542. static int mos7720_write(struct usb_serial_port *port,
  543. const unsigned char *data, int count)
  544. {
  545. int status;
  546. int i;
  547. int bytes_sent = 0;
  548. int transfer_size;
  549. struct moschip_port *mos7720_port;
  550. struct usb_serial *serial;
  551. struct urb *urb;
  552. const unsigned char *current_position = data;
  553. dbg("%s:entering ...........", __FUNCTION__);
  554. serial = port->serial;
  555. mos7720_port = usb_get_serial_port_data(port);
  556. if (mos7720_port == NULL) {
  557. dbg("mos7720_port is NULL");
  558. return -ENODEV;
  559. }
  560. /* try to find a free urb in the list */
  561. urb = NULL;
  562. for (i = 0; i < NUM_URBS; ++i) {
  563. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  564. urb = mos7720_port->write_urb_pool[i];
  565. dbg("URB:%d",i);
  566. break;
  567. }
  568. }
  569. if (urb == NULL) {
  570. dbg("%s - no more free urbs", __FUNCTION__);
  571. goto exit;
  572. }
  573. if (urb->transfer_buffer == NULL) {
  574. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  575. GFP_KERNEL);
  576. if (urb->transfer_buffer == NULL) {
  577. err("%s no more kernel memory...", __FUNCTION__);
  578. goto exit;
  579. }
  580. }
  581. transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
  582. memcpy(urb->transfer_buffer, current_position, transfer_size);
  583. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
  584. urb->transfer_buffer);
  585. /* fill urb with data and submit */
  586. usb_fill_bulk_urb(urb, serial->dev,
  587. usb_sndbulkpipe(serial->dev,
  588. port->bulk_out_endpointAddress),
  589. urb->transfer_buffer, transfer_size,
  590. mos7720_bulk_out_data_callback, mos7720_port);
  591. /* send it down the pipe */
  592. status = usb_submit_urb(urb,GFP_ATOMIC);
  593. if (status) {
  594. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  595. __FUNCTION__, status);
  596. bytes_sent = status;
  597. goto exit;
  598. }
  599. bytes_sent = transfer_size;
  600. exit:
  601. return bytes_sent;
  602. }
  603. static void mos7720_throttle(struct usb_serial_port *port)
  604. {
  605. struct moschip_port *mos7720_port;
  606. struct tty_struct *tty;
  607. int status;
  608. dbg("%s- port %d\n", __FUNCTION__, port->number);
  609. mos7720_port = usb_get_serial_port_data(port);
  610. if (mos7720_port == NULL)
  611. return;
  612. if (!mos7720_port->open) {
  613. dbg("port not opened");
  614. return;
  615. }
  616. dbg("%s: Entering ..........", __FUNCTION__);
  617. tty = port->tty;
  618. if (!tty) {
  619. dbg("%s - no tty available", __FUNCTION__);
  620. return;
  621. }
  622. /* if we are implementing XON/XOFF, send the stop character */
  623. if (I_IXOFF(tty)) {
  624. unsigned char stop_char = STOP_CHAR(tty);
  625. status = mos7720_write(port, &stop_char, 1);
  626. if (status <= 0)
  627. return;
  628. }
  629. /* if we are implementing RTS/CTS, toggle that line */
  630. if (tty->termios->c_cflag & CRTSCTS) {
  631. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  632. status = send_mos_cmd(port->serial, MOS_WRITE,
  633. port->number - port->serial->minor,
  634. UART_MCR, &mos7720_port->shadowMCR);
  635. if (status != 0)
  636. return;
  637. }
  638. }
  639. static void mos7720_unthrottle(struct usb_serial_port *port)
  640. {
  641. struct tty_struct *tty;
  642. int status;
  643. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  644. if (mos7720_port == NULL)
  645. return;
  646. if (!mos7720_port->open) {
  647. dbg("%s - port not opened", __FUNCTION__);
  648. return;
  649. }
  650. dbg("%s: Entering ..........", __FUNCTION__);
  651. tty = port->tty;
  652. if (!tty) {
  653. dbg("%s - no tty available", __FUNCTION__);
  654. return;
  655. }
  656. /* if we are implementing XON/XOFF, send the start character */
  657. if (I_IXOFF(tty)) {
  658. unsigned char start_char = START_CHAR(tty);
  659. status = mos7720_write(port, &start_char, 1);
  660. if (status <= 0)
  661. return;
  662. }
  663. /* if we are implementing RTS/CTS, toggle that line */
  664. if (tty->termios->c_cflag & CRTSCTS) {
  665. mos7720_port->shadowMCR |= UART_MCR_RTS;
  666. status = send_mos_cmd(port->serial, MOS_WRITE,
  667. port->number - port->serial->minor,
  668. UART_MCR, &mos7720_port->shadowMCR);
  669. if (status != 0)
  670. return;
  671. }
  672. }
  673. static int set_higher_rates(struct moschip_port *mos7720_port,
  674. unsigned int baud)
  675. {
  676. unsigned char data;
  677. struct usb_serial_port *port;
  678. struct usb_serial *serial;
  679. int port_number;
  680. if (mos7720_port == NULL)
  681. return -EINVAL;
  682. port = mos7720_port->port;
  683. serial = port->serial;
  684. /***********************************************
  685. * Init Sequence for higher rates
  686. ***********************************************/
  687. dbg("Sending Setting Commands ..........");
  688. port_number = port->number - port->serial->minor;
  689. data = 0x000;
  690. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  691. data = 0x000;
  692. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  693. data = 0x0CF;
  694. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  695. data = 0x00b;
  696. mos7720_port->shadowMCR = data;
  697. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  698. data = 0x00b;
  699. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  700. data = 0x000;
  701. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  702. data = 0x000;
  703. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  704. /***********************************************
  705. * Set for higher rates *
  706. ***********************************************/
  707. data = baud * 0x10;
  708. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
  709. data = 0x003;
  710. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  711. data = 0x003;
  712. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  713. data = 0x02b;
  714. mos7720_port->shadowMCR = data;
  715. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  716. data = 0x02b;
  717. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  718. /***********************************************
  719. * Set DLL/DLM
  720. ***********************************************/
  721. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  722. mos7720_port->shadowLCR = data;
  723. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  724. data = 0x001; /* DLL */
  725. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  726. data = 0x000; /* DLM */
  727. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  728. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  729. mos7720_port->shadowLCR = data;
  730. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  731. return 0;
  732. }
  733. /* baud rate information */
  734. struct divisor_table_entry
  735. {
  736. __u32 baudrate;
  737. __u16 divisor;
  738. };
  739. /* Define table of divisors for moschip 7720 hardware *
  740. * These assume a 3.6864MHz crystal, the standard /16, and *
  741. * MCR.7 = 0. */
  742. static struct divisor_table_entry divisor_table[] = {
  743. { 50, 2304},
  744. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  745. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  746. { 150, 768},
  747. { 300, 384},
  748. { 600, 192},
  749. { 1200, 96},
  750. { 1800, 64},
  751. { 2400, 48},
  752. { 4800, 24},
  753. { 7200, 16},
  754. { 9600, 12},
  755. { 19200, 6},
  756. { 38400, 3},
  757. { 57600, 2},
  758. { 115200, 1},
  759. };
  760. /*****************************************************************************
  761. * calc_baud_rate_divisor
  762. * this function calculates the proper baud rate divisor for the specified
  763. * baud rate.
  764. *****************************************************************************/
  765. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  766. {
  767. int i;
  768. __u16 custom;
  769. __u16 round1;
  770. __u16 round;
  771. dbg("%s - %d", __FUNCTION__, baudrate);
  772. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  773. if (divisor_table[i].baudrate == baudrate) {
  774. *divisor = divisor_table[i].divisor;
  775. return 0;
  776. }
  777. }
  778. /* After trying for all the standard baud rates *
  779. * Try calculating the divisor for this baud rate */
  780. if (baudrate > 75 && baudrate < 230400) {
  781. /* get the divisor */
  782. custom = (__u16)(230400L / baudrate);
  783. /* Check for round off */
  784. round1 = (__u16)(2304000L / baudrate);
  785. round = (__u16)(round1 - (custom * 10));
  786. if (round > 4)
  787. custom++;
  788. *divisor = custom;
  789. dbg("Baud %d = %d",baudrate, custom);
  790. return 0;
  791. }
  792. dbg("Baud calculation Failed...");
  793. return -EINVAL;
  794. }
  795. /*
  796. * send_cmd_write_baud_rate
  797. * this function sends the proper command to change the baud rate of the
  798. * specified port.
  799. */
  800. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  801. int baudrate)
  802. {
  803. struct usb_serial_port *port;
  804. struct usb_serial *serial;
  805. int divisor;
  806. int status;
  807. unsigned char data;
  808. unsigned char number;
  809. if (mos7720_port == NULL)
  810. return -1;
  811. port = mos7720_port->port;
  812. serial = port->serial;
  813. dbg("%s: Entering ..........", __FUNCTION__);
  814. number = port->number - port->serial->minor;
  815. dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
  816. /* Calculate the Divisor */
  817. status = calc_baud_rate_divisor(baudrate, &divisor);
  818. if (status) {
  819. err("%s - bad baud rate", __FUNCTION__);
  820. return status;
  821. }
  822. /* Enable access to divisor latch */
  823. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  824. mos7720_port->shadowLCR = data;
  825. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  826. /* Write the divisor */
  827. data = ((unsigned char)(divisor & 0xff));
  828. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  829. data = ((unsigned char)((divisor & 0xff00) >> 8));
  830. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  831. /* Disable access to divisor latch */
  832. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  833. mos7720_port->shadowLCR = data;
  834. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  835. return status;
  836. }
  837. /*
  838. * change_port_settings
  839. * This routine is called to set the UART on the device to match
  840. * the specified new settings.
  841. */
  842. static void change_port_settings(struct moschip_port *mos7720_port,
  843. struct ktermios *old_termios)
  844. {
  845. struct usb_serial_port *port;
  846. struct usb_serial *serial;
  847. struct tty_struct *tty;
  848. int baud;
  849. unsigned cflag;
  850. unsigned iflag;
  851. __u8 mask = 0xff;
  852. __u8 lData;
  853. __u8 lParity;
  854. __u8 lStop;
  855. int status;
  856. int port_number;
  857. char data;
  858. if (mos7720_port == NULL)
  859. return ;
  860. port = mos7720_port->port;
  861. serial = port->serial;
  862. port_number = port->number - port->serial->minor;
  863. dbg("%s - port %d", __FUNCTION__, port->number);
  864. if (!mos7720_port->open) {
  865. dbg("%s - port not opened", __FUNCTION__);
  866. return;
  867. }
  868. tty = mos7720_port->port->tty;
  869. if ((!tty) || (!tty->termios)) {
  870. dbg("%s - no tty structures", __FUNCTION__);
  871. return;
  872. }
  873. dbg("%s: Entering ..........", __FUNCTION__);
  874. lData = UART_LCR_WLEN8;
  875. lStop = 0x00; /* 1 stop bit */
  876. lParity = 0x00; /* No parity */
  877. cflag = tty->termios->c_cflag;
  878. iflag = tty->termios->c_iflag;
  879. /* Change the number of bits */
  880. switch (cflag & CSIZE) {
  881. case CS5:
  882. lData = UART_LCR_WLEN5;
  883. mask = 0x1f;
  884. break;
  885. case CS6:
  886. lData = UART_LCR_WLEN6;
  887. mask = 0x3f;
  888. break;
  889. case CS7:
  890. lData = UART_LCR_WLEN7;
  891. mask = 0x7f;
  892. break;
  893. default:
  894. case CS8:
  895. lData = UART_LCR_WLEN8;
  896. break;
  897. }
  898. /* Change the Parity bit */
  899. if (cflag & PARENB) {
  900. if (cflag & PARODD) {
  901. lParity = UART_LCR_PARITY;
  902. dbg("%s - parity = odd", __FUNCTION__);
  903. } else {
  904. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  905. dbg("%s - parity = even", __FUNCTION__);
  906. }
  907. } else {
  908. dbg("%s - parity = none", __FUNCTION__);
  909. }
  910. if (cflag & CMSPAR)
  911. lParity = lParity | 0x20;
  912. /* Change the Stop bit */
  913. if (cflag & CSTOPB) {
  914. lStop = UART_LCR_STOP;
  915. dbg("%s - stop bits = 2", __FUNCTION__);
  916. } else {
  917. lStop = 0x00;
  918. dbg("%s - stop bits = 1", __FUNCTION__);
  919. }
  920. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  921. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  922. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  923. /* Update the LCR with the correct value */
  924. mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  925. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  926. /* Disable Interrupts */
  927. data = 0x00;
  928. send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
  929. data = 0x00;
  930. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  931. data = 0xcf;
  932. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  933. /* Send the updated LCR value to the mos7720 */
  934. data = mos7720_port->shadowLCR;
  935. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  936. data = 0x00b;
  937. mos7720_port->shadowMCR = data;
  938. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  939. data = 0x00b;
  940. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  941. /* set up the MCR register and send it to the mos7720 */
  942. mos7720_port->shadowMCR = UART_MCR_OUT2;
  943. if (cflag & CBAUD)
  944. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  945. if (cflag & CRTSCTS) {
  946. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  947. /* To set hardware flow control to the specified *
  948. * serial port, in SP1/2_CONTROL_REG */
  949. if (port->number) {
  950. data = 0x001;
  951. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  952. 0x08, &data);
  953. } else {
  954. data = 0x002;
  955. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  956. 0x08, &data);
  957. }
  958. } else {
  959. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  960. }
  961. data = mos7720_port->shadowMCR;
  962. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  963. /* Determine divisor based on baud rate */
  964. baud = tty_get_baud_rate(tty);
  965. if (!baud) {
  966. /* pick a default, any default... */
  967. dbg("Picked default baud...");
  968. baud = 9600;
  969. }
  970. if (baud >= 230400) {
  971. set_higher_rates(mos7720_port, baud);
  972. /* Enable Interrupts */
  973. data = 0x0c;
  974. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  975. return;
  976. }
  977. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  978. status = send_cmd_write_baud_rate(mos7720_port, baud);
  979. /* Enable Interrupts */
  980. data = 0x0c;
  981. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  982. if (port->read_urb->status != -EINPROGRESS) {
  983. port->read_urb->dev = serial->dev;
  984. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  985. if (status)
  986. dbg("usb_submit_urb(read bulk) failed, status = %d",
  987. status);
  988. }
  989. return;
  990. }
  991. /*
  992. * mos7720_set_termios
  993. * this function is called by the tty driver when it wants to change the
  994. * termios structure.
  995. */
  996. static void mos7720_set_termios(struct usb_serial_port *port,
  997. struct ktermios *old_termios)
  998. {
  999. int status;
  1000. unsigned int cflag;
  1001. struct usb_serial *serial;
  1002. struct moschip_port *mos7720_port;
  1003. struct tty_struct *tty;
  1004. serial = port->serial;
  1005. mos7720_port = usb_get_serial_port_data(port);
  1006. if (mos7720_port == NULL)
  1007. return;
  1008. tty = port->tty;
  1009. if (!port->tty || !port->tty->termios) {
  1010. dbg("%s - no tty or termios", __FUNCTION__);
  1011. return;
  1012. }
  1013. if (!mos7720_port->open) {
  1014. dbg("%s - port not opened", __FUNCTION__);
  1015. return;
  1016. }
  1017. dbg("%s\n","setting termios - ASPIRE");
  1018. cflag = tty->termios->c_cflag;
  1019. if (!cflag) {
  1020. printk("%s %s\n",__FUNCTION__,"cflag is NULL");
  1021. return;
  1022. }
  1023. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1024. tty->termios->c_cflag,
  1025. RELEVANT_IFLAG(tty->termios->c_iflag));
  1026. if (old_termios)
  1027. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1028. old_termios->c_cflag,
  1029. RELEVANT_IFLAG(old_termios->c_iflag));
  1030. dbg("%s - port %d", __FUNCTION__, port->number);
  1031. /* change the port settings to the new ones specified */
  1032. change_port_settings(mos7720_port, old_termios);
  1033. if(!port->read_urb) {
  1034. dbg("%s","URB KILLED !!!!!\n");
  1035. return;
  1036. }
  1037. if(port->read_urb->status != -EINPROGRESS) {
  1038. port->read_urb->dev = serial->dev;
  1039. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1040. if (status)
  1041. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1042. status);
  1043. }
  1044. return;
  1045. }
  1046. /*
  1047. * get_lsr_info - get line status register info
  1048. *
  1049. * Purpose: Let user call ioctl() to get info when the UART physically
  1050. * is emptied. On bus types like RS485, the transmitter must
  1051. * release the bus after transmitting. This must be done when
  1052. * the transmit shift register is empty, not be done when the
  1053. * transmit holding register is empty. This functionality
  1054. * allows an RS485 driver to be written in user space.
  1055. */
  1056. static int get_lsr_info(struct moschip_port *mos7720_port,
  1057. unsigned int __user *value)
  1058. {
  1059. int count;
  1060. unsigned int result = 0;
  1061. count = mos7720_chars_in_buffer(mos7720_port->port);
  1062. if (count == 0) {
  1063. dbg("%s -- Empty", __FUNCTION__);
  1064. result = TIOCSER_TEMT;
  1065. }
  1066. if (copy_to_user(value, &result, sizeof(int)))
  1067. return -EFAULT;
  1068. return 0;
  1069. }
  1070. /*
  1071. * get_number_bytes_avail - get number of bytes available
  1072. *
  1073. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1074. */
  1075. static int get_number_bytes_avail(struct moschip_port *mos7720_port,
  1076. unsigned int __user *value)
  1077. {
  1078. unsigned int result = 0;
  1079. struct tty_struct *tty = mos7720_port->port->tty;
  1080. if (!tty)
  1081. return -ENOIOCTLCMD;
  1082. result = tty->read_cnt;
  1083. dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
  1084. if (copy_to_user(value, &result, sizeof(int)))
  1085. return -EFAULT;
  1086. return -ENOIOCTLCMD;
  1087. }
  1088. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1089. unsigned int __user *value)
  1090. {
  1091. unsigned int mcr ;
  1092. unsigned int arg;
  1093. unsigned char data;
  1094. struct usb_serial_port *port;
  1095. if (mos7720_port == NULL)
  1096. return -1;
  1097. port = (struct usb_serial_port*)mos7720_port->port;
  1098. mcr = mos7720_port->shadowMCR;
  1099. if (copy_from_user(&arg, value, sizeof(int)))
  1100. return -EFAULT;
  1101. switch (cmd) {
  1102. case TIOCMBIS:
  1103. if (arg & TIOCM_RTS)
  1104. mcr |= UART_MCR_RTS;
  1105. if (arg & TIOCM_DTR)
  1106. mcr |= UART_MCR_RTS;
  1107. if (arg & TIOCM_LOOP)
  1108. mcr |= UART_MCR_LOOP;
  1109. break;
  1110. case TIOCMBIC:
  1111. if (arg & TIOCM_RTS)
  1112. mcr &= ~UART_MCR_RTS;
  1113. if (arg & TIOCM_DTR)
  1114. mcr &= ~UART_MCR_RTS;
  1115. if (arg & TIOCM_LOOP)
  1116. mcr &= ~UART_MCR_LOOP;
  1117. break;
  1118. case TIOCMSET:
  1119. /* turn off the RTS and DTR and LOOPBACK
  1120. * and then only turn on what was asked to */
  1121. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1122. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1123. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1124. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1125. break;
  1126. }
  1127. mos7720_port->shadowMCR = mcr;
  1128. data = mos7720_port->shadowMCR;
  1129. send_mos_cmd(port->serial, MOS_WRITE,
  1130. port->number - port->serial->minor, UART_MCR, &data);
  1131. return 0;
  1132. }
  1133. static int get_modem_info(struct moschip_port *mos7720_port,
  1134. unsigned int __user *value)
  1135. {
  1136. unsigned int result = 0;
  1137. unsigned int msr = mos7720_port->shadowMSR;
  1138. unsigned int mcr = mos7720_port->shadowMCR;
  1139. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1140. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1141. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1142. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1143. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1144. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1145. dbg("%s -- %x", __FUNCTION__, result);
  1146. if (copy_to_user(value, &result, sizeof(int)))
  1147. return -EFAULT;
  1148. return 0;
  1149. }
  1150. static int get_serial_info(struct moschip_port *mos7720_port,
  1151. struct serial_struct __user *retinfo)
  1152. {
  1153. struct serial_struct tmp;
  1154. if (!retinfo)
  1155. return -EFAULT;
  1156. memset(&tmp, 0, sizeof(tmp));
  1157. tmp.type = PORT_16550A;
  1158. tmp.line = mos7720_port->port->serial->minor;
  1159. tmp.port = mos7720_port->port->number;
  1160. tmp.irq = 0;
  1161. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1162. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1163. tmp.baud_base = 9600;
  1164. tmp.close_delay = 5*HZ;
  1165. tmp.closing_wait = 30*HZ;
  1166. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1167. return -EFAULT;
  1168. return 0;
  1169. }
  1170. static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
  1171. unsigned int cmd, unsigned long arg)
  1172. {
  1173. struct moschip_port *mos7720_port;
  1174. struct async_icount cnow;
  1175. struct async_icount cprev;
  1176. struct serial_icounter_struct icount;
  1177. mos7720_port = usb_get_serial_port_data(port);
  1178. if (mos7720_port == NULL)
  1179. return -ENODEV;
  1180. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  1181. switch (cmd) {
  1182. case TIOCINQ:
  1183. /* return number of bytes available */
  1184. dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
  1185. return get_number_bytes_avail(mos7720_port,
  1186. (unsigned int __user *)arg);
  1187. break;
  1188. case TIOCSERGETLSR:
  1189. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  1190. return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
  1191. return 0;
  1192. case TIOCMBIS:
  1193. case TIOCMBIC:
  1194. case TIOCMSET:
  1195. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
  1196. port->number);
  1197. return set_modem_info(mos7720_port, cmd,
  1198. (unsigned int __user *)arg);
  1199. case TIOCMGET:
  1200. dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
  1201. return get_modem_info(mos7720_port,
  1202. (unsigned int __user *)arg);
  1203. case TIOCGSERIAL:
  1204. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  1205. return get_serial_info(mos7720_port,
  1206. (struct serial_struct __user *)arg);
  1207. case TIOCSSERIAL:
  1208. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  1209. break;
  1210. case TIOCMIWAIT:
  1211. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  1212. cprev = mos7720_port->icount;
  1213. while (1) {
  1214. if (signal_pending(current))
  1215. return -ERESTARTSYS;
  1216. cnow = mos7720_port->icount;
  1217. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1218. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1219. return -EIO; /* no change => error */
  1220. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1221. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1222. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1223. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1224. return 0;
  1225. }
  1226. cprev = cnow;
  1227. }
  1228. /* NOTREACHED */
  1229. break;
  1230. case TIOCGICOUNT:
  1231. cnow = mos7720_port->icount;
  1232. icount.cts = cnow.cts;
  1233. icount.dsr = cnow.dsr;
  1234. icount.rng = cnow.rng;
  1235. icount.dcd = cnow.dcd;
  1236. icount.rx = cnow.rx;
  1237. icount.tx = cnow.tx;
  1238. icount.frame = cnow.frame;
  1239. icount.overrun = cnow.overrun;
  1240. icount.parity = cnow.parity;
  1241. icount.brk = cnow.brk;
  1242. icount.buf_overrun = cnow.buf_overrun;
  1243. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  1244. port->number, icount.rx, icount.tx );
  1245. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1246. return -EFAULT;
  1247. return 0;
  1248. }
  1249. return -ENOIOCTLCMD;
  1250. }
  1251. static int mos7720_startup(struct usb_serial *serial)
  1252. {
  1253. struct moschip_serial *mos7720_serial;
  1254. struct moschip_port *mos7720_port;
  1255. struct usb_device *dev;
  1256. int i;
  1257. char data;
  1258. dbg("%s: Entering ..........", __FUNCTION__);
  1259. if (!serial) {
  1260. dbg("Invalid Handler");
  1261. return -ENODEV;
  1262. }
  1263. dev = serial->dev;
  1264. /* create our private serial structure */
  1265. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1266. if (mos7720_serial == NULL) {
  1267. err("%s - Out of memory", __FUNCTION__);
  1268. return -ENOMEM;
  1269. }
  1270. usb_set_serial_data(serial, mos7720_serial);
  1271. /* we set up the pointers to the endpoints in the mos7720_open *
  1272. * function, as the structures aren't created yet. */
  1273. /* set up port private structures */
  1274. for (i = 0; i < serial->num_ports; ++i) {
  1275. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1276. if (mos7720_port == NULL) {
  1277. err("%s - Out of memory", __FUNCTION__);
  1278. usb_set_serial_data(serial, NULL);
  1279. kfree(mos7720_serial);
  1280. return -ENOMEM;
  1281. }
  1282. /* Initialize all port interrupt end point to port 0 int
  1283. * endpoint. Our device has only one interrupt endpoint
  1284. * comman to all ports */
  1285. serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
  1286. mos7720_port->port = serial->port[i];
  1287. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1288. dbg("port number is %d", serial->port[i]->number);
  1289. dbg("serial number is %d", serial->minor);
  1290. }
  1291. /* setting configuration feature to one */
  1292. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1293. (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
  1294. send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
  1295. dbg("LSR:%x",data);
  1296. send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
  1297. dbg("LSR:%x",data);
  1298. return 0;
  1299. }
  1300. static void mos7720_shutdown(struct usb_serial *serial)
  1301. {
  1302. int i;
  1303. /* free private structure allocated for serial port */
  1304. for (i=0; i < serial->num_ports; ++i) {
  1305. kfree(usb_get_serial_port_data(serial->port[i]));
  1306. usb_set_serial_port_data(serial->port[i], NULL);
  1307. }
  1308. /* free private structure allocated for serial device */
  1309. kfree(usb_get_serial_data(serial));
  1310. usb_set_serial_data(serial, NULL);
  1311. }
  1312. static struct usb_driver usb_driver = {
  1313. .name = "moschip7720",
  1314. .probe = usb_serial_probe,
  1315. .disconnect = usb_serial_disconnect,
  1316. .id_table = moschip_port_id_table,
  1317. .no_dynamic_id = 1,
  1318. };
  1319. static struct usb_serial_driver moschip7720_2port_driver = {
  1320. .driver = {
  1321. .owner = THIS_MODULE,
  1322. .name = "moschip7720",
  1323. },
  1324. .description = "Moschip 2 port adapter",
  1325. .usb_driver = &usb_driver,
  1326. .id_table = moschip_port_id_table,
  1327. .num_interrupt_in = 1,
  1328. .num_bulk_in = 2,
  1329. .num_bulk_out = 2,
  1330. .num_ports = 2,
  1331. .open = mos7720_open,
  1332. .close = mos7720_close,
  1333. .throttle = mos7720_throttle,
  1334. .unthrottle = mos7720_unthrottle,
  1335. .attach = mos7720_startup,
  1336. .shutdown = mos7720_shutdown,
  1337. .ioctl = mos7720_ioctl,
  1338. .set_termios = mos7720_set_termios,
  1339. .write = mos7720_write,
  1340. .write_room = mos7720_write_room,
  1341. .chars_in_buffer = mos7720_chars_in_buffer,
  1342. .break_ctl = mos7720_break,
  1343. .read_bulk_callback = mos7720_bulk_in_callback,
  1344. .read_int_callback = mos7720_interrupt_callback,
  1345. };
  1346. static int __init moschip7720_init(void)
  1347. {
  1348. int retval;
  1349. dbg("%s: Entering ..........", __FUNCTION__);
  1350. /* Register with the usb serial */
  1351. retval = usb_serial_register(&moschip7720_2port_driver);
  1352. if (retval)
  1353. goto failed_port_device_register;
  1354. info(DRIVER_DESC " " DRIVER_VERSION);
  1355. /* Register with the usb */
  1356. retval = usb_register(&usb_driver);
  1357. if (retval)
  1358. goto failed_usb_register;
  1359. return 0;
  1360. failed_usb_register:
  1361. usb_serial_deregister(&moschip7720_2port_driver);
  1362. failed_port_device_register:
  1363. return retval;
  1364. }
  1365. static void __exit moschip7720_exit(void)
  1366. {
  1367. usb_deregister(&usb_driver);
  1368. usb_serial_deregister(&moschip7720_2port_driver);
  1369. }
  1370. module_init(moschip7720_init);
  1371. module_exit(moschip7720_exit);
  1372. /* Module information */
  1373. MODULE_AUTHOR( DRIVER_AUTHOR );
  1374. MODULE_DESCRIPTION( DRIVER_DESC );
  1375. MODULE_LICENSE("GPL");
  1376. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1377. MODULE_PARM_DESC(debug, "Debug enabled or not");