hso.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  9. * <ajb@spheresystems.co.uk>
  10. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  11. * Copyright (C) 2008 Novell, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  25. * USA
  26. *
  27. *
  28. *****************************************************************************/
  29. /******************************************************************************
  30. *
  31. * Description of the device:
  32. *
  33. * Interface 0: Contains the IP network interface on the bulk end points.
  34. * The multiplexed serial ports are using the interrupt and
  35. * control endpoints.
  36. * Interrupt contains a bitmap telling which multiplexed
  37. * serialport needs servicing.
  38. *
  39. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  40. * port is opened, as this have a huge impact on the network port
  41. * throughput.
  42. *
  43. * Interface 2: Standard modem interface - circuit switched interface, this
  44. * can be used to make a standard ppp connection however it
  45. * should not be used in conjunction with the IP network interface
  46. * enabled for USB performance reasons i.e. if using this set
  47. * ideally disable_net=1.
  48. *
  49. *****************************************************************************/
  50. #include <linux/sched.h>
  51. #include <linux/slab.h>
  52. #include <linux/init.h>
  53. #include <linux/delay.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/module.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/usb.h>
  58. #include <linux/timer.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/kmod.h>
  63. #include <linux/rfkill.h>
  64. #include <linux/ip.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/usb/cdc.h>
  67. #include <net/arp.h>
  68. #include <asm/byteorder.h>
  69. #include <linux/serial_core.h>
  70. #include <linux/serial.h>
  71. #define DRIVER_VERSION "1.2"
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __attribute__((packed));
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct usb_device *usb;
  245. struct usb_interface *interface;
  246. struct device *dev;
  247. struct kref ref;
  248. struct mutex mutex;
  249. };
  250. /* Type of interface */
  251. #define HSO_INTF_MASK 0xFF00
  252. #define HSO_INTF_MUX 0x0100
  253. #define HSO_INTF_BULK 0x0200
  254. /* Type of port */
  255. #define HSO_PORT_MASK 0xFF
  256. #define HSO_PORT_NO_PORT 0x0
  257. #define HSO_PORT_CONTROL 0x1
  258. #define HSO_PORT_APP 0x2
  259. #define HSO_PORT_GPS 0x3
  260. #define HSO_PORT_PCSC 0x4
  261. #define HSO_PORT_APP2 0x5
  262. #define HSO_PORT_GPS_CONTROL 0x6
  263. #define HSO_PORT_MSD 0x7
  264. #define HSO_PORT_VOICE 0x8
  265. #define HSO_PORT_DIAG2 0x9
  266. #define HSO_PORT_DIAG 0x10
  267. #define HSO_PORT_MODEM 0x11
  268. #define HSO_PORT_NETWORK 0x12
  269. /* Additional device info */
  270. #define HSO_INFO_MASK 0xFF000000
  271. #define HSO_INFO_CRC_BUG 0x01000000
  272. /*****************************************************************************/
  273. /* Prototypes */
  274. /*****************************************************************************/
  275. /* Serial driver functions */
  276. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  277. unsigned int set, unsigned int clear);
  278. static void ctrl_callback(struct urb *urb);
  279. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  280. static void hso_kick_transmit(struct hso_serial *serial);
  281. /* Helper functions */
  282. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  283. struct usb_device *usb, gfp_t gfp);
  284. static void log_usb_status(int status, const char *function);
  285. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  286. int type, int dir);
  287. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  288. static void hso_free_interface(struct usb_interface *intf);
  289. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  290. static int hso_stop_serial_device(struct hso_device *hso_dev);
  291. static int hso_start_net_device(struct hso_device *hso_dev);
  292. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  293. static int hso_stop_net_device(struct hso_device *hso_dev);
  294. static void hso_serial_ref_free(struct kref *ref);
  295. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  296. static int hso_mux_serial_read(struct hso_serial *serial);
  297. static void async_get_intf(struct work_struct *data);
  298. static void async_put_intf(struct work_struct *data);
  299. static int hso_put_activity(struct hso_device *hso_dev);
  300. static int hso_get_activity(struct hso_device *hso_dev);
  301. static void tiocmget_intr_callback(struct urb *urb);
  302. /*****************************************************************************/
  303. /* Helping functions */
  304. /*****************************************************************************/
  305. /* #define DEBUG */
  306. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  307. {
  308. return hso_dev->port_data.dev_net;
  309. }
  310. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  311. {
  312. return hso_dev->port_data.dev_serial;
  313. }
  314. /* Debugging functions */
  315. #ifdef DEBUG
  316. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  317. unsigned int len)
  318. {
  319. static char name[255];
  320. sprintf(name, "hso[%d:%s]", line_count, func_name);
  321. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  322. }
  323. #define DUMP(buf_, len_) \
  324. dbg_dump(__LINE__, __func__, buf_, len_)
  325. #define DUMP1(buf_, len_) \
  326. do { \
  327. if (0x01 & debug) \
  328. DUMP(buf_, len_); \
  329. } while (0)
  330. #else
  331. #define DUMP(buf_, len_)
  332. #define DUMP1(buf_, len_)
  333. #endif
  334. /* module parameters */
  335. static int debug;
  336. static int tty_major;
  337. static int disable_net;
  338. /* driver info */
  339. static const char driver_name[] = "hso";
  340. static const char tty_filename[] = "ttyHS";
  341. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  342. /* the usb driver itself (registered in hso_init) */
  343. static struct usb_driver hso_driver;
  344. /* serial structures */
  345. static struct tty_driver *tty_drv;
  346. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  347. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  348. static spinlock_t serial_table_lock;
  349. static const s32 default_port_spec[] = {
  350. HSO_INTF_MUX | HSO_PORT_NETWORK,
  351. HSO_INTF_BULK | HSO_PORT_DIAG,
  352. HSO_INTF_BULK | HSO_PORT_MODEM,
  353. 0
  354. };
  355. static const s32 icon321_port_spec[] = {
  356. HSO_INTF_MUX | HSO_PORT_NETWORK,
  357. HSO_INTF_BULK | HSO_PORT_DIAG2,
  358. HSO_INTF_BULK | HSO_PORT_MODEM,
  359. HSO_INTF_BULK | HSO_PORT_DIAG,
  360. 0
  361. };
  362. #define default_port_device(vendor, product) \
  363. USB_DEVICE(vendor, product), \
  364. .driver_info = (kernel_ulong_t)default_port_spec
  365. #define icon321_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)icon321_port_spec
  368. /* list of devices we support */
  369. static const struct usb_device_id hso_ids[] = {
  370. {default_port_device(0x0af0, 0x6711)},
  371. {default_port_device(0x0af0, 0x6731)},
  372. {default_port_device(0x0af0, 0x6751)},
  373. {default_port_device(0x0af0, 0x6771)},
  374. {default_port_device(0x0af0, 0x6791)},
  375. {default_port_device(0x0af0, 0x6811)},
  376. {default_port_device(0x0af0, 0x6911)},
  377. {default_port_device(0x0af0, 0x6951)},
  378. {default_port_device(0x0af0, 0x6971)},
  379. {default_port_device(0x0af0, 0x7011)},
  380. {default_port_device(0x0af0, 0x7031)},
  381. {default_port_device(0x0af0, 0x7051)},
  382. {default_port_device(0x0af0, 0x7071)},
  383. {default_port_device(0x0af0, 0x7111)},
  384. {default_port_device(0x0af0, 0x7211)},
  385. {default_port_device(0x0af0, 0x7251)},
  386. {default_port_device(0x0af0, 0x7271)},
  387. {default_port_device(0x0af0, 0x7311)},
  388. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  389. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  390. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  391. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  392. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  393. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  395. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  396. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  397. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7701)},
  399. {USB_DEVICE(0x0af0, 0x7801)},
  400. {USB_DEVICE(0x0af0, 0x7901)},
  401. {USB_DEVICE(0x0af0, 0x7361)},
  402. {USB_DEVICE(0x0af0, 0xd057)},
  403. {USB_DEVICE(0x0af0, 0xd055)},
  404. {}
  405. };
  406. MODULE_DEVICE_TABLE(usb, hso_ids);
  407. /* Sysfs attribute */
  408. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  409. struct device_attribute *attr,
  410. char *buf)
  411. {
  412. struct hso_device *hso_dev = dev->driver_data;
  413. char *port_name;
  414. if (!hso_dev)
  415. return 0;
  416. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  417. case HSO_PORT_CONTROL:
  418. port_name = "Control";
  419. break;
  420. case HSO_PORT_APP:
  421. port_name = "Application";
  422. break;
  423. case HSO_PORT_APP2:
  424. port_name = "Application2";
  425. break;
  426. case HSO_PORT_GPS:
  427. port_name = "GPS";
  428. break;
  429. case HSO_PORT_GPS_CONTROL:
  430. port_name = "GPS Control";
  431. break;
  432. case HSO_PORT_PCSC:
  433. port_name = "PCSC";
  434. break;
  435. case HSO_PORT_DIAG:
  436. port_name = "Diagnostic";
  437. break;
  438. case HSO_PORT_DIAG2:
  439. port_name = "Diagnostic2";
  440. break;
  441. case HSO_PORT_MODEM:
  442. port_name = "Modem";
  443. break;
  444. case HSO_PORT_NETWORK:
  445. port_name = "Network";
  446. break;
  447. default:
  448. port_name = "Unknown";
  449. break;
  450. }
  451. return sprintf(buf, "%s\n", port_name);
  452. }
  453. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  454. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  455. {
  456. int idx;
  457. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  458. if (serial->rx_urb[idx] == urb)
  459. return idx;
  460. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  461. return -1;
  462. }
  463. /* converts mux value to a port spec value */
  464. static u32 hso_mux_to_port(int mux)
  465. {
  466. u32 result;
  467. switch (mux) {
  468. case 0x1:
  469. result = HSO_PORT_CONTROL;
  470. break;
  471. case 0x2:
  472. result = HSO_PORT_APP;
  473. break;
  474. case 0x4:
  475. result = HSO_PORT_PCSC;
  476. break;
  477. case 0x8:
  478. result = HSO_PORT_GPS;
  479. break;
  480. case 0x10:
  481. result = HSO_PORT_APP2;
  482. break;
  483. default:
  484. result = HSO_PORT_NO_PORT;
  485. }
  486. return result;
  487. }
  488. /* converts port spec value to a mux value */
  489. static u32 hso_port_to_mux(int port)
  490. {
  491. u32 result;
  492. switch (port & HSO_PORT_MASK) {
  493. case HSO_PORT_CONTROL:
  494. result = 0x0;
  495. break;
  496. case HSO_PORT_APP:
  497. result = 0x1;
  498. break;
  499. case HSO_PORT_PCSC:
  500. result = 0x2;
  501. break;
  502. case HSO_PORT_GPS:
  503. result = 0x3;
  504. break;
  505. case HSO_PORT_APP2:
  506. result = 0x4;
  507. break;
  508. default:
  509. result = 0x0;
  510. }
  511. return result;
  512. }
  513. static struct hso_serial *get_serial_by_shared_int_and_type(
  514. struct hso_shared_int *shared_int,
  515. int mux)
  516. {
  517. int i, port;
  518. port = hso_mux_to_port(mux);
  519. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  520. if (serial_table[i]
  521. && (dev2ser(serial_table[i])->shared_int == shared_int)
  522. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  523. return dev2ser(serial_table[i]);
  524. }
  525. }
  526. return NULL;
  527. }
  528. static struct hso_serial *get_serial_by_index(unsigned index)
  529. {
  530. struct hso_serial *serial = NULL;
  531. unsigned long flags;
  532. spin_lock_irqsave(&serial_table_lock, flags);
  533. if (serial_table[index])
  534. serial = dev2ser(serial_table[index]);
  535. spin_unlock_irqrestore(&serial_table_lock, flags);
  536. return serial;
  537. }
  538. static int get_free_serial_index(void)
  539. {
  540. int index;
  541. unsigned long flags;
  542. spin_lock_irqsave(&serial_table_lock, flags);
  543. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  544. if (serial_table[index] == NULL) {
  545. spin_unlock_irqrestore(&serial_table_lock, flags);
  546. return index;
  547. }
  548. }
  549. spin_unlock_irqrestore(&serial_table_lock, flags);
  550. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  551. return -1;
  552. }
  553. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&serial_table_lock, flags);
  557. if (serial)
  558. serial_table[index] = serial->parent;
  559. else
  560. serial_table[index] = NULL;
  561. spin_unlock_irqrestore(&serial_table_lock, flags);
  562. }
  563. /* log a meaningful explanation of an USB status */
  564. static void log_usb_status(int status, const char *function)
  565. {
  566. char *explanation;
  567. switch (status) {
  568. case -ENODEV:
  569. explanation = "no device";
  570. break;
  571. case -ENOENT:
  572. explanation = "endpoint not enabled";
  573. break;
  574. case -EPIPE:
  575. explanation = "endpoint stalled";
  576. break;
  577. case -ENOSPC:
  578. explanation = "not enough bandwidth";
  579. break;
  580. case -ESHUTDOWN:
  581. explanation = "device disabled";
  582. break;
  583. case -EHOSTUNREACH:
  584. explanation = "device suspended";
  585. break;
  586. case -EINVAL:
  587. case -EAGAIN:
  588. case -EFBIG:
  589. case -EMSGSIZE:
  590. explanation = "internal error";
  591. break;
  592. default:
  593. explanation = "unknown status";
  594. break;
  595. }
  596. D1("%s: received USB status - %s (%d)", function, explanation, status);
  597. }
  598. /* Network interface functions */
  599. /* called when net interface is brought up by ifconfig */
  600. static int hso_net_open(struct net_device *net)
  601. {
  602. struct hso_net *odev = netdev_priv(net);
  603. unsigned long flags = 0;
  604. if (!odev) {
  605. dev_err(&net->dev, "No net device !\n");
  606. return -ENODEV;
  607. }
  608. odev->skb_tx_buf = NULL;
  609. /* setup environment */
  610. spin_lock_irqsave(&odev->net_lock, flags);
  611. odev->rx_parse_state = WAIT_IP;
  612. odev->rx_buf_size = 0;
  613. odev->rx_buf_missing = sizeof(struct iphdr);
  614. spin_unlock_irqrestore(&odev->net_lock, flags);
  615. /* We are up and running. */
  616. set_bit(HSO_NET_RUNNING, &odev->flags);
  617. hso_start_net_device(odev->parent);
  618. /* Tell the kernel we are ready to start receiving from it */
  619. netif_start_queue(net);
  620. return 0;
  621. }
  622. /* called when interface is brought down by ifconfig */
  623. static int hso_net_close(struct net_device *net)
  624. {
  625. struct hso_net *odev = netdev_priv(net);
  626. /* we don't need the queue anymore */
  627. netif_stop_queue(net);
  628. /* no longer running */
  629. clear_bit(HSO_NET_RUNNING, &odev->flags);
  630. hso_stop_net_device(odev->parent);
  631. /* done */
  632. return 0;
  633. }
  634. /* USB tells is xmit done, we should start the netqueue again */
  635. static void write_bulk_callback(struct urb *urb)
  636. {
  637. struct hso_net *odev = urb->context;
  638. int status = urb->status;
  639. /* Sanity check */
  640. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  641. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  642. return;
  643. }
  644. /* Do we still have a valid kernel network device? */
  645. if (!netif_device_present(odev->net)) {
  646. dev_err(&urb->dev->dev, "%s: net device not present\n",
  647. __func__);
  648. return;
  649. }
  650. /* log status, but don't act on it, we don't need to resubmit anything
  651. * anyhow */
  652. if (status)
  653. log_usb_status(status, __func__);
  654. hso_put_activity(odev->parent);
  655. /* Tell the network interface we are ready for another frame */
  656. netif_wake_queue(odev->net);
  657. }
  658. /* called by kernel when we need to transmit a packet */
  659. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  660. {
  661. struct hso_net *odev = netdev_priv(net);
  662. int result;
  663. /* Tell the kernel, "No more frames 'til we are done with this one." */
  664. netif_stop_queue(net);
  665. if (hso_get_activity(odev->parent) == -EAGAIN) {
  666. odev->skb_tx_buf = skb;
  667. return 0;
  668. }
  669. /* log if asked */
  670. DUMP1(skb->data, skb->len);
  671. /* Copy it from kernel memory to OUR memory */
  672. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  673. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  674. /* Fill in the URB for shipping it out. */
  675. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  676. odev->parent->usb,
  677. usb_sndbulkpipe(odev->parent->usb,
  678. odev->out_endp->
  679. bEndpointAddress & 0x7F),
  680. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  681. odev);
  682. /* Deal with the Zero Length packet problem, I hope */
  683. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  684. /* Send the URB on its merry way. */
  685. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  686. if (result) {
  687. dev_warn(&odev->parent->interface->dev,
  688. "failed mux_bulk_tx_urb %d", result);
  689. net->stats.tx_errors++;
  690. netif_start_queue(net);
  691. } else {
  692. net->stats.tx_packets++;
  693. net->stats.tx_bytes += skb->len;
  694. /* And tell the kernel when the last transmit started. */
  695. net->trans_start = jiffies;
  696. }
  697. dev_kfree_skb(skb);
  698. /* we're done */
  699. return result;
  700. }
  701. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  702. {
  703. struct hso_net *odev = netdev_priv(net);
  704. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  705. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  706. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  707. }
  708. static struct ethtool_ops ops = {
  709. .get_drvinfo = hso_get_drvinfo,
  710. .get_link = ethtool_op_get_link
  711. };
  712. /* called when a packet did not ack after watchdogtimeout */
  713. static void hso_net_tx_timeout(struct net_device *net)
  714. {
  715. struct hso_net *odev = netdev_priv(net);
  716. if (!odev)
  717. return;
  718. /* Tell syslog we are hosed. */
  719. dev_warn(&net->dev, "Tx timed out.\n");
  720. /* Tear the waiting frame off the list */
  721. if (odev->mux_bulk_tx_urb
  722. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  723. usb_unlink_urb(odev->mux_bulk_tx_urb);
  724. /* Update statistics */
  725. net->stats.tx_errors++;
  726. }
  727. /* make a real packet from the received USB buffer */
  728. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  729. unsigned int count, unsigned char is_eop)
  730. {
  731. unsigned short temp_bytes;
  732. unsigned short buffer_offset = 0;
  733. unsigned short frame_len;
  734. unsigned char *tmp_rx_buf;
  735. /* log if needed */
  736. D1("Rx %d bytes", count);
  737. DUMP(ip_pkt, min(128, (int)count));
  738. while (count) {
  739. switch (odev->rx_parse_state) {
  740. case WAIT_IP:
  741. /* waiting for IP header. */
  742. /* wanted bytes - size of ip header */
  743. temp_bytes =
  744. (count <
  745. odev->rx_buf_missing) ? count : odev->
  746. rx_buf_missing;
  747. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  748. odev->rx_buf_size, ip_pkt + buffer_offset,
  749. temp_bytes);
  750. odev->rx_buf_size += temp_bytes;
  751. buffer_offset += temp_bytes;
  752. odev->rx_buf_missing -= temp_bytes;
  753. count -= temp_bytes;
  754. if (!odev->rx_buf_missing) {
  755. /* header is complete allocate an sk_buffer and
  756. * continue to WAIT_DATA */
  757. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  758. if ((frame_len > DEFAULT_MRU) ||
  759. (frame_len < sizeof(struct iphdr))) {
  760. dev_err(&odev->net->dev,
  761. "Invalid frame (%d) length\n",
  762. frame_len);
  763. odev->rx_parse_state = WAIT_SYNC;
  764. continue;
  765. }
  766. /* Allocate an sk_buff */
  767. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  768. if (!odev->skb_rx_buf) {
  769. /* We got no receive buffer. */
  770. D1("could not allocate memory");
  771. odev->rx_parse_state = WAIT_SYNC;
  772. return;
  773. }
  774. /* Here's where it came from */
  775. odev->skb_rx_buf->dev = odev->net;
  776. /* Copy what we got so far. make room for iphdr
  777. * after tail. */
  778. tmp_rx_buf =
  779. skb_put(odev->skb_rx_buf,
  780. sizeof(struct iphdr));
  781. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  782. sizeof(struct iphdr));
  783. /* ETH_HLEN */
  784. odev->rx_buf_size = sizeof(struct iphdr);
  785. /* Filip actually use .tot_len */
  786. odev->rx_buf_missing =
  787. frame_len - sizeof(struct iphdr);
  788. odev->rx_parse_state = WAIT_DATA;
  789. }
  790. break;
  791. case WAIT_DATA:
  792. temp_bytes = (count < odev->rx_buf_missing)
  793. ? count : odev->rx_buf_missing;
  794. /* Copy the rest of the bytes that are left in the
  795. * buffer into the waiting sk_buf. */
  796. /* Make room for temp_bytes after tail. */
  797. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  798. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  799. odev->rx_buf_missing -= temp_bytes;
  800. count -= temp_bytes;
  801. buffer_offset += temp_bytes;
  802. odev->rx_buf_size += temp_bytes;
  803. if (!odev->rx_buf_missing) {
  804. /* Packet is complete. Inject into stack. */
  805. /* We have IP packet here */
  806. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  807. /* don't check it */
  808. odev->skb_rx_buf->ip_summed =
  809. CHECKSUM_UNNECESSARY;
  810. skb_reset_mac_header(odev->skb_rx_buf);
  811. /* Ship it off to the kernel */
  812. netif_rx(odev->skb_rx_buf);
  813. /* No longer our buffer. */
  814. odev->skb_rx_buf = NULL;
  815. /* update out statistics */
  816. odev->net->stats.rx_packets++;
  817. odev->net->stats.rx_bytes += odev->rx_buf_size;
  818. odev->rx_buf_size = 0;
  819. odev->rx_buf_missing = sizeof(struct iphdr);
  820. odev->rx_parse_state = WAIT_IP;
  821. }
  822. break;
  823. case WAIT_SYNC:
  824. D1(" W_S");
  825. count = 0;
  826. break;
  827. default:
  828. D1(" ");
  829. count--;
  830. break;
  831. }
  832. }
  833. /* Recovery mechanism for WAIT_SYNC state. */
  834. if (is_eop) {
  835. if (odev->rx_parse_state == WAIT_SYNC) {
  836. odev->rx_parse_state = WAIT_IP;
  837. odev->rx_buf_size = 0;
  838. odev->rx_buf_missing = sizeof(struct iphdr);
  839. }
  840. }
  841. }
  842. /* Moving data from usb to kernel (in interrupt state) */
  843. static void read_bulk_callback(struct urb *urb)
  844. {
  845. struct hso_net *odev = urb->context;
  846. struct net_device *net;
  847. int result;
  848. int status = urb->status;
  849. /* is al ok? (Filip: Who's Al ?) */
  850. if (status) {
  851. log_usb_status(status, __func__);
  852. return;
  853. }
  854. /* Sanity check */
  855. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  856. D1("BULK IN callback but driver is not active!");
  857. return;
  858. }
  859. usb_mark_last_busy(urb->dev);
  860. net = odev->net;
  861. if (!netif_device_present(net)) {
  862. /* Somebody killed our network interface... */
  863. return;
  864. }
  865. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  866. u32 rest;
  867. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  868. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  869. if (((rest == 5) || (rest == 6))
  870. && !memcmp(((u8 *) urb->transfer_buffer) +
  871. urb->actual_length - 4, crc_check, 4)) {
  872. urb->actual_length -= 4;
  873. }
  874. }
  875. /* do we even have a packet? */
  876. if (urb->actual_length) {
  877. /* Handle the IP stream, add header and push it onto network
  878. * stack if the packet is complete. */
  879. spin_lock(&odev->net_lock);
  880. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  881. (urb->transfer_buffer_length >
  882. urb->actual_length) ? 1 : 0);
  883. spin_unlock(&odev->net_lock);
  884. }
  885. /* We are done with this URB, resubmit it. Prep the USB to wait for
  886. * another frame. Reuse same as received. */
  887. usb_fill_bulk_urb(urb,
  888. odev->parent->usb,
  889. usb_rcvbulkpipe(odev->parent->usb,
  890. odev->in_endp->
  891. bEndpointAddress & 0x7F),
  892. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  893. read_bulk_callback, odev);
  894. /* Give this to the USB subsystem so it can tell us when more data
  895. * arrives. */
  896. result = usb_submit_urb(urb, GFP_ATOMIC);
  897. if (result)
  898. dev_warn(&odev->parent->interface->dev,
  899. "%s failed submit mux_bulk_rx_urb %d", __func__,
  900. result);
  901. }
  902. /* Serial driver functions */
  903. static void hso_init_termios(struct ktermios *termios)
  904. {
  905. /*
  906. * The default requirements for this device are:
  907. */
  908. termios->c_iflag &=
  909. ~(IGNBRK /* disable ignore break */
  910. | BRKINT /* disable break causes interrupt */
  911. | PARMRK /* disable mark parity errors */
  912. | ISTRIP /* disable clear high bit of input characters */
  913. | INLCR /* disable translate NL to CR */
  914. | IGNCR /* disable ignore CR */
  915. | ICRNL /* disable translate CR to NL */
  916. | IXON); /* disable enable XON/XOFF flow control */
  917. /* disable postprocess output characters */
  918. termios->c_oflag &= ~OPOST;
  919. termios->c_lflag &=
  920. ~(ECHO /* disable echo input characters */
  921. | ECHONL /* disable echo new line */
  922. | ICANON /* disable erase, kill, werase, and rprnt
  923. special characters */
  924. | ISIG /* disable interrupt, quit, and suspend special
  925. characters */
  926. | IEXTEN); /* disable non-POSIX special characters */
  927. termios->c_cflag &=
  928. ~(CSIZE /* no size */
  929. | PARENB /* disable parity bit */
  930. | CBAUD /* clear current baud rate */
  931. | CBAUDEX); /* clear current buad rate */
  932. termios->c_cflag |= CS8; /* character size 8 bits */
  933. /* baud rate 115200 */
  934. tty_termios_encode_baud_rate(termios, 115200, 115200);
  935. }
  936. static void _hso_serial_set_termios(struct tty_struct *tty,
  937. struct ktermios *old)
  938. {
  939. struct hso_serial *serial = get_serial_by_tty(tty);
  940. struct ktermios *termios;
  941. if (!serial) {
  942. printk(KERN_ERR "%s: no tty structures", __func__);
  943. return;
  944. }
  945. D4("port %d", serial->minor);
  946. /*
  947. * Fix up unsupported bits
  948. */
  949. termios = tty->termios;
  950. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  951. termios->c_cflag &=
  952. ~(CSIZE /* no size */
  953. | PARENB /* disable parity bit */
  954. | CBAUD /* clear current baud rate */
  955. | CBAUDEX); /* clear current buad rate */
  956. termios->c_cflag |= CS8; /* character size 8 bits */
  957. /* baud rate 115200 */
  958. tty_encode_baud_rate(tty, 115200, 115200);
  959. }
  960. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  961. {
  962. int result;
  963. #ifdef CONFIG_HSO_AUTOPM
  964. usb_mark_last_busy(urb->dev);
  965. #endif
  966. /* We are done with this URB, resubmit it. Prep the USB to wait for
  967. * another frame */
  968. usb_fill_bulk_urb(urb, serial->parent->usb,
  969. usb_rcvbulkpipe(serial->parent->usb,
  970. serial->in_endp->
  971. bEndpointAddress & 0x7F),
  972. urb->transfer_buffer, serial->rx_data_length,
  973. hso_std_serial_read_bulk_callback, serial);
  974. /* Give this to the USB subsystem so it can tell us when more data
  975. * arrives. */
  976. result = usb_submit_urb(urb, GFP_ATOMIC);
  977. if (result) {
  978. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  979. __func__, result);
  980. }
  981. }
  982. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  983. {
  984. int count;
  985. struct urb *curr_urb;
  986. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  987. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  988. count = put_rxbuf_data(curr_urb, serial);
  989. if (count == -1)
  990. return;
  991. if (count == 0) {
  992. serial->curr_rx_urb_idx++;
  993. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  994. serial->curr_rx_urb_idx = 0;
  995. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  996. }
  997. }
  998. }
  999. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1000. {
  1001. int count = 0;
  1002. struct urb *urb;
  1003. urb = serial->rx_urb[0];
  1004. if (serial->open_count > 0) {
  1005. count = put_rxbuf_data(urb, serial);
  1006. if (count == -1)
  1007. return;
  1008. }
  1009. /* Re issue a read as long as we receive data. */
  1010. if (count == 0 && ((urb->actual_length != 0) ||
  1011. (serial->rx_state == RX_PENDING))) {
  1012. serial->rx_state = RX_SENT;
  1013. hso_mux_serial_read(serial);
  1014. } else
  1015. serial->rx_state = RX_IDLE;
  1016. }
  1017. /* read callback for Diag and CS port */
  1018. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1019. {
  1020. struct hso_serial *serial = urb->context;
  1021. int status = urb->status;
  1022. /* sanity check */
  1023. if (!serial) {
  1024. D1("serial == NULL");
  1025. return;
  1026. } else if (status) {
  1027. log_usb_status(status, __func__);
  1028. return;
  1029. }
  1030. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1031. D1("Actual length = %d\n", urb->actual_length);
  1032. DUMP1(urb->transfer_buffer, urb->actual_length);
  1033. /* Anyone listening? */
  1034. if (serial->open_count == 0)
  1035. return;
  1036. if (status == 0) {
  1037. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1038. u32 rest;
  1039. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1040. rest =
  1041. urb->actual_length %
  1042. serial->in_endp->wMaxPacketSize;
  1043. if (((rest == 5) || (rest == 6))
  1044. && !memcmp(((u8 *) urb->transfer_buffer) +
  1045. urb->actual_length - 4, crc_check, 4)) {
  1046. urb->actual_length -= 4;
  1047. }
  1048. }
  1049. /* Valid data, handle RX data */
  1050. spin_lock(&serial->serial_lock);
  1051. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1052. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1053. spin_unlock(&serial->serial_lock);
  1054. } else if (status == -ENOENT || status == -ECONNRESET) {
  1055. /* Unlinked - check for throttled port. */
  1056. D2("Port %d, successfully unlinked urb", serial->minor);
  1057. spin_lock(&serial->serial_lock);
  1058. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1059. hso_resubmit_rx_bulk_urb(serial, urb);
  1060. spin_unlock(&serial->serial_lock);
  1061. } else {
  1062. D2("Port %d, status = %d for read urb", serial->minor, status);
  1063. return;
  1064. }
  1065. }
  1066. /*
  1067. * This needs to be a tasklet otherwise we will
  1068. * end up recursively calling this function.
  1069. */
  1070. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1071. {
  1072. unsigned long flags;
  1073. spin_lock_irqsave(&serial->serial_lock, flags);
  1074. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1075. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1076. else
  1077. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1078. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1079. }
  1080. static void hso_unthrottle(struct tty_struct *tty)
  1081. {
  1082. struct hso_serial *serial = get_serial_by_tty(tty);
  1083. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1084. }
  1085. static void hso_unthrottle_workfunc(struct work_struct *work)
  1086. {
  1087. struct hso_serial *serial =
  1088. container_of(work, struct hso_serial,
  1089. retry_unthrottle_workqueue);
  1090. hso_unthrottle_tasklet(serial);
  1091. }
  1092. /* open the requested serial port */
  1093. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1094. {
  1095. struct hso_serial *serial = get_serial_by_index(tty->index);
  1096. int result;
  1097. /* sanity check */
  1098. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1099. WARN_ON(1);
  1100. tty->driver_data = NULL;
  1101. D1("Failed to open port");
  1102. return -ENODEV;
  1103. }
  1104. mutex_lock(&serial->parent->mutex);
  1105. result = usb_autopm_get_interface(serial->parent->interface);
  1106. if (result < 0)
  1107. goto err_out;
  1108. D1("Opening %d", serial->minor);
  1109. kref_get(&serial->parent->ref);
  1110. /* setup */
  1111. spin_lock_irq(&serial->serial_lock);
  1112. tty->driver_data = serial;
  1113. tty_kref_put(serial->tty);
  1114. serial->tty = tty_kref_get(tty);
  1115. spin_unlock_irq(&serial->serial_lock);
  1116. /* check for port already opened, if not set the termios */
  1117. serial->open_count++;
  1118. if (serial->open_count == 1) {
  1119. tty->low_latency = 1;
  1120. serial->rx_state = RX_IDLE;
  1121. /* Force default termio settings */
  1122. _hso_serial_set_termios(tty, NULL);
  1123. tasklet_init(&serial->unthrottle_tasklet,
  1124. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1125. (unsigned long)serial);
  1126. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1127. hso_unthrottle_workfunc);
  1128. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1129. if (result) {
  1130. hso_stop_serial_device(serial->parent);
  1131. serial->open_count--;
  1132. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1133. }
  1134. } else {
  1135. D1("Port was already open");
  1136. }
  1137. usb_autopm_put_interface(serial->parent->interface);
  1138. /* done */
  1139. if (result)
  1140. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1141. err_out:
  1142. mutex_unlock(&serial->parent->mutex);
  1143. return result;
  1144. }
  1145. /* close the requested serial port */
  1146. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1147. {
  1148. struct hso_serial *serial = tty->driver_data;
  1149. u8 usb_gone;
  1150. D1("Closing serial port");
  1151. /* Open failed, no close cleanup required */
  1152. if (serial == NULL)
  1153. return;
  1154. mutex_lock(&serial->parent->mutex);
  1155. usb_gone = serial->parent->usb_gone;
  1156. if (!usb_gone)
  1157. usb_autopm_get_interface(serial->parent->interface);
  1158. /* reset the rts and dtr */
  1159. /* do the actual close */
  1160. serial->open_count--;
  1161. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1162. if (serial->open_count <= 0) {
  1163. serial->open_count = 0;
  1164. spin_lock_irq(&serial->serial_lock);
  1165. if (serial->tty == tty) {
  1166. serial->tty->driver_data = NULL;
  1167. serial->tty = NULL;
  1168. tty_kref_put(tty);
  1169. }
  1170. spin_unlock_irq(&serial->serial_lock);
  1171. if (!usb_gone)
  1172. hso_stop_serial_device(serial->parent);
  1173. tasklet_kill(&serial->unthrottle_tasklet);
  1174. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1175. }
  1176. if (!usb_gone)
  1177. usb_autopm_put_interface(serial->parent->interface);
  1178. mutex_unlock(&serial->parent->mutex);
  1179. }
  1180. /* close the requested serial port */
  1181. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1182. int count)
  1183. {
  1184. struct hso_serial *serial = get_serial_by_tty(tty);
  1185. int space, tx_bytes;
  1186. unsigned long flags;
  1187. /* sanity check */
  1188. if (serial == NULL) {
  1189. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1190. return -ENODEV;
  1191. }
  1192. spin_lock_irqsave(&serial->serial_lock, flags);
  1193. space = serial->tx_data_length - serial->tx_buffer_count;
  1194. tx_bytes = (count < space) ? count : space;
  1195. if (!tx_bytes)
  1196. goto out;
  1197. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1198. serial->tx_buffer_count += tx_bytes;
  1199. out:
  1200. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1201. hso_kick_transmit(serial);
  1202. /* done */
  1203. return tx_bytes;
  1204. }
  1205. /* how much room is there for writing */
  1206. static int hso_serial_write_room(struct tty_struct *tty)
  1207. {
  1208. struct hso_serial *serial = get_serial_by_tty(tty);
  1209. int room;
  1210. unsigned long flags;
  1211. spin_lock_irqsave(&serial->serial_lock, flags);
  1212. room = serial->tx_data_length - serial->tx_buffer_count;
  1213. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1214. /* return free room */
  1215. return room;
  1216. }
  1217. /* setup the term */
  1218. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1219. {
  1220. struct hso_serial *serial = get_serial_by_tty(tty);
  1221. unsigned long flags;
  1222. if (old)
  1223. D5("Termios called with: cflags new[%d] - old[%d]",
  1224. tty->termios->c_cflag, old->c_cflag);
  1225. /* the actual setup */
  1226. spin_lock_irqsave(&serial->serial_lock, flags);
  1227. if (serial->open_count)
  1228. _hso_serial_set_termios(tty, old);
  1229. else
  1230. tty->termios = old;
  1231. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1232. /* done */
  1233. return;
  1234. }
  1235. /* how many characters in the buffer */
  1236. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1237. {
  1238. struct hso_serial *serial = get_serial_by_tty(tty);
  1239. int chars;
  1240. unsigned long flags;
  1241. /* sanity check */
  1242. if (serial == NULL)
  1243. return 0;
  1244. spin_lock_irqsave(&serial->serial_lock, flags);
  1245. chars = serial->tx_buffer_count;
  1246. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1247. return chars;
  1248. }
  1249. static int tiocmget_submit_urb(struct hso_serial *serial,
  1250. struct hso_tiocmget *tiocmget,
  1251. struct usb_device *usb)
  1252. {
  1253. int result;
  1254. if (serial->parent->usb_gone)
  1255. return -ENODEV;
  1256. usb_fill_int_urb(tiocmget->urb, usb,
  1257. usb_rcvintpipe(usb,
  1258. tiocmget->endp->
  1259. bEndpointAddress & 0x7F),
  1260. &tiocmget->serial_state_notification,
  1261. sizeof(struct hso_serial_state_notification),
  1262. tiocmget_intr_callback, serial,
  1263. tiocmget->endp->bInterval);
  1264. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1265. if (result) {
  1266. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1267. result);
  1268. }
  1269. return result;
  1270. }
  1271. static void tiocmget_intr_callback(struct urb *urb)
  1272. {
  1273. struct hso_serial *serial = urb->context;
  1274. struct hso_tiocmget *tiocmget;
  1275. int status = urb->status;
  1276. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1277. struct uart_icount *icount;
  1278. struct hso_serial_state_notification *serial_state_notification;
  1279. struct usb_device *usb;
  1280. /* Sanity checks */
  1281. if (!serial)
  1282. return;
  1283. if (status) {
  1284. log_usb_status(status, __func__);
  1285. return;
  1286. }
  1287. tiocmget = serial->tiocmget;
  1288. if (!tiocmget)
  1289. return;
  1290. usb = serial->parent->usb;
  1291. serial_state_notification = &tiocmget->serial_state_notification;
  1292. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1293. serial_state_notification->bNotification != B_NOTIFICATION ||
  1294. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1295. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1296. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1297. dev_warn(&usb->dev,
  1298. "hso received invalid serial state notification\n");
  1299. DUMP(serial_state_notification,
  1300. sizeof(hso_serial_state_notifation))
  1301. } else {
  1302. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1303. UART_state_bitmap);
  1304. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1305. icount = &tiocmget->icount;
  1306. spin_lock(&serial->serial_lock);
  1307. if ((UART_state_bitmap & B_OVERRUN) !=
  1308. (prev_UART_state_bitmap & B_OVERRUN))
  1309. icount->parity++;
  1310. if ((UART_state_bitmap & B_PARITY) !=
  1311. (prev_UART_state_bitmap & B_PARITY))
  1312. icount->parity++;
  1313. if ((UART_state_bitmap & B_FRAMING) !=
  1314. (prev_UART_state_bitmap & B_FRAMING))
  1315. icount->frame++;
  1316. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1317. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1318. icount->rng++;
  1319. if ((UART_state_bitmap & B_BREAK) !=
  1320. (prev_UART_state_bitmap & B_BREAK))
  1321. icount->brk++;
  1322. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1323. (prev_UART_state_bitmap & B_TX_CARRIER))
  1324. icount->dsr++;
  1325. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1326. (prev_UART_state_bitmap & B_RX_CARRIER))
  1327. icount->dcd++;
  1328. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1329. spin_unlock(&serial->serial_lock);
  1330. tiocmget->intr_completed = 1;
  1331. wake_up_interruptible(&tiocmget->waitq);
  1332. }
  1333. memset(serial_state_notification, 0,
  1334. sizeof(struct hso_serial_state_notification));
  1335. tiocmget_submit_urb(serial,
  1336. tiocmget,
  1337. serial->parent->usb);
  1338. }
  1339. /*
  1340. * next few functions largely stolen from drivers/serial/serial_core.c
  1341. */
  1342. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1343. * - mask passed in arg for lines of interest
  1344. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1345. * Caller should use TIOCGICOUNT to see which one it was
  1346. */
  1347. static int
  1348. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1349. {
  1350. DECLARE_WAITQUEUE(wait, current);
  1351. struct uart_icount cprev, cnow;
  1352. struct hso_tiocmget *tiocmget;
  1353. int ret;
  1354. tiocmget = serial->tiocmget;
  1355. if (!tiocmget)
  1356. return -ENOENT;
  1357. /*
  1358. * note the counters on entry
  1359. */
  1360. spin_lock_irq(&serial->serial_lock);
  1361. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1362. spin_unlock_irq(&serial->serial_lock);
  1363. add_wait_queue(&tiocmget->waitq, &wait);
  1364. for (;;) {
  1365. spin_lock_irq(&serial->serial_lock);
  1366. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1367. spin_unlock_irq(&serial->serial_lock);
  1368. set_current_state(TASK_INTERRUPTIBLE);
  1369. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1370. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1371. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1372. ret = 0;
  1373. break;
  1374. }
  1375. schedule();
  1376. /* see if a signal did it */
  1377. if (signal_pending(current)) {
  1378. ret = -ERESTARTSYS;
  1379. break;
  1380. }
  1381. cprev = cnow;
  1382. }
  1383. current->state = TASK_RUNNING;
  1384. remove_wait_queue(&tiocmget->waitq, &wait);
  1385. return ret;
  1386. }
  1387. /*
  1388. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1389. * Return: write counters to the user passed counter struct
  1390. * NB: both 1->0 and 0->1 transitions are counted except for
  1391. * RI where only 0->1 is counted.
  1392. */
  1393. static int hso_get_count(struct hso_serial *serial,
  1394. struct serial_icounter_struct __user *icnt)
  1395. {
  1396. struct serial_icounter_struct icount;
  1397. struct uart_icount cnow;
  1398. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1399. if (!tiocmget)
  1400. return -ENOENT;
  1401. spin_lock_irq(&serial->serial_lock);
  1402. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1403. spin_unlock_irq(&serial->serial_lock);
  1404. icount.cts = cnow.cts;
  1405. icount.dsr = cnow.dsr;
  1406. icount.rng = cnow.rng;
  1407. icount.dcd = cnow.dcd;
  1408. icount.rx = cnow.rx;
  1409. icount.tx = cnow.tx;
  1410. icount.frame = cnow.frame;
  1411. icount.overrun = cnow.overrun;
  1412. icount.parity = cnow.parity;
  1413. icount.brk = cnow.brk;
  1414. icount.buf_overrun = cnow.buf_overrun;
  1415. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1416. }
  1417. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1418. {
  1419. int retval;
  1420. struct hso_serial *serial = get_serial_by_tty(tty);
  1421. struct hso_tiocmget *tiocmget;
  1422. u16 UART_state_bitmap;
  1423. /* sanity check */
  1424. if (!serial) {
  1425. D1("no tty structures");
  1426. return -EINVAL;
  1427. }
  1428. spin_lock_irq(&serial->serial_lock);
  1429. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1430. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1431. tiocmget = serial->tiocmget;
  1432. if (tiocmget) {
  1433. UART_state_bitmap = le16_to_cpu(
  1434. tiocmget->prev_UART_state_bitmap);
  1435. if (UART_state_bitmap & B_RING_SIGNAL)
  1436. retval |= TIOCM_RNG;
  1437. if (UART_state_bitmap & B_RX_CARRIER)
  1438. retval |= TIOCM_CD;
  1439. if (UART_state_bitmap & B_TX_CARRIER)
  1440. retval |= TIOCM_DSR;
  1441. }
  1442. spin_unlock_irq(&serial->serial_lock);
  1443. return retval;
  1444. }
  1445. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1446. unsigned int set, unsigned int clear)
  1447. {
  1448. int val = 0;
  1449. unsigned long flags;
  1450. int if_num;
  1451. struct hso_serial *serial = get_serial_by_tty(tty);
  1452. /* sanity check */
  1453. if (!serial) {
  1454. D1("no tty structures");
  1455. return -EINVAL;
  1456. }
  1457. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1458. spin_lock_irqsave(&serial->serial_lock, flags);
  1459. if (set & TIOCM_RTS)
  1460. serial->rts_state = 1;
  1461. if (set & TIOCM_DTR)
  1462. serial->dtr_state = 1;
  1463. if (clear & TIOCM_RTS)
  1464. serial->rts_state = 0;
  1465. if (clear & TIOCM_DTR)
  1466. serial->dtr_state = 0;
  1467. if (serial->dtr_state)
  1468. val |= 0x01;
  1469. if (serial->rts_state)
  1470. val |= 0x02;
  1471. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1472. return usb_control_msg(serial->parent->usb,
  1473. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1474. 0x21, val, if_num, NULL, 0,
  1475. USB_CTRL_SET_TIMEOUT);
  1476. }
  1477. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1478. unsigned int cmd, unsigned long arg)
  1479. {
  1480. struct hso_serial *serial = get_serial_by_tty(tty);
  1481. void __user *uarg = (void __user *)arg;
  1482. int ret = 0;
  1483. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1484. if (!serial)
  1485. return -ENODEV;
  1486. switch (cmd) {
  1487. case TIOCMIWAIT:
  1488. ret = hso_wait_modem_status(serial, arg);
  1489. break;
  1490. case TIOCGICOUNT:
  1491. ret = hso_get_count(serial, uarg);
  1492. break;
  1493. default:
  1494. ret = -ENOIOCTLCMD;
  1495. break;
  1496. }
  1497. return ret;
  1498. }
  1499. /* starts a transmit */
  1500. static void hso_kick_transmit(struct hso_serial *serial)
  1501. {
  1502. u8 *temp;
  1503. unsigned long flags;
  1504. int res;
  1505. spin_lock_irqsave(&serial->serial_lock, flags);
  1506. if (!serial->tx_buffer_count)
  1507. goto out;
  1508. if (serial->tx_urb_used)
  1509. goto out;
  1510. /* Wakeup USB interface if necessary */
  1511. if (hso_get_activity(serial->parent) == -EAGAIN)
  1512. goto out;
  1513. /* Switch pointers around to avoid memcpy */
  1514. temp = serial->tx_buffer;
  1515. serial->tx_buffer = serial->tx_data;
  1516. serial->tx_data = temp;
  1517. serial->tx_data_count = serial->tx_buffer_count;
  1518. serial->tx_buffer_count = 0;
  1519. /* If temp is set, it means we switched buffers */
  1520. if (temp && serial->write_data) {
  1521. res = serial->write_data(serial);
  1522. if (res >= 0)
  1523. serial->tx_urb_used = 1;
  1524. }
  1525. out:
  1526. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1527. }
  1528. /* make a request (for reading and writing data to muxed serial port) */
  1529. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1530. struct urb *ctrl_urb,
  1531. struct usb_ctrlrequest *ctrl_req,
  1532. u8 *ctrl_urb_data, u32 size)
  1533. {
  1534. int result;
  1535. int pipe;
  1536. /* Sanity check */
  1537. if (!serial || !ctrl_urb || !ctrl_req) {
  1538. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1539. return -EINVAL;
  1540. }
  1541. /* initialize */
  1542. ctrl_req->wValue = 0;
  1543. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1544. ctrl_req->wLength = cpu_to_le16(size);
  1545. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1546. /* Reading command */
  1547. ctrl_req->bRequestType = USB_DIR_IN |
  1548. USB_TYPE_OPTION_VENDOR |
  1549. USB_RECIP_INTERFACE;
  1550. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1551. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1552. } else {
  1553. /* Writing command */
  1554. ctrl_req->bRequestType = USB_DIR_OUT |
  1555. USB_TYPE_OPTION_VENDOR |
  1556. USB_RECIP_INTERFACE;
  1557. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1558. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1559. }
  1560. /* syslog */
  1561. D2("%s command (%02x) len: %d, port: %d",
  1562. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1563. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1564. /* Load ctrl urb */
  1565. ctrl_urb->transfer_flags = 0;
  1566. usb_fill_control_urb(ctrl_urb,
  1567. serial->parent->usb,
  1568. pipe,
  1569. (u8 *) ctrl_req,
  1570. ctrl_urb_data, size, ctrl_callback, serial);
  1571. /* Send it on merry way */
  1572. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1573. if (result) {
  1574. dev_err(&ctrl_urb->dev->dev,
  1575. "%s failed submit ctrl_urb %d type %d", __func__,
  1576. result, type);
  1577. return result;
  1578. }
  1579. /* done */
  1580. return size;
  1581. }
  1582. /* called by intr_callback when read occurs */
  1583. static int hso_mux_serial_read(struct hso_serial *serial)
  1584. {
  1585. if (!serial)
  1586. return -EINVAL;
  1587. /* clean data */
  1588. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1589. /* make the request */
  1590. if (serial->num_rx_urbs != 1) {
  1591. dev_err(&serial->parent->interface->dev,
  1592. "ERROR: mux'd reads with multiple buffers "
  1593. "not possible\n");
  1594. return 0;
  1595. }
  1596. return mux_device_request(serial,
  1597. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1598. serial->parent->port_spec & HSO_PORT_MASK,
  1599. serial->rx_urb[0],
  1600. &serial->ctrl_req_rx,
  1601. serial->rx_data[0], serial->rx_data_length);
  1602. }
  1603. /* used for muxed serial port callback (muxed serial read) */
  1604. static void intr_callback(struct urb *urb)
  1605. {
  1606. struct hso_shared_int *shared_int = urb->context;
  1607. struct hso_serial *serial;
  1608. unsigned char *port_req;
  1609. int status = urb->status;
  1610. int i;
  1611. usb_mark_last_busy(urb->dev);
  1612. /* sanity check */
  1613. if (!shared_int)
  1614. return;
  1615. /* status check */
  1616. if (status) {
  1617. log_usb_status(status, __func__);
  1618. return;
  1619. }
  1620. D4("\n--- Got intr callback 0x%02X ---", status);
  1621. /* what request? */
  1622. port_req = urb->transfer_buffer;
  1623. D4(" port_req = 0x%.2X\n", *port_req);
  1624. /* loop over all muxed ports to find the one sending this */
  1625. for (i = 0; i < 8; i++) {
  1626. /* max 8 channels on MUX */
  1627. if (*port_req & (1 << i)) {
  1628. serial = get_serial_by_shared_int_and_type(shared_int,
  1629. (1 << i));
  1630. if (serial != NULL) {
  1631. D1("Pending read interrupt on port %d\n", i);
  1632. spin_lock(&serial->serial_lock);
  1633. if (serial->rx_state == RX_IDLE) {
  1634. /* Setup and send a ctrl req read on
  1635. * port i */
  1636. if (!serial->rx_urb_filled[0]) {
  1637. serial->rx_state = RX_SENT;
  1638. hso_mux_serial_read(serial);
  1639. } else
  1640. serial->rx_state = RX_PENDING;
  1641. } else {
  1642. D1("Already pending a read on "
  1643. "port %d\n", i);
  1644. }
  1645. spin_unlock(&serial->serial_lock);
  1646. }
  1647. }
  1648. }
  1649. /* Resubmit interrupt urb */
  1650. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1651. }
  1652. /* called for writing to muxed serial port */
  1653. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1654. {
  1655. if (NULL == serial)
  1656. return -EINVAL;
  1657. return mux_device_request(serial,
  1658. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1659. serial->parent->port_spec & HSO_PORT_MASK,
  1660. serial->tx_urb,
  1661. &serial->ctrl_req_tx,
  1662. serial->tx_data, serial->tx_data_count);
  1663. }
  1664. /* write callback for Diag and CS port */
  1665. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1666. {
  1667. struct hso_serial *serial = urb->context;
  1668. int status = urb->status;
  1669. struct tty_struct *tty;
  1670. /* sanity check */
  1671. if (!serial) {
  1672. D1("serial == NULL");
  1673. return;
  1674. }
  1675. spin_lock(&serial->serial_lock);
  1676. serial->tx_urb_used = 0;
  1677. tty = tty_kref_get(serial->tty);
  1678. spin_unlock(&serial->serial_lock);
  1679. if (status) {
  1680. log_usb_status(status, __func__);
  1681. tty_kref_put(tty);
  1682. return;
  1683. }
  1684. hso_put_activity(serial->parent);
  1685. if (tty) {
  1686. tty_wakeup(tty);
  1687. tty_kref_put(tty);
  1688. }
  1689. hso_kick_transmit(serial);
  1690. D1(" ");
  1691. return;
  1692. }
  1693. /* called for writing diag or CS serial port */
  1694. static int hso_std_serial_write_data(struct hso_serial *serial)
  1695. {
  1696. int count = serial->tx_data_count;
  1697. int result;
  1698. usb_fill_bulk_urb(serial->tx_urb,
  1699. serial->parent->usb,
  1700. usb_sndbulkpipe(serial->parent->usb,
  1701. serial->out_endp->
  1702. bEndpointAddress & 0x7F),
  1703. serial->tx_data, serial->tx_data_count,
  1704. hso_std_serial_write_bulk_callback, serial);
  1705. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1706. if (result) {
  1707. dev_warn(&serial->parent->usb->dev,
  1708. "Failed to submit urb - res %d\n", result);
  1709. return result;
  1710. }
  1711. return count;
  1712. }
  1713. /* callback after read or write on muxed serial port */
  1714. static void ctrl_callback(struct urb *urb)
  1715. {
  1716. struct hso_serial *serial = urb->context;
  1717. struct usb_ctrlrequest *req;
  1718. int status = urb->status;
  1719. struct tty_struct *tty;
  1720. /* sanity check */
  1721. if (!serial)
  1722. return;
  1723. spin_lock(&serial->serial_lock);
  1724. serial->tx_urb_used = 0;
  1725. tty = tty_kref_get(serial->tty);
  1726. spin_unlock(&serial->serial_lock);
  1727. if (status) {
  1728. log_usb_status(status, __func__);
  1729. tty_kref_put(tty);
  1730. return;
  1731. }
  1732. /* what request? */
  1733. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1734. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1735. D4("Actual length of urb = %d\n", urb->actual_length);
  1736. DUMP1(urb->transfer_buffer, urb->actual_length);
  1737. if (req->bRequestType ==
  1738. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1739. /* response to a read command */
  1740. serial->rx_urb_filled[0] = 1;
  1741. spin_lock(&serial->serial_lock);
  1742. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1743. spin_unlock(&serial->serial_lock);
  1744. } else {
  1745. hso_put_activity(serial->parent);
  1746. if (tty)
  1747. tty_wakeup(tty);
  1748. /* response to a write command */
  1749. hso_kick_transmit(serial);
  1750. }
  1751. tty_kref_put(tty);
  1752. }
  1753. /* handle RX data for serial port */
  1754. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1755. {
  1756. struct tty_struct *tty;
  1757. int write_length_remaining = 0;
  1758. int curr_write_len;
  1759. /* Sanity check */
  1760. if (urb == NULL || serial == NULL) {
  1761. D1("serial = NULL");
  1762. return -2;
  1763. }
  1764. /* All callers to put_rxbuf_data hold serial_lock */
  1765. tty = tty_kref_get(serial->tty);
  1766. /* Push data to tty */
  1767. if (tty) {
  1768. write_length_remaining = urb->actual_length -
  1769. serial->curr_rx_urb_offset;
  1770. D1("data to push to tty");
  1771. while (write_length_remaining) {
  1772. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1773. tty_kref_put(tty);
  1774. return -1;
  1775. }
  1776. curr_write_len = tty_insert_flip_string
  1777. (tty, urb->transfer_buffer +
  1778. serial->curr_rx_urb_offset,
  1779. write_length_remaining);
  1780. serial->curr_rx_urb_offset += curr_write_len;
  1781. write_length_remaining -= curr_write_len;
  1782. tty_flip_buffer_push(tty);
  1783. }
  1784. }
  1785. if (write_length_remaining == 0) {
  1786. serial->curr_rx_urb_offset = 0;
  1787. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1788. }
  1789. tty_kref_put(tty);
  1790. return write_length_remaining;
  1791. }
  1792. /* Base driver functions */
  1793. static void hso_log_port(struct hso_device *hso_dev)
  1794. {
  1795. char *port_type;
  1796. char port_dev[20];
  1797. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1798. case HSO_PORT_CONTROL:
  1799. port_type = "Control";
  1800. break;
  1801. case HSO_PORT_APP:
  1802. port_type = "Application";
  1803. break;
  1804. case HSO_PORT_GPS:
  1805. port_type = "GPS";
  1806. break;
  1807. case HSO_PORT_GPS_CONTROL:
  1808. port_type = "GPS control";
  1809. break;
  1810. case HSO_PORT_APP2:
  1811. port_type = "Application2";
  1812. break;
  1813. case HSO_PORT_PCSC:
  1814. port_type = "PCSC";
  1815. break;
  1816. case HSO_PORT_DIAG:
  1817. port_type = "Diagnostic";
  1818. break;
  1819. case HSO_PORT_DIAG2:
  1820. port_type = "Diagnostic2";
  1821. break;
  1822. case HSO_PORT_MODEM:
  1823. port_type = "Modem";
  1824. break;
  1825. case HSO_PORT_NETWORK:
  1826. port_type = "Network";
  1827. break;
  1828. default:
  1829. port_type = "Unknown";
  1830. break;
  1831. }
  1832. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1833. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1834. } else
  1835. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1836. dev2ser(hso_dev)->minor);
  1837. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1838. port_type, port_dev);
  1839. }
  1840. static int hso_start_net_device(struct hso_device *hso_dev)
  1841. {
  1842. int i, result = 0;
  1843. struct hso_net *hso_net = dev2net(hso_dev);
  1844. if (!hso_net)
  1845. return -ENODEV;
  1846. /* send URBs for all read buffers */
  1847. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1848. /* Prep a receive URB */
  1849. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1850. hso_dev->usb,
  1851. usb_rcvbulkpipe(hso_dev->usb,
  1852. hso_net->in_endp->
  1853. bEndpointAddress & 0x7F),
  1854. hso_net->mux_bulk_rx_buf_pool[i],
  1855. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1856. hso_net);
  1857. /* Put it out there so the device can send us stuff */
  1858. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1859. GFP_NOIO);
  1860. if (result)
  1861. dev_warn(&hso_dev->usb->dev,
  1862. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1863. i, result);
  1864. }
  1865. return result;
  1866. }
  1867. static int hso_stop_net_device(struct hso_device *hso_dev)
  1868. {
  1869. int i;
  1870. struct hso_net *hso_net = dev2net(hso_dev);
  1871. if (!hso_net)
  1872. return -ENODEV;
  1873. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1874. if (hso_net->mux_bulk_rx_urb_pool[i])
  1875. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1876. }
  1877. if (hso_net->mux_bulk_tx_urb)
  1878. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1879. return 0;
  1880. }
  1881. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1882. {
  1883. int i, result = 0;
  1884. struct hso_serial *serial = dev2ser(hso_dev);
  1885. if (!serial)
  1886. return -ENODEV;
  1887. /* If it is not the MUX port fill in and submit a bulk urb (already
  1888. * allocated in hso_serial_start) */
  1889. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1890. for (i = 0; i < serial->num_rx_urbs; i++) {
  1891. usb_fill_bulk_urb(serial->rx_urb[i],
  1892. serial->parent->usb,
  1893. usb_rcvbulkpipe(serial->parent->usb,
  1894. serial->in_endp->
  1895. bEndpointAddress &
  1896. 0x7F),
  1897. serial->rx_data[i],
  1898. serial->rx_data_length,
  1899. hso_std_serial_read_bulk_callback,
  1900. serial);
  1901. result = usb_submit_urb(serial->rx_urb[i], flags);
  1902. if (result) {
  1903. dev_warn(&serial->parent->usb->dev,
  1904. "Failed to submit urb - res %d\n",
  1905. result);
  1906. break;
  1907. }
  1908. }
  1909. } else {
  1910. mutex_lock(&serial->shared_int->shared_int_lock);
  1911. if (!serial->shared_int->use_count) {
  1912. result =
  1913. hso_mux_submit_intr_urb(serial->shared_int,
  1914. hso_dev->usb, flags);
  1915. }
  1916. serial->shared_int->use_count++;
  1917. mutex_unlock(&serial->shared_int->shared_int_lock);
  1918. }
  1919. if (serial->tiocmget)
  1920. tiocmget_submit_urb(serial,
  1921. serial->tiocmget,
  1922. serial->parent->usb);
  1923. return result;
  1924. }
  1925. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1926. {
  1927. int i;
  1928. struct hso_serial *serial = dev2ser(hso_dev);
  1929. struct hso_tiocmget *tiocmget;
  1930. if (!serial)
  1931. return -ENODEV;
  1932. for (i = 0; i < serial->num_rx_urbs; i++) {
  1933. if (serial->rx_urb[i]) {
  1934. usb_kill_urb(serial->rx_urb[i]);
  1935. serial->rx_urb_filled[i] = 0;
  1936. }
  1937. }
  1938. serial->curr_rx_urb_idx = 0;
  1939. serial->curr_rx_urb_offset = 0;
  1940. if (serial->tx_urb)
  1941. usb_kill_urb(serial->tx_urb);
  1942. if (serial->shared_int) {
  1943. mutex_lock(&serial->shared_int->shared_int_lock);
  1944. if (serial->shared_int->use_count &&
  1945. (--serial->shared_int->use_count == 0)) {
  1946. struct urb *urb;
  1947. urb = serial->shared_int->shared_intr_urb;
  1948. if (urb)
  1949. usb_kill_urb(urb);
  1950. }
  1951. mutex_unlock(&serial->shared_int->shared_int_lock);
  1952. }
  1953. tiocmget = serial->tiocmget;
  1954. if (tiocmget) {
  1955. wake_up_interruptible(&tiocmget->waitq);
  1956. usb_kill_urb(tiocmget->urb);
  1957. }
  1958. return 0;
  1959. }
  1960. static void hso_serial_common_free(struct hso_serial *serial)
  1961. {
  1962. int i;
  1963. if (serial->parent->dev)
  1964. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1965. tty_unregister_device(tty_drv, serial->minor);
  1966. for (i = 0; i < serial->num_rx_urbs; i++) {
  1967. /* unlink and free RX URB */
  1968. usb_free_urb(serial->rx_urb[i]);
  1969. /* free the RX buffer */
  1970. kfree(serial->rx_data[i]);
  1971. }
  1972. /* unlink and free TX URB */
  1973. usb_free_urb(serial->tx_urb);
  1974. kfree(serial->tx_data);
  1975. }
  1976. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1977. int rx_size, int tx_size)
  1978. {
  1979. struct device *dev;
  1980. int minor;
  1981. int i;
  1982. minor = get_free_serial_index();
  1983. if (minor < 0)
  1984. goto exit;
  1985. /* register our minor number */
  1986. serial->parent->dev = tty_register_device(tty_drv, minor,
  1987. &serial->parent->interface->dev);
  1988. dev = serial->parent->dev;
  1989. dev->driver_data = serial->parent;
  1990. i = device_create_file(dev, &dev_attr_hsotype);
  1991. /* fill in specific data for later use */
  1992. serial->minor = minor;
  1993. serial->magic = HSO_SERIAL_MAGIC;
  1994. spin_lock_init(&serial->serial_lock);
  1995. serial->num_rx_urbs = num_urbs;
  1996. /* RX, allocate urb and initialize */
  1997. /* prepare our RX buffer */
  1998. serial->rx_data_length = rx_size;
  1999. for (i = 0; i < serial->num_rx_urbs; i++) {
  2000. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2001. if (!serial->rx_urb[i]) {
  2002. dev_err(dev, "Could not allocate urb?\n");
  2003. goto exit;
  2004. }
  2005. serial->rx_urb[i]->transfer_buffer = NULL;
  2006. serial->rx_urb[i]->transfer_buffer_length = 0;
  2007. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2008. GFP_KERNEL);
  2009. if (!serial->rx_data[i]) {
  2010. dev_err(dev, "%s - Out of memory\n", __func__);
  2011. goto exit;
  2012. }
  2013. }
  2014. /* TX, allocate urb and initialize */
  2015. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2016. if (!serial->tx_urb) {
  2017. dev_err(dev, "Could not allocate urb?\n");
  2018. goto exit;
  2019. }
  2020. serial->tx_urb->transfer_buffer = NULL;
  2021. serial->tx_urb->transfer_buffer_length = 0;
  2022. /* prepare our TX buffer */
  2023. serial->tx_data_count = 0;
  2024. serial->tx_buffer_count = 0;
  2025. serial->tx_data_length = tx_size;
  2026. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2027. if (!serial->tx_data) {
  2028. dev_err(dev, "%s - Out of memory", __func__);
  2029. goto exit;
  2030. }
  2031. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2032. if (!serial->tx_buffer) {
  2033. dev_err(dev, "%s - Out of memory", __func__);
  2034. goto exit;
  2035. }
  2036. return 0;
  2037. exit:
  2038. hso_serial_common_free(serial);
  2039. return -1;
  2040. }
  2041. /* Creates a general hso device */
  2042. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2043. int port_spec)
  2044. {
  2045. struct hso_device *hso_dev;
  2046. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2047. if (!hso_dev)
  2048. return NULL;
  2049. hso_dev->port_spec = port_spec;
  2050. hso_dev->usb = interface_to_usbdev(intf);
  2051. hso_dev->interface = intf;
  2052. kref_init(&hso_dev->ref);
  2053. mutex_init(&hso_dev->mutex);
  2054. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2055. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2056. return hso_dev;
  2057. }
  2058. /* Removes a network device in the network device table */
  2059. static int remove_net_device(struct hso_device *hso_dev)
  2060. {
  2061. int i;
  2062. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2063. if (network_table[i] == hso_dev) {
  2064. network_table[i] = NULL;
  2065. break;
  2066. }
  2067. }
  2068. if (i == HSO_MAX_NET_DEVICES)
  2069. return -1;
  2070. return 0;
  2071. }
  2072. /* Frees our network device */
  2073. static void hso_free_net_device(struct hso_device *hso_dev)
  2074. {
  2075. int i;
  2076. struct hso_net *hso_net = dev2net(hso_dev);
  2077. if (!hso_net)
  2078. return;
  2079. /* start freeing */
  2080. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2081. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2082. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2083. }
  2084. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2085. kfree(hso_net->mux_bulk_tx_buf);
  2086. remove_net_device(hso_net->parent);
  2087. if (hso_net->net) {
  2088. unregister_netdev(hso_net->net);
  2089. free_netdev(hso_net->net);
  2090. }
  2091. kfree(hso_dev);
  2092. }
  2093. /* initialize the network interface */
  2094. static void hso_net_init(struct net_device *net)
  2095. {
  2096. struct hso_net *hso_net = netdev_priv(net);
  2097. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2098. /* fill in the other fields */
  2099. net->open = hso_net_open;
  2100. net->stop = hso_net_close;
  2101. net->hard_start_xmit = hso_net_start_xmit;
  2102. net->tx_timeout = hso_net_tx_timeout;
  2103. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2104. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2105. net->type = ARPHRD_NONE;
  2106. net->mtu = DEFAULT_MTU - 14;
  2107. net->tx_queue_len = 10;
  2108. SET_ETHTOOL_OPS(net, &ops);
  2109. /* and initialize the semaphore */
  2110. spin_lock_init(&hso_net->net_lock);
  2111. }
  2112. /* Adds a network device in the network device table */
  2113. static int add_net_device(struct hso_device *hso_dev)
  2114. {
  2115. int i;
  2116. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2117. if (network_table[i] == NULL) {
  2118. network_table[i] = hso_dev;
  2119. break;
  2120. }
  2121. }
  2122. if (i == HSO_MAX_NET_DEVICES)
  2123. return -1;
  2124. return 0;
  2125. }
  2126. static int hso_radio_toggle(void *data, enum rfkill_state state)
  2127. {
  2128. struct hso_device *hso_dev = data;
  2129. int enabled = (state == RFKILL_STATE_ON);
  2130. int rv;
  2131. mutex_lock(&hso_dev->mutex);
  2132. if (hso_dev->usb_gone)
  2133. rv = 0;
  2134. else
  2135. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2136. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2137. USB_CTRL_SET_TIMEOUT);
  2138. mutex_unlock(&hso_dev->mutex);
  2139. return rv;
  2140. }
  2141. /* Creates and sets up everything for rfkill */
  2142. static void hso_create_rfkill(struct hso_device *hso_dev,
  2143. struct usb_interface *interface)
  2144. {
  2145. struct hso_net *hso_net = dev2net(hso_dev);
  2146. struct device *dev = &hso_net->net->dev;
  2147. char *rfkn;
  2148. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  2149. RFKILL_TYPE_WWAN);
  2150. if (!hso_net->rfkill) {
  2151. dev_err(dev, "%s - Out of memory\n", __func__);
  2152. return;
  2153. }
  2154. rfkn = kzalloc(20, GFP_KERNEL);
  2155. if (!rfkn) {
  2156. rfkill_free(hso_net->rfkill);
  2157. hso_net->rfkill = NULL;
  2158. dev_err(dev, "%s - Out of memory\n", __func__);
  2159. return;
  2160. }
  2161. snprintf(rfkn, 20, "hso-%d",
  2162. interface->altsetting->desc.bInterfaceNumber);
  2163. hso_net->rfkill->name = rfkn;
  2164. hso_net->rfkill->state = RFKILL_STATE_ON;
  2165. hso_net->rfkill->data = hso_dev;
  2166. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  2167. if (rfkill_register(hso_net->rfkill) < 0) {
  2168. kfree(rfkn);
  2169. hso_net->rfkill->name = NULL;
  2170. rfkill_free(hso_net->rfkill);
  2171. hso_net->rfkill = NULL;
  2172. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2173. return;
  2174. }
  2175. }
  2176. /* Creates our network device */
  2177. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  2178. {
  2179. int result, i;
  2180. struct net_device *net;
  2181. struct hso_net *hso_net;
  2182. struct hso_device *hso_dev;
  2183. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  2184. if (!hso_dev)
  2185. return NULL;
  2186. /* allocate our network device, then we can put in our private data */
  2187. /* call hso_net_init to do the basic initialization */
  2188. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2189. if (!net) {
  2190. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2191. goto exit;
  2192. }
  2193. hso_net = netdev_priv(net);
  2194. hso_dev->port_data.dev_net = hso_net;
  2195. hso_net->net = net;
  2196. hso_net->parent = hso_dev;
  2197. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2198. USB_DIR_IN);
  2199. if (!hso_net->in_endp) {
  2200. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2201. goto exit;
  2202. }
  2203. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2204. USB_DIR_OUT);
  2205. if (!hso_net->out_endp) {
  2206. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2207. goto exit;
  2208. }
  2209. SET_NETDEV_DEV(net, &interface->dev);
  2210. /* registering our net device */
  2211. result = register_netdev(net);
  2212. if (result) {
  2213. dev_err(&interface->dev, "Failed to register device\n");
  2214. goto exit;
  2215. }
  2216. /* start allocating */
  2217. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2218. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2219. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2220. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2221. goto exit;
  2222. }
  2223. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2224. GFP_KERNEL);
  2225. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2226. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2227. goto exit;
  2228. }
  2229. }
  2230. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2231. if (!hso_net->mux_bulk_tx_urb) {
  2232. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2233. goto exit;
  2234. }
  2235. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2236. if (!hso_net->mux_bulk_tx_buf) {
  2237. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2238. goto exit;
  2239. }
  2240. add_net_device(hso_dev);
  2241. hso_log_port(hso_dev);
  2242. hso_create_rfkill(hso_dev, interface);
  2243. return hso_dev;
  2244. exit:
  2245. hso_free_net_device(hso_dev);
  2246. return NULL;
  2247. }
  2248. static void hso_free_tiomget(struct hso_serial *serial)
  2249. {
  2250. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2251. if (tiocmget) {
  2252. kfree(tiocmget);
  2253. if (tiocmget->urb) {
  2254. usb_free_urb(tiocmget->urb);
  2255. tiocmget->urb = NULL;
  2256. }
  2257. serial->tiocmget = NULL;
  2258. }
  2259. }
  2260. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2261. static void hso_free_serial_device(struct hso_device *hso_dev)
  2262. {
  2263. struct hso_serial *serial = dev2ser(hso_dev);
  2264. if (!serial)
  2265. return;
  2266. set_serial_by_index(serial->minor, NULL);
  2267. hso_serial_common_free(serial);
  2268. if (serial->shared_int) {
  2269. mutex_lock(&serial->shared_int->shared_int_lock);
  2270. if (--serial->shared_int->ref_count == 0)
  2271. hso_free_shared_int(serial->shared_int);
  2272. else
  2273. mutex_unlock(&serial->shared_int->shared_int_lock);
  2274. }
  2275. hso_free_tiomget(serial);
  2276. kfree(serial);
  2277. kfree(hso_dev);
  2278. }
  2279. /* Creates a bulk AT channel */
  2280. static struct hso_device *hso_create_bulk_serial_device(
  2281. struct usb_interface *interface, int port)
  2282. {
  2283. struct hso_device *hso_dev;
  2284. struct hso_serial *serial;
  2285. int num_urbs;
  2286. struct hso_tiocmget *tiocmget;
  2287. hso_dev = hso_create_device(interface, port);
  2288. if (!hso_dev)
  2289. return NULL;
  2290. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2291. if (!serial)
  2292. goto exit;
  2293. serial->parent = hso_dev;
  2294. hso_dev->port_data.dev_serial = serial;
  2295. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2296. num_urbs = 2;
  2297. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2298. GFP_KERNEL);
  2299. /* it isn't going to break our heart if serial->tiocmget
  2300. * allocation fails don't bother checking this.
  2301. */
  2302. if (serial->tiocmget) {
  2303. tiocmget = serial->tiocmget;
  2304. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2305. if (tiocmget->urb) {
  2306. mutex_init(&tiocmget->mutex);
  2307. init_waitqueue_head(&tiocmget->waitq);
  2308. tiocmget->endp = hso_get_ep(
  2309. interface,
  2310. USB_ENDPOINT_XFER_INT,
  2311. USB_DIR_IN);
  2312. } else
  2313. hso_free_tiomget(serial);
  2314. }
  2315. }
  2316. else
  2317. num_urbs = 1;
  2318. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2319. BULK_URB_TX_SIZE))
  2320. goto exit;
  2321. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2322. USB_DIR_IN);
  2323. if (!serial->in_endp) {
  2324. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2325. goto exit2;
  2326. }
  2327. if (!
  2328. (serial->out_endp =
  2329. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2330. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2331. goto exit2;
  2332. }
  2333. serial->write_data = hso_std_serial_write_data;
  2334. /* and record this serial */
  2335. set_serial_by_index(serial->minor, serial);
  2336. /* setup the proc dirs and files if needed */
  2337. hso_log_port(hso_dev);
  2338. /* done, return it */
  2339. return hso_dev;
  2340. exit2:
  2341. hso_serial_common_free(serial);
  2342. exit:
  2343. hso_free_tiomget(serial);
  2344. kfree(serial);
  2345. kfree(hso_dev);
  2346. return NULL;
  2347. }
  2348. /* Creates a multiplexed AT channel */
  2349. static
  2350. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2351. int port,
  2352. struct hso_shared_int *mux)
  2353. {
  2354. struct hso_device *hso_dev;
  2355. struct hso_serial *serial;
  2356. int port_spec;
  2357. port_spec = HSO_INTF_MUX;
  2358. port_spec &= ~HSO_PORT_MASK;
  2359. port_spec |= hso_mux_to_port(port);
  2360. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2361. return NULL;
  2362. hso_dev = hso_create_device(interface, port_spec);
  2363. if (!hso_dev)
  2364. return NULL;
  2365. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2366. if (!serial)
  2367. goto exit;
  2368. hso_dev->port_data.dev_serial = serial;
  2369. serial->parent = hso_dev;
  2370. if (hso_serial_common_create
  2371. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2372. goto exit;
  2373. serial->tx_data_length--;
  2374. serial->write_data = hso_mux_serial_write_data;
  2375. serial->shared_int = mux;
  2376. mutex_lock(&serial->shared_int->shared_int_lock);
  2377. serial->shared_int->ref_count++;
  2378. mutex_unlock(&serial->shared_int->shared_int_lock);
  2379. /* and record this serial */
  2380. set_serial_by_index(serial->minor, serial);
  2381. /* setup the proc dirs and files if needed */
  2382. hso_log_port(hso_dev);
  2383. /* done, return it */
  2384. return hso_dev;
  2385. exit:
  2386. if (serial) {
  2387. tty_unregister_device(tty_drv, serial->minor);
  2388. kfree(serial);
  2389. }
  2390. if (hso_dev)
  2391. kfree(hso_dev);
  2392. return NULL;
  2393. }
  2394. static void hso_free_shared_int(struct hso_shared_int *mux)
  2395. {
  2396. usb_free_urb(mux->shared_intr_urb);
  2397. kfree(mux->shared_intr_buf);
  2398. mutex_unlock(&mux->shared_int_lock);
  2399. kfree(mux);
  2400. }
  2401. static
  2402. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2403. {
  2404. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2405. if (!mux)
  2406. return NULL;
  2407. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2408. USB_DIR_IN);
  2409. if (!mux->intr_endp) {
  2410. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2411. goto exit;
  2412. }
  2413. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2414. if (!mux->shared_intr_urb) {
  2415. dev_err(&interface->dev, "Could not allocate intr urb?");
  2416. goto exit;
  2417. }
  2418. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2419. GFP_KERNEL);
  2420. if (!mux->shared_intr_buf) {
  2421. dev_err(&interface->dev, "Could not allocate intr buf?");
  2422. goto exit;
  2423. }
  2424. mutex_init(&mux->shared_int_lock);
  2425. return mux;
  2426. exit:
  2427. kfree(mux->shared_intr_buf);
  2428. usb_free_urb(mux->shared_intr_urb);
  2429. kfree(mux);
  2430. return NULL;
  2431. }
  2432. /* Gets the port spec for a certain interface */
  2433. static int hso_get_config_data(struct usb_interface *interface)
  2434. {
  2435. struct usb_device *usbdev = interface_to_usbdev(interface);
  2436. u8 config_data[17];
  2437. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2438. s32 result;
  2439. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2440. 0x86, 0xC0, 0, 0, config_data, 17,
  2441. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2442. return -EIO;
  2443. }
  2444. switch (config_data[if_num]) {
  2445. case 0x0:
  2446. result = 0;
  2447. break;
  2448. case 0x1:
  2449. result = HSO_PORT_DIAG;
  2450. break;
  2451. case 0x2:
  2452. result = HSO_PORT_GPS;
  2453. break;
  2454. case 0x3:
  2455. result = HSO_PORT_GPS_CONTROL;
  2456. break;
  2457. case 0x4:
  2458. result = HSO_PORT_APP;
  2459. break;
  2460. case 0x5:
  2461. result = HSO_PORT_APP2;
  2462. break;
  2463. case 0x6:
  2464. result = HSO_PORT_CONTROL;
  2465. break;
  2466. case 0x7:
  2467. result = HSO_PORT_NETWORK;
  2468. break;
  2469. case 0x8:
  2470. result = HSO_PORT_MODEM;
  2471. break;
  2472. case 0x9:
  2473. result = HSO_PORT_MSD;
  2474. break;
  2475. case 0xa:
  2476. result = HSO_PORT_PCSC;
  2477. break;
  2478. case 0xb:
  2479. result = HSO_PORT_VOICE;
  2480. break;
  2481. default:
  2482. result = 0;
  2483. }
  2484. if (result)
  2485. result |= HSO_INTF_BULK;
  2486. if (config_data[16] & 0x1)
  2487. result |= HSO_INFO_CRC_BUG;
  2488. return result;
  2489. }
  2490. /* called once for each interface upon device insertion */
  2491. static int hso_probe(struct usb_interface *interface,
  2492. const struct usb_device_id *id)
  2493. {
  2494. int mux, i, if_num, port_spec;
  2495. unsigned char port_mask;
  2496. struct hso_device *hso_dev = NULL;
  2497. struct hso_shared_int *shared_int;
  2498. struct hso_device *tmp_dev = NULL;
  2499. if_num = interface->altsetting->desc.bInterfaceNumber;
  2500. /* Get the interface/port specification from either driver_info or from
  2501. * the device itself */
  2502. if (id->driver_info)
  2503. port_spec = ((u32 *)(id->driver_info))[if_num];
  2504. else
  2505. port_spec = hso_get_config_data(interface);
  2506. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2507. dev_err(&interface->dev, "Not our interface\n");
  2508. return -ENODEV;
  2509. }
  2510. /* Check if we need to switch to alt interfaces prior to port
  2511. * configuration */
  2512. if (interface->num_altsetting > 1)
  2513. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2514. interface->needs_remote_wakeup = 1;
  2515. /* Allocate new hso device(s) */
  2516. switch (port_spec & HSO_INTF_MASK) {
  2517. case HSO_INTF_MUX:
  2518. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2519. /* Create the network device */
  2520. if (!disable_net) {
  2521. hso_dev = hso_create_net_device(interface);
  2522. if (!hso_dev)
  2523. goto exit;
  2524. tmp_dev = hso_dev;
  2525. }
  2526. }
  2527. if (hso_get_mux_ports(interface, &port_mask))
  2528. /* TODO: de-allocate everything */
  2529. goto exit;
  2530. shared_int = hso_create_shared_int(interface);
  2531. if (!shared_int)
  2532. goto exit;
  2533. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2534. if (port_mask & i) {
  2535. hso_dev = hso_create_mux_serial_device(
  2536. interface, i, shared_int);
  2537. if (!hso_dev)
  2538. goto exit;
  2539. }
  2540. }
  2541. if (tmp_dev)
  2542. hso_dev = tmp_dev;
  2543. break;
  2544. case HSO_INTF_BULK:
  2545. /* It's a regular bulk interface */
  2546. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2547. && !disable_net)
  2548. hso_dev = hso_create_net_device(interface);
  2549. else
  2550. hso_dev =
  2551. hso_create_bulk_serial_device(interface, port_spec);
  2552. if (!hso_dev)
  2553. goto exit;
  2554. break;
  2555. default:
  2556. goto exit;
  2557. }
  2558. /* save our data pointer in this device */
  2559. usb_set_intfdata(interface, hso_dev);
  2560. /* done */
  2561. return 0;
  2562. exit:
  2563. hso_free_interface(interface);
  2564. return -ENODEV;
  2565. }
  2566. /* device removed, cleaning up */
  2567. static void hso_disconnect(struct usb_interface *interface)
  2568. {
  2569. hso_free_interface(interface);
  2570. /* remove reference of our private data */
  2571. usb_set_intfdata(interface, NULL);
  2572. }
  2573. static void async_get_intf(struct work_struct *data)
  2574. {
  2575. struct hso_device *hso_dev =
  2576. container_of(data, struct hso_device, async_get_intf);
  2577. usb_autopm_get_interface(hso_dev->interface);
  2578. }
  2579. static void async_put_intf(struct work_struct *data)
  2580. {
  2581. struct hso_device *hso_dev =
  2582. container_of(data, struct hso_device, async_put_intf);
  2583. usb_autopm_put_interface(hso_dev->interface);
  2584. }
  2585. static int hso_get_activity(struct hso_device *hso_dev)
  2586. {
  2587. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2588. if (!hso_dev->is_active) {
  2589. hso_dev->is_active = 1;
  2590. schedule_work(&hso_dev->async_get_intf);
  2591. }
  2592. }
  2593. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2594. return -EAGAIN;
  2595. usb_mark_last_busy(hso_dev->usb);
  2596. return 0;
  2597. }
  2598. static int hso_put_activity(struct hso_device *hso_dev)
  2599. {
  2600. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2601. if (hso_dev->is_active) {
  2602. hso_dev->is_active = 0;
  2603. schedule_work(&hso_dev->async_put_intf);
  2604. return -EAGAIN;
  2605. }
  2606. }
  2607. hso_dev->is_active = 0;
  2608. return 0;
  2609. }
  2610. /* called by kernel when we need to suspend device */
  2611. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2612. {
  2613. int i, result;
  2614. /* Stop all serial ports */
  2615. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2616. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2617. result = hso_stop_serial_device(serial_table[i]);
  2618. if (result)
  2619. goto out;
  2620. }
  2621. }
  2622. /* Stop all network ports */
  2623. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2624. if (network_table[i] &&
  2625. (network_table[i]->interface == iface)) {
  2626. result = hso_stop_net_device(network_table[i]);
  2627. if (result)
  2628. goto out;
  2629. }
  2630. }
  2631. out:
  2632. return 0;
  2633. }
  2634. /* called by kernel when we need to resume device */
  2635. static int hso_resume(struct usb_interface *iface)
  2636. {
  2637. int i, result = 0;
  2638. struct hso_net *hso_net;
  2639. /* Start all serial ports */
  2640. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2641. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2642. if (dev2ser(serial_table[i])->open_count) {
  2643. result =
  2644. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2645. hso_kick_transmit(dev2ser(serial_table[i]));
  2646. if (result)
  2647. goto out;
  2648. }
  2649. }
  2650. }
  2651. /* Start all network ports */
  2652. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2653. if (network_table[i] &&
  2654. (network_table[i]->interface == iface)) {
  2655. hso_net = dev2net(network_table[i]);
  2656. if (hso_net->flags & IFF_UP) {
  2657. /* First transmit any lingering data,
  2658. then restart the device. */
  2659. if (hso_net->skb_tx_buf) {
  2660. dev_dbg(&iface->dev,
  2661. "Transmitting"
  2662. " lingering data\n");
  2663. hso_net_start_xmit(hso_net->skb_tx_buf,
  2664. hso_net->net);
  2665. hso_net->skb_tx_buf = NULL;
  2666. }
  2667. result = hso_start_net_device(network_table[i]);
  2668. if (result)
  2669. goto out;
  2670. }
  2671. }
  2672. }
  2673. out:
  2674. return result;
  2675. }
  2676. static void hso_serial_ref_free(struct kref *ref)
  2677. {
  2678. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2679. hso_free_serial_device(hso_dev);
  2680. }
  2681. static void hso_free_interface(struct usb_interface *interface)
  2682. {
  2683. struct hso_serial *hso_dev;
  2684. struct tty_struct *tty;
  2685. int i;
  2686. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2687. if (serial_table[i]
  2688. && (serial_table[i]->interface == interface)) {
  2689. hso_dev = dev2ser(serial_table[i]);
  2690. spin_lock_irq(&hso_dev->serial_lock);
  2691. tty = tty_kref_get(hso_dev->tty);
  2692. spin_unlock_irq(&hso_dev->serial_lock);
  2693. if (tty)
  2694. tty_hangup(tty);
  2695. mutex_lock(&hso_dev->parent->mutex);
  2696. tty_kref_put(tty);
  2697. hso_dev->parent->usb_gone = 1;
  2698. mutex_unlock(&hso_dev->parent->mutex);
  2699. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2700. }
  2701. }
  2702. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2703. if (network_table[i]
  2704. && (network_table[i]->interface == interface)) {
  2705. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2706. /* hso_stop_net_device doesn't stop the net queue since
  2707. * traffic needs to start it again when suspended */
  2708. netif_stop_queue(dev2net(network_table[i])->net);
  2709. hso_stop_net_device(network_table[i]);
  2710. cancel_work_sync(&network_table[i]->async_put_intf);
  2711. cancel_work_sync(&network_table[i]->async_get_intf);
  2712. if (rfk)
  2713. rfkill_unregister(rfk);
  2714. hso_free_net_device(network_table[i]);
  2715. }
  2716. }
  2717. }
  2718. /* Helper functions */
  2719. /* Get the endpoint ! */
  2720. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2721. int type, int dir)
  2722. {
  2723. int i;
  2724. struct usb_host_interface *iface = intf->cur_altsetting;
  2725. struct usb_endpoint_descriptor *endp;
  2726. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2727. endp = &iface->endpoint[i].desc;
  2728. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2729. (usb_endpoint_type(endp) == type))
  2730. return endp;
  2731. }
  2732. return NULL;
  2733. }
  2734. /* Get the byte that describes which ports are enabled */
  2735. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2736. {
  2737. int i;
  2738. struct usb_host_interface *iface = intf->cur_altsetting;
  2739. if (iface->extralen == 3) {
  2740. *ports = iface->extra[2];
  2741. return 0;
  2742. }
  2743. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2744. if (iface->endpoint[i].extralen == 3) {
  2745. *ports = iface->endpoint[i].extra[2];
  2746. return 0;
  2747. }
  2748. }
  2749. return -1;
  2750. }
  2751. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2752. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2753. struct usb_device *usb, gfp_t gfp)
  2754. {
  2755. int result;
  2756. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2757. usb_rcvintpipe(usb,
  2758. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2759. shared_int->shared_intr_buf,
  2760. shared_int->intr_endp->wMaxPacketSize,
  2761. intr_callback, shared_int,
  2762. shared_int->intr_endp->bInterval);
  2763. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2764. if (result)
  2765. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2766. result);
  2767. return result;
  2768. }
  2769. /* operations setup of the serial interface */
  2770. static const struct tty_operations hso_serial_ops = {
  2771. .open = hso_serial_open,
  2772. .close = hso_serial_close,
  2773. .write = hso_serial_write,
  2774. .write_room = hso_serial_write_room,
  2775. .ioctl = hso_serial_ioctl,
  2776. .set_termios = hso_serial_set_termios,
  2777. .chars_in_buffer = hso_serial_chars_in_buffer,
  2778. .tiocmget = hso_serial_tiocmget,
  2779. .tiocmset = hso_serial_tiocmset,
  2780. .unthrottle = hso_unthrottle
  2781. };
  2782. static struct usb_driver hso_driver = {
  2783. .name = driver_name,
  2784. .probe = hso_probe,
  2785. .disconnect = hso_disconnect,
  2786. .id_table = hso_ids,
  2787. .suspend = hso_suspend,
  2788. .resume = hso_resume,
  2789. .reset_resume = hso_resume,
  2790. .supports_autosuspend = 1,
  2791. };
  2792. static int __init hso_init(void)
  2793. {
  2794. int i;
  2795. int result;
  2796. /* put it in the log */
  2797. printk(KERN_INFO "hso: %s\n", version);
  2798. /* Initialise the serial table semaphore and table */
  2799. spin_lock_init(&serial_table_lock);
  2800. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2801. serial_table[i] = NULL;
  2802. /* allocate our driver using the proper amount of supported minors */
  2803. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2804. if (!tty_drv)
  2805. return -ENOMEM;
  2806. /* fill in all needed values */
  2807. tty_drv->magic = TTY_DRIVER_MAGIC;
  2808. tty_drv->owner = THIS_MODULE;
  2809. tty_drv->driver_name = driver_name;
  2810. tty_drv->name = tty_filename;
  2811. /* if major number is provided as parameter, use that one */
  2812. if (tty_major)
  2813. tty_drv->major = tty_major;
  2814. tty_drv->minor_start = 0;
  2815. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2816. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2817. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2818. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2819. tty_drv->init_termios = tty_std_termios;
  2820. hso_init_termios(&tty_drv->init_termios);
  2821. tty_set_operations(tty_drv, &hso_serial_ops);
  2822. /* register the tty driver */
  2823. result = tty_register_driver(tty_drv);
  2824. if (result) {
  2825. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2826. __func__, result);
  2827. return result;
  2828. }
  2829. /* register this module as an usb driver */
  2830. result = usb_register(&hso_driver);
  2831. if (result) {
  2832. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2833. result);
  2834. /* cleanup serial interface */
  2835. tty_unregister_driver(tty_drv);
  2836. return result;
  2837. }
  2838. /* done */
  2839. return 0;
  2840. }
  2841. static void __exit hso_exit(void)
  2842. {
  2843. printk(KERN_INFO "hso: unloaded\n");
  2844. tty_unregister_driver(tty_drv);
  2845. /* deregister the usb driver */
  2846. usb_deregister(&hso_driver);
  2847. }
  2848. /* Module definitions */
  2849. module_init(hso_init);
  2850. module_exit(hso_exit);
  2851. MODULE_AUTHOR(MOD_AUTHOR);
  2852. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2853. MODULE_LICENSE(MOD_LICENSE);
  2854. MODULE_INFO(Version, DRIVER_VERSION);
  2855. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2856. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2857. module_param(debug, int, S_IRUGO | S_IWUSR);
  2858. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2859. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2860. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2861. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2862. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2863. module_param(disable_net, int, S_IRUGO | S_IWUSR);